Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting the correct count in SQL

I am totally new to SQL. I have a simple select query similar to this:

SELECT COUNT(col1) FROM table1

There are some 120 records in the table and shown on the GUI. For some reason, this query always returns a number which is less than the actual count.

Can somebody please help me?

like image 503
bobby Avatar asked Nov 28 '22 08:11

bobby


2 Answers

Try

select count(*) from table1

Edit: To explain further, count(*) gives you the rowcount for a table, including duplicates and nulls. count(isnull(col1,0)) will do the same thing, but slightly slower, since isnull must be evaluated for each row.

like image 92
Blorgbeard Avatar answered Dec 30 '22 12:12

Blorgbeard


You might have some null values in col1 column. Aggregate functions ignore nulls. try this

SELECT COUNT(ISNULL(col1,0)) FROM   table1
like image 27
Gulzar Nazim Avatar answered Dec 30 '22 14:12

Gulzar Nazim