0Pricing
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.

Scalar, Row, and Table Subqueries is a free SQL Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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';

Row Subquery

Returns one row of multiple columns. Less common, mostly used with row constructors:

SELECT *
FROM orders
WHERE (status, country) = (
  SELECT status, country FROM orders WHERE id = 1
);

Table Subquery in FROM

Treat a query result like a temporary table:

SELECT country, AVG(big_order_total) AS avg_big
FROM (
  SELECT country, total AS big_order_total
  FROM orders
  WHERE total > 1000
) AS sub
GROUP BY country;

Aliasing FROM Subqueries

Every FROM-subquery requires an alias in PostgreSQL — even if you don't use it:

SELECT * FROM (SELECT 1) sub;     -- "sub" required

Subqueries in WHERE

Filter by a derived set of values:

SELECT * FROM users
WHERE id IN (
  SELECT user_id FROM orders WHERE total > 100
);

EXISTS / NOT EXISTS

Test whether a subquery returns at least one row:

SELECT * FROM users u
WHERE EXISTS (
  SELECT 1 FROM orders o WHERE o.user_id = u.id
);

Subqueries in SELECT

Embed a per-row scalar subquery:

SELECT id, full_name,
  (SELECT COUNT(*) FROM orders o WHERE o.user_id = u.id) AS order_count
FROM users u;

-- Often readable, but a LEFT JOIN + GROUP BY is usually faster.

Subqueries as Expressions

A scalar subquery returning more than one row is an error:

SELECT (SELECT email FROM users);
-- ERROR: more than one row returned by a subquery used as an expression

When to Refactor to JOIN

Many "x IN (SELECT...)" can be rewritten as JOIN. Pick the form that reads clearer; modern planners often produce the same plan.

Subqueries vs CTEs

If you need the same subquery twice, give it a name with a CTE — covered in the next lesson.

Recap

Subqueries come in three flavours.

  • Scalar in SELECT/WHERE
  • Row in equality tests
  • Table in FROM
  • EXISTS for "any match" tests

Quick Check

Which subquery form is used inside a FROM clause as a temporary table?

Frequently asked questions

Is the “Scalar, Row, and Table Subqueries” lesson free?

Yes — the full text of “Scalar, Row, and Table Subqueries” is free to read here on the web, and the SQL Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Academy course, upgrade to CoddyKit PRO.

What will I learn in “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. You practise SQL Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SQL Academy?

No prior experience is required. SQL Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Scalar, Row, and Table Subqueries” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SQL Academy lesson?

Yes. Every SQL Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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