Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "select count(*)" from nothing return 1

Tags:

With SQL Server 2012 :

use master
select *

yields

Must specify table to select from

which is exactly what I would expect.

But the funny thing is that

use master
select count(*)

returns 1.

Can someone explain to me what is counted here?

Edit : And possibly include sources...

like image 645
Johnny5 Avatar asked Dec 03 '13 17:12

Johnny5


1 Answers

SQL Server is (behind the curtain) effectively applying a from to a dummy table, which has only one row. Thus you will get 1 for your count.

select 'test'

will do the same thing, as an example, return 'test' one time.

It's like the DUAL table in Oracle, SYSDUMMY1 in DB2, etc.

As requested, here's a couple of links to MS Connect on this topic:

Clicky

More Clicky

like image 184
Andrew Avatar answered Jan 03 '23 00:01

Andrew