Tags
Advanced SQL tutorial pdf, Count(*) VS Count(ColumnName) VS Count(1), Define below transformation in DFD?, Difference between Cached Report and Snapshot Report, Download SQL Questions, Download SQL Server Interview Question in PDF, Download SQL SERVER Interview questions, Download SQL Server Interview questions and answers, download sql server interview questions and answers pdf, download sql server interview questions by Pawan Khowal, download sql server interview questions by Pawan Kumar, download sql server interview questions by Pawan Kumar Khowal, Download T-SQL Interview Questions, Free Download SQL SERVER Interview questions, How to make connection with a FTP server?, How to show "No Data Found" Message to end user?, SQL, sql - Count(*) vs Count(1), sql - COUNT(*) vs. COUNT(1) vs. COUNT(pk): which is better?, SQL Best Practice: count(1) or count(*), SQL Common Interview Questions, SQL Common Interview Questions and answers, SQL FAQ, SQL FAQs, SQL Interview Q & A, SQL Interview Questions, SQL Queries asked in interviews, SQL Questions, SQL Server - COUNT(1) Vs COUNT(*) Vs COUNT(100) | Which one is fastest?, SQL Server - General Interview Questions and Answers, SQL Server developer Interview questions and answers, SQL Server developer Interview questions with answers, SQL SERVER Indexes, SQL SERVER Interview questions, SQL SERVER Interview questions & Answers, SQL Server Interview questions and answers, SQL Server Interview Questions and Answers - Free PDF, sql server interview questions and answers for net developers, SQL SERVER Tips, SQL Tips & Tricks, SQL Tips and Tricks, SQL Tricks, T-SQL Server Interview Questions, Transact-SQL COUNT(*) vs COUNT(column) vs COUNT(1
SQL Server – COUNT(1) Vs COUNT(*) Vs COUNT(1000) | Which one is fastest?
Well I often get this question from interviewers, team mates and my friends. Still many people don’t know the answer to this question. Well they all are same. Let me just create some sample data to prove it.
-- CREATE TABLE testCounts ( ID INT ) GO INSERT INTO testCounts VALUES ( 100 ) GO 10000 --
In the above statements I have created a table and inserted 10000 rows into it. Now we have to write SQL statements to fetch total number of rows from the table. Some of them are provided below.
-- SELECT COUNT(1) FROM dbo.testCounts SELECT COUNT(-1) FROM dbo.testCounts SELECT COUNT(*) FROM dbo.testCounts SELECT COUNT(100) FROM dbo.testCounts SELECT COUNT(10001) FROM dbo.testCounts SELECT COUNT('Pawan') FROM dbo.testCounts --
All the above statements will give us the same result i.e. 10000 rows. Well you can just put any junk in there still it will give the proper result. Check out the screen shot below for some of the outputs.
Now comes the main question, which one of these is fastest. Well they all are same in terms of performance. All are going to perform in the same manner. Check out the execution plans for all the queries. All are giving exact same execution plan.
I hope now things are clear. That’s all folks; I hope you’ve enjoyed the article and I’ll see you soon with more articles.
Thanks!
Pawan Kumar Khowal
MSBISKills.com
You must be logged in to post a comment.