SQL Interview: 81 COUNT(*) and COUNT(Column)

SQL Interview: 81 COUNT(*) and COUNT(Column)

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Development Level: Medium

Question:

You execute the following code:

DROP TABLE IF EXISTS dbo.Table1;
GO

CREATE TABLE dbo.Table1
(
    Column1 int NULL
);
GO

INSERT dbo.Table1 (Column1)
VALUES (1), (NULL), (NULL), (2);
GO

-- Query 1
SELECT COUNT(*), COUNT(Column1)
FROM dbo.Table1;

-- Query 2
SELECT COUNT(*), COUNT(Column1)
FROM dbo.Table1
WHERE Column1 = NULL;

What values will be returned for each query?

Answer:

The output for Query 1 will be:

4 and 2

COUNT(*) counts all rows that match, unrelated to NULL values.

COUNT(Column1) counts all rows where Column1 is not NULL.

Query 2 will return:

0 and 0

No rows will match the = NULL criteria. To find the NULL rows, the query would need to have a predicate of WHERE Column1 IS NULL.

2025-09-25