Scalar, Row, and Table Subqueries
Use subqueries that return a single value, a single row, or a full table, and place them in SELECT, FROM, and WHERE clauses.
What Is a Subquery?
A subquery is a SELECT inside another SQL statement. It can be:
- A scalar subquery — returns a single value
- A row subquery — returns a single row
- A table subquery — returns a relation (used in FROM)
Scalar Subquery
Used wherever a single value is expected:
SELECT id, total,
total - (SELECT AVG(total) FROM orders) AS diff_from_avg
FROM orders
WHERE created_at >= NOW() - INTERVAL '30 days';All lessons in this course
- Scalar, Row, and Table Subqueries
- Correlated vs Non-Correlated Subqueries
- Common Table Expressions (WITH)
- Recursive CTEs for Hierarchies