SQL QuestSQL Interview Questions › Subqueries & CTEs

A Subquery That Returns a Set (IN)

EasyFreeQuerying BasicsSubqueries & CTEs

Marketing is planning a US campaign and wants the customers who have ordered from the USA.

Show name and membership for those customers. Order by name.

The new idea: a subquery doesn't have to return one value — it can return a whole column of values, and IN checks membership against that list. WHERE customer_id IN (SELECT customer_id FROM orders WHERE country = 'USA') reads almost exactly like the English sentence.

Notice the shape difference from the previous challenge: > needs a single number, IN needs a set. Feeding a multi-row subquery to > is an error, and it's one of the first real error messages people hit.

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: 3 rows — Daniel Martinez, John Smith, Michael Brown

Hint

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

Concepts

SELECT Subquery IN WHERE

Practise the topic: SQL practice questions · CTE practice

Related questions

A Subquery That Returns One ValueEasy · FreeGive a Query a Name (WITH)Easy · FreeMovies Rated Above the AverageEasy · FreeEvery Film by a Director Who Once Hit 8.5Easy · FreeFilter on an Average You Just ComputedEasy · FreeBetter Than Its Own GenreEasy · 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