COUNT
An aggregate that counts rows with COUNT(*), or non-NULL values with COUNT(column).
In more detail
The difference matters after a left join: COUNT(*) counts the NULL-filled row of a customer with no orders as one, while COUNT(o.id) correctly gives zero. COUNT(DISTINCT column) counts different values.
Example
COUNT(*) AS rows_returned, COUNT(o.id) AS real_ordersRelated terms
- aggregate function — A function that turns many input rows into one value, such as COUNT, SUM, AVG, MIN or MAX.
- DISTINCT — A keyword that removes duplicate rows from a query's answer, or duplicate values inside an aggregate such as COUNT.
- LEFT JOIN — A join that keeps every row of the left table and adds the matching right-hand rows, or NULLs when there is no match.
- NULL — SQL's marker for a missing or unknown value, which is not zero, not empty text and not equal to anything, even another NULL.
Taught in
- Why Tables And Relations Beat Files — PostgreSQL, Indexing & Relational Data Modeling
- What An Outer Join Really Returns — PostgreSQL, Indexing & Relational Data Modeling
- Aggregate Functions — PostgreSQL, Indexing & Relational Data Modeling