SQL SERVER – Puzzle – SELECT * vs SELECT COUNT(*)

SQL SERVER - Puzzle - SELECT * vs SELECT COUNT(*) selectstar-1 Earlier this weekend I have presented at Bangalore User Group on the subject of SQL Server Tips and Tricks. During the presentation I have asked a question to attendees. It was very interesting to see that I have received various different answer to my question. Here is the same puzzle for you and I would like to see what your answer to this question.

Question: SELECT * gives error when executed alone, but SELECT COUNT (*) does not. Why?

SQL SERVER - Puzzle - SELECT * vs SELECT COUNT(*) selectstar
Select * – resulting Error
SQL SERVER - Puzzle - SELECT * vs SELECT COUNT(*) selectcountstar
Select count * – NOT resulting Error

Please leave your answer as a comment over here. If you prefer, you can blog post about this on your blog and put a link here. I will publish valid answer with due credit in future blog posts. Remember one should not use SELECT * in the production environment as there are many different disadvantages of the SELECT *. The primary disadvantage is that it retrieves the data most of the time which users are not using normally in the production display. For example, not all the time we need to retrieve last updated time or even GUID if they exists on the table. The purpose of this blog post was for demonstration purpose and just for fun.

Reference: Pinal Dave (https://blog.sqlauthority.com)

SQL Scripts, SQL Server
Previous Post
SQL Azure – SQL Azure Throttling and Decoding Reason Codes
Next Post
SQL SERVER – BI Quiz – Troubleshooting Cube Performance

Related Posts

Leave a Reply