Subqueries

Use the result of one query inside another.

Above average

SQL
SELECT name FROM students
WHERE marks > (SELECT AVG(marks) FROM students)
ORDER BY name;
Output
Asha
Chitra
Esther

Second highest marks

A favourite placement question.

SQL
SELECT MAX(marks) FROM students
WHERE marks < (SELECT MAX(marks) FROM students);
Output
88

Inside IN

SQL
SELECT name FROM students
WHERE id IN (SELECT student_id FROM enrolments)
ORDER BY name;
Output
Asha
Chitra
Esther
โ† JoinsChanging data โ†’