Read first
Task 28
Subquery in FROM
A subquery can act as a temporary table.
Task
Why use a subquery in FROM?
The editor already contains a starting point. Change it until every check passes — the Next task button unlocks when it does.
What you start with
SELECT city, total FROM (SELECT city, COUNT(*) AS total FROM students GROUP BY city) AS per_city WHERE total > 2;
Syntax reference
SELECT city, total FROM (SELECT city, COUNT(*) AS total FROM students GROUP BY city) AS per_city WHERE total > 2;
Read it, then write it yourself. Copy-paste teaches nothing.
The sample database
CREATE TABLE courses (id INTEGER PRIMARY KEY, title TEXT, monthly_fee INTEGER); CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT, city TEXT, course_id INTEGER, joined TEXT); CREATE TABLE fees (id INTEGER PRIMARY KEY, student_id INTEGER, month TEXT, amount INTEGER, paid INTEGER); INSERT INTO courses VALUES (1,'Web Design',9000),(2,'Python',11667),(3,'JavaScript',11667),(4,'AI & Prompt Engineering',15000); INSERT INTO students VALUES (1,'Sara','Faisalabad',2,'2026-01-12'), (2,'Ali','Faisalabad',1,'2026-01-20'), (3,'Hina','Jhumra',2,'2026-02-02'), (4,'Bilal','Faisalabad',4,'2026-02-15'), (5,'Ayesha','Sargodha',3,'2026-03-01'), (6,'Usman','Jhumra',1,'2026-03-11'), (7,'Zainab','Faisalabad',4,'2026-04-05'), (8,'Hamza','Faisalabad',3,'2026-04-19'); INSERT INTO fees VALUES (1,1,'2026-01',11667,1),(2,1,'2026-02',11667,1), (3,2,'2026-01',9000,1),(4,2,'2026-02',9000,0), (5,3,'2026-02',11667,1),(6,3,'2026-03',11667,0), (7,4,'2026-02',15000,1),(8,4,'2026-03',15000,1), (9,5,'2026-03',11667,0), (10,6,'2026-03',9000,1),(11,6,'2026-04',9000,0), (12,7,'2026-04',15000,0), (13,8,'2026-04',11667,1);
SQL
1 automatic check Section 3 · Joins and subqueries
Code editor
Ctrl + Enter runs
Run your code to see the output and the checks.
Your result
Submit your code to see results
Write your code in the middle column and press Run code.
Why this works
Checks on this task:
- Check 1
One correct answer:
SELECT city, total FROM (SELECT city, COUNT(*) AS total FROM students GROUP BY city) AS per_city WHERE total > 2;
Track progress
0 of 56 tasks complete
Next task
Next task unlocks when every check passes.
Sign in to keep your code and certificates.
Next task (pass every check first)
Hint
Ask yourself: Why use a subquery in FROM?
Still stuck? Open “Syntax reference” in the left column, then type it yourself and change one value.