July 24, 2013 at 5:48 am
I accidently had an unfinished piece of SQL code in my query window during execution. The code was
select count(*)
Now to my supprise it returned 1. Anyone know why it returns 1? The only thing i can think of is that its counting itself.
/T
July 24, 2013 at 6:05 am
tommyh (7/24/2013)
I accidently had an unfinished piece of SQL code in my query window during execution. The code was
select count(*)
Now to my supprise it returned 1. Anyone know why it returns 1? The only thing i can think of is that its counting itself.
/T
Yes. That is a nature of COUNT(*) as it returns number of affected rows.
If you execute SELECT 'A' - it returns 1 row with 'A' in it.
For SELECT COUNT(*), SQL Server can see that only one row will be returned so: it returns 1.
Sometime, you may hear the question on interview: What is the shortest select query which will return 1 without using digits/numbers. In this question, most of interviewers are actually expecting this SELECT COUNT(*) thing. Althought it's not the shortest SELECT query to do so, it's a most expected one.
P.S. There are few shorter ways to get 1 returned:
SELECT DAY($)
or
SELECT EXP(£)
😉
July 24, 2013 at 9:48 am
I'm not sure what the point of this interview question would be, since in the real world if you needed a query that always returned 1 you would use SELECT 1.
July 24, 2013 at 10:05 am
dan-572483 (7/24/2013)
I'm not sure what the point of this interview question would be, since in the real world if you needed a query that always returned 1 you would use SELECT 1.
There is none!
As well as in many other common interview questions...
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply