We know that in production code SELECT * is bad.

However, I will often write SELECT * in my SQL answers when

  • The OP failed to include their SQL (so I've got nothing to copy and paste)
  • The OP's question is really answered with the JOIN or WHERE
  • There's no syntax in my answer like GROUP BY that SELECT * can easily conflict with. [A]

Recently someone thought an answer of mine would be improved if I removed the SELECT *. Obviously, I think it is acceptable to answer with SELECT * but I can't say why.

[A] I'm pretending that MySQL and its treatment of GROUP BY don't exist

share|improve this question
2  
I suspect that 'acceptable' depends on precisely who is currently looking at your answer, and whether that person has votes to spare. In the php tag specifically I can think of one user that'll down-vote for the presence of (though possibly mitigated by explanation of/disclaimer for) a bad practice. –  David Thomas May 16 '11 at 22:47
2  
I like Robert's answer but I think SELECT ... FROM also makes sense even to SQL newbies. –  NickC May 17 '11 at 0:15
add comment

3 Answers

up vote 16 down vote accepted

Just change it to

SELECT [your fields here] FROM

Or remind the OP to replace the asterisk with actual fields.

share|improve this answer
1  
I think I'll start doing this since the intent is clear, isn't a bad practice, and I can still be lazy –  Some Helpful Commenter May 16 '11 at 23:24
    
-1 invalid column alias. Just kidding :) –  NickC May 17 '11 at 0:09
    
I usually use <columns> –  Joel Coehoorn May 17 '11 at 3:51
    
How about SELECT yourcolumn1, yourcolumn2, etc FROM yourtable ? –  Piskvor May 17 '11 at 14:27
add comment

Bad practices are OK as long as you note that they are bad practices. If you say something like "I'm doing this because it's shorter/unimportant/a placeholder", then it is clear to anyone reading your answer that they shouldn't copy that code directly into their finished product. However, if you don't have a disclaimer of some sort, an inexperienced programmer may believe that it is OK to use your code as-is, which could lead to future problems and them citing your answer as to why they thought it should work.

share|improve this answer
add comment

I generally leave some kind of "please don't be as lazy as me" note at my own answers every time I use SELECT *, empty catch blocks and any other kind of dangerous stuff :D. This generally avoids both bad practices dissemination and "please don't do that" comments.

share|improve this answer
add comment

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .