Subqueries
โ Report an issue with this lessonSELECT title, price
FROM courses
WHERE price > (SELECT AVG(price) FROM courses);
SELECT name FROM users
WHERE id IN (SELECT user_id FROM enrollments WHERE course_id = 3);
A subquery is a query nested inside another. They're often clearer than
a JOIN for "give me rows where some condition holds in another table" โ
though for large datasets, an equivalent JOIN can sometimes perform
better, which is worth checking with EXPLAIN.
Try it yourself
Exercise: Write a subquery selecting courses priced above the average price.
Expected output: open-ended โ there's no single correct output here, just get your code running without errors.
Run your code and get it working before marking this lesson complete.
// free preview
9 more lessons โ including Capstone project: a reporting query set โ plus a certificate are waiting.
Unlock the full course โ $149.99