Engine-agnostic SQL: the joins, aggregates, subqueries and set logic every data interview starts with.
Table products(id, category, price) where price is never NULL.
Return one row per category with category, product_count (how many
products it has) and total_price (the sum of their prices). Categories
are compared exactly as stored.
Table orders(id, customer_id), customer_id never NULL. Return
customer_id and order_count for every customer who has placed three
or more orders. Customers below the threshold must not appear at all.
Table reviews(id, product_id, rating) where rating is an integer 1-5
that is NULL when the reviewer left only text. For every product that has
at least one review return product_id, review_count (all reviews),
rated_count (reviews that carry a rating) and avg_rating (the average
Table tickets(id, team, status) where status is free text but the
report only cares about 'open', 'closed' and 'blocked'. Return one
row per team that has at least one ticket, with team, open_count,
closed_count and blocked_count. A team with no tickets in a given
Walk me through the difference between COUNT(*), COUNT(price) and
COUNT(DISTINCT price) on the same table. On a table of five rows where
two prices are missing and the other three are all 9.99, what does each
one return?
What does GROUP BY actually do to a result set? And why does the
database reject SELECT category, name, SUM(price) FROM products GROUP BY category?