0PricingLogin
SQL Academy · Lesson

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

  1. Scalar, Row, and Table Subqueries
  2. Correlated vs Non-Correlated Subqueries
  3. Common Table Expressions (WITH)
  4. Recursive CTEs for Hierarchies
← Back to SQL Academy