views:

46

answers:

2

What is the difference between these two methods?

select count(*) from `table` where `column` = 'value';

and

select count(`column` = 'value') from `table`;

To me they seem to do the same thing; I assume this is not the case for the database. Does one method leverage indexes better than the other?

In this case I use MySQL but a general answer is preferred here.

+3  A: 

count(column = 'value')

Would generally seem to imply you are counting the boolean result of the comparison, which should count all the rows in the table, though perhaps your particular DBMS has an exception.

Filter the query on the where clause. It will be a good habit in case you ever need to write a similar query for a different database.

wllmsaccnt
A: 

Running your samples through mySQL yields different results. The second query you posted doesn't restrict the result set - all rows are returned. The first query, however, does.

DeathMagus
You appear to be correct, I'm not sure why I thought otherwise.
Core Xii
Oddly enough, you could probably do a SUM(CAST(`column` = 'value') AS BIT) and retrieve the count, but that seems like a lot of obscure work to avoid a where clause. The casting might be unnecessary, depending on your DBMS.
wllmsaccnt