5

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

2
  • 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. Commented May 16, 2011 at 22:47
  • 2
    I like Robert's answer but I think SELECT ... FROM also makes sense even to SQL newbies.
    – Nicole
    Commented May 17, 2011 at 0:15

3 Answers 3

16

Just change it to

SELECT [your fields here] FROM

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

4
  • 1
    I think I'll start doing this since the intent is clear, isn't a bad practice, and I can still be lazy Commented May 16, 2011 at 23:24
  • -1 invalid column alias. Just kidding :)
    – Nicole
    Commented May 17, 2011 at 0:09
  • I usually use <columns> Commented May 17, 2011 at 3:51
  • How about SELECT yourcolumn1, yourcolumn2, etc FROM yourtable ? Commented May 17, 2011 at 14:27
8

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.

3

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.

You must log in to answer this question.

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