Sub-query in a WHERE clause with IN
A sub-query is a SELECT inside brackets in a WHERE clause. It returns a list of values, and IN keeps only the rows whose value is in that list. NOT IN keeps the rows that are not in it.
Understanding Sub-query in a WHERE clause with IN in QCE Digital Solutions Unit 2
What it does: The inner SELECT sits in brackets inside the WHERE clause. It runs first and gives IN a list of values to test against. This finds students with a mark above 80 without joining the tables. A join could also do it, but the sub-query only returns the students columns, and no columns from results. IN keeps only the rows whose value appears in the list. BETWEEN tests a range, not a list. The sub-query must return one column that matches the type of the field before IN, so use SELECT studentId.
Common mistakes: The FROM keyword is missing. The outer query must name its table with FROM, so it should be SELECT firstName FROM students. The sub-query returns a list of productId values. The = operator only compares to one value, so it should be WHERE productId IN (.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 305–336) and word search (Modules 234–409).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
IN (SELECT studentId FROM results)Fill in the Blanks
Spot the Error
Student with a class code? Join your class →