52. Order Count Per Customer (Scalar Subquery)
EasySubqueriesscalar subquerysubquery in SELECTcorrelated subquery
A scalar subquery is a subquery that returns exactly one row and one column, so it can stand in wherever a single value is expected — including the SELECT list. When it references the outer row (here c.id) it is also correlated: it re-runs once per outer row.
Task: for every customer, return their name and how many orders they have placed.
Return columns name, order_count. Order by name ascending.
Tables
Loading schema…
Expected outputrows must come back in this order
Computing expected output…
This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases — 5test cases in all. A sloppy query that only fits the example won't pass.
SQL· SQLite · runs in your browser
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.