0

I found this posted on an Internet forum:

You could find out you're one of those new fangled web applications developers who don't actually know much about databases and don't see anything wrong with

SELECT * FROM `tbl_products` ORDER BY `product_times_bought` DESC LIMIT 0, 500

I'm new to databases, and this looks like a valid query to me. Yet some guy was saying that there is something wrong with using such queries in practice. I don't know why: efficiency? robustness? What could be wrong with this query?

Community
  • 1
  • 1
Samhan
  • 3
  • 2
  • What is the error that tells you when run the query? The sintax of the query is correct – doctore Jun 07 '11 at 07:25
  • nothing wrong with the syntax. You do not give us all the necessary info to be able to answer your question. – Itay Moav -Malimovka Jun 07 '11 at 07:25
  • @Dev @doctore @Dalen [http://stackoverflow.com/q/321299/677526](http://stackoverflow.com/q/321299/677526) [http://stackoverflow.com/q/65512/677526](http://stackoverflow.com/q/65512/677526) –  Jun 07 '11 at 08:07
  • @lunchmeat317 What is about the misuse of select * in this case? @Samhan said that this query gives him an error, but the sintax is correct. Other thing is that the best practice is select the columns that you want instead of all columns. – doctore Jun 07 '11 at 08:23
  • @doctore Samhan never said he got an error from this. He did say that he saw a statement on an internet forum and wondered what was wrong with it. The "wrong" in this example isn't a syntax error; the "wrong" is specified in the links I posted above. –  Jun 07 '11 at 14:19

1 Answers1

2

Dont see any syntax error, but its the * which is considered bad practice. The issue with that is, you have no control on which columns the database returns, plus it returns all of them (wasted bandwitdh). It could have other issues like breaking the order it the table is recreated and if a column you need is not there, you wont catch that error in the select query itself, plus reading it doesnt tell you which columns you actually need (mostly you dont need all the columns).So, you should ask the specific columns you need instead of a generic *.

Guys, any other bad practices or recommendations?

Jai
  • 3,549
  • 3
  • 23
  • 31