SQL QuestSQL Interview Questions › Joins

Counting Across a JOIN

EasyFreeQuerying BasicsJoinsAggregation & Grouping

Retention wants how many orders each customer has placed, most active first.

Join orders to customers, and show name and order_count. Order by order_count descending, then name.

The new idea: GROUP BY works across a join exactly like it works on one table. Join first, group second — SQL builds the combined rows, then collapses them.

Now count your output rows. You get 10, not 16. This is the inner join from the previous challenge dropping the six customers with no orders, and it matters: a "customers by order count" report that silently omits your least engaged customers is the report you'd most want them in. Fixing that is what challenge 106 does with a LEFT JOIN.

Solve it in the browser editor →

Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".

Schema

orders

order_idcustomer_idproductcategoryquantitypricetotalorder_datecountrystatus
11Laptop ProElectronics11299.991299.992024-01-15USAcompleted
22Wireless MouseElectronics249.9999.982024-01-16Canadacompleted
33Office ChairFurniture1349.99349.992024-01-17USAcompleted

customers

customer_idnameemailsignup_datemembershiptotal_orders
1John Smithjohn.smith@email.com2023-01-15Gold15
2Emma Wilsonemma.wilson@email.com2023-03-20Silver8
3Michael Brownmichael.brown@email.com2023-02-10Gold12

Expected output: 10 rows — John Smith 6, Emma Wilson 5, Michael Brown 5, ...

Hint

The hint for this one spells out most of the query, so it stays in the editor. Reach for SELECT, JOIN, GROUP BY, and open the hint there if you stall.

Concepts

SELECT JOIN GROUP BY COUNT

Practise the topic: SQL practice questions · JOIN practice · GROUP BY exercises

In these company practice sets

Stripe · Walmart · TikTok

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Your First JOINEasy · FreeJOIN with a FilterEasy · FreeLEFT JOIN: Watch the NULLs AppearEasy · FreeTransaction Share by Merchant CategoryEasy · FreeSignup-Month Cohort SpendEasy · FreeCard Spend by CountryEasy · Free

Where would this cost you points in an interview?

Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.

Take the readiness test