Counting Rows with SQL COUNT
COUNT is the first aggregate most SQL learners need: it answers how many.
Aggregate functions summarize many rows into fewer rows.
COUNT
answers a simple but common question: how many rows are there?
count(*)
counts every row.
count(column_name)
only counts rows where that column has a value.
Pick the count that matches the question
Use
count(*)
when the question is about rows. Use
count(column)
when the question is about known values in one column. That difference
matters whenever a column can be null.
select count(*) as total_rows,
count(nullable_column) as rows_with_value
from table_name Open the DuckDB playground with the matching dataset and query already filled in.
Schema · Garden Shop Table · orders 6 columns · 24 rows
One row per order. Unshipped orders have a null shipped_date.
Return one summary row showing the total number of orders
as total_orders and the number of orders where
shipped date is present as
orders_with_ship_date.
Run your query to inspect the result.
Expected answer
- Columns: total_orders, orders_with_ship_date.
- Rows: 1 summary row.
- Expected values: 24 total orders and 17 orders with a ship date.
Related
COUNT, SUM, AVG, MIN, MAX, GROUP BY, and HAVING.
COUNT, SUM, AVG, GROUP BY, and HAVING.
Practice aggregate reports, HAVING, and answer shape.
Avoid truncating fractional results.
Group or aggregate every selected column.
Practice buckets, business labels, and conditional counts.