SQL QuestSQL Interview Questions › Joins

LEFT JOIN: Keep Everyone

MediumFreeQuerying BasicsJoinsAggregation & Grouping

A regular JOIN drops customers with no orders. A LEFT JOIN keeps them. Show every customer with their name, membership, order_count (0 if no orders), and total_spent (rounded to 2 decimals; 0 if no orders). Sort by total_spent descending, then name. This is your first encounter with how JOINs handle missing data.

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

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

orders

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

Expected output: Appears with order_count: 0, total_spent: 0

Hint

FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id keeps all customers. COUNT(o.order_id) returns 0 (not NULL) for unmatched rows. For the money column: COALESCE(ROUND(SUM(o.total), 2), 0) — round first, then default the no-order customers to 0.

Concepts

SELECT LEFT JOIN GROUP BY

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

In these company practice sets

Snowflake · LinkedIn

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

Related questions

Customers Who Never OrderedMedium · FreeAbove-Average Departments (Derived Table)Medium · FreeLEFT JOIN NULL Semantics: Inactive CustomersMedium · FreeManagement Hierarchy OverviewMedium · FreeCount Direct ReportsMedium · FreeCustomer Recency AnalysisMedium · 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