Plate 18
Cardinality Instructor
Rare Filed · Card 01Card one. Stamped 21:40, which is when I stopped teaching and started writing. You asked how many orders Aiko has and you shipped the answer five. COUNT(*) says 5. COUNT(o.order_id) says 5. COUNT(DISTINCT o.order_id) says 2. All three ran in one query and all three are correct. Two orders, five line items, one FROM clause. I drew that crow’s foot this morning and left it up all day. You had forty minutes in front of it. Carla has no orders and still counts as 1.
Margin note
COUNT(*) counts join rows; COUNT(DISTINCT order_id) counts orders
SELECT c.name,
COUNT(*) AS count_star,
COUNT(o.order_id) AS count_order_id,
COUNT(DISTINCT o.order_id) AS count_distinct_orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
LEFT JOIN line_items li ON li.order_id = o.order_id
GROUP BY c.name;
COUNT(*) counts rows of the joined result, COUNT(col) counts non-NULL values, COUNT(DISTINCT col) counts distinct ones. Carla has no orders, so the outer join hands her one all-NULL row that COUNT(*) still counts.
Executed against DuckDB 1.5.3-r.3, 2026-09-05 · companion file: nora-companion.sql