01 / 21

Scalar
Subqueries

Learn how to embed a query inside another query to compute single values on the fly: from averages to lookups, all within your SELECT statement.

SQL

What Is a Subquery?

A subquery is a complete SQL query nested inside another SQL statement. It is enclosed in parentheses and can appear in different clauses: SELECT, FROM, WHERE, and more.

SELECT name, (SELECT COUNT(*) FROM orders) AS total_orders FROM customers;

What Makes It "Scalar"?

A scalar subquery is a subquery that returns exactly one row and one column: a single value. The word "scalar" comes from mathematics, meaning a single number rather than a vector or table.

✓ Scalar
Returns one value: a single number, string, or date.
✕ Non-Scalar
Returns multiple rows or columns: an entire table or list.

Why Use Scalar Subqueries?

Compare
Show each row alongside a global aggregate like an overall average.
Lookup
Pull a single value from another table without a full JOIN.
Compute
Calculate derived metrics inline, keeping queries compact.

Scalar subqueries let you add context to every row: "How does this employee's salary compare to the company average?" becomes a single query instead of two.

The Core Rule

OUTER QUERY SELECT column_a, SCALAR SUBQUERY (SELECT AGG(col) FROM table WHERE ...) AS alias FROM main_table; Returns ONE value

Anatomy of the Syntax

SELECT column_1, column_2, (SELECT AGG_FUNCTION(col) FROM other_table WHERE condition) AS alias_name FROM main_table;
1
Parentheses wrap the entire inner SELECT statement.
2
One aggregate or expression ensures a single return value.
3
AS alias_name gives the computed column a readable name.
4
The outer query runs once per row, embedding the scalar result in every row.

Scalar vs. Table Subqueries

Scalar Subquery Table Subquery
Returns 1 row, 1 column Multiple rows/columns
Used in SELECT, WHERE, HAVING FROM, WHERE (with IN/EXISTS)
Common pattern Aggregate function Derived table, IN list
Error if wrong? Yes: >1 row fails No: returns a set
Example use Compare row to average Filter by a set of IDs

How to Apply It

1
Identify the value you need: What single number or string do you want to add to each row?
2
Write the inner query alone first: Run it separately to confirm it returns exactly one value.
3
Wrap it in parentheses: Place it in your SELECT clause as a new column expression.
4
Give it an alias: Use AS to name the result column clearly.
5
Test the full query: Run it and verify the scalar value appears correctly in every row.

Example 1: Company Average

Show each employee's salary alongside the company-wide average salary.

SELECT name, salary, (SELECT AVG(salary) FROM employees) AS avg_salary FROM employees;
name salary avg_salary Alice 60,000 55,000 Bob 50,000 55,000 Carol 55,000 55,000 Same value in every row

Example 2: Difference From Average

Compute how much each employee's salary differs from the company average.

SELECT name, salary, salary - (SELECT AVG(salary) FROM employees) AS diff_from_avg FROM employees;

Example 3: Cross-Table Lookup

Show each order along with the customer name from a different table, using a correlated scalar subquery.

SELECT order_id, amount, (SELECT name FROM customers c WHERE c.id = orders.customer_id) AS customer_name FROM orders;

Correlated vs. Uncorrelated

Uncorrelated
The inner query is independent: it can run on its own without referencing the outer query. Runs once.
Correlated
The inner query references a column from the outer query. Runs once per outer row.
Uncorrelated SELECT AVG(sal) FROM employees Runs once: 55000 Correlated SELECT name FROM customers c WHERE c.id = orders.cust_id Runs per row

Example 4: Using MAX

Display each product alongside the highest price in the entire catalog.

SELECT product_name, price, (SELECT MAX(price) FROM products) AS max_price FROM products;

Example 5: Percentage of Total

Calculate each department's budget as a percentage of the total company budget.

SELECT dept_name, budget, ROUND(budget * 100.0 / (SELECT SUM(budget) FROM departments), 1) AS pct_of_total FROM departments;

Common Mistakes

✕ Returning Multiple Rows
Forgetting a WHERE clause or aggregate, so the subquery returns a table instead of a single value. The database will throw an error.
✕ Missing Parentheses
Leaving off the parentheses around the subquery. SQL requires them to know where the inner query begins and ends.
✕ Forgetting the Alias
Not using AS to name the computed column. Without an alias, the result column gets a messy auto-generated name.

Summary at a Glance

Concept Key Point
Scalar Subquery Returns exactly 1 row, 1 column
Placement Inside SELECT clause as a column
Best With AVG, SUM, COUNT, MAX, MIN
Uncorrelated Runs once, same value every row
Correlated Runs per row, can reference outer columns
Must Have Parentheses + alias with AS

Question 1 of 5

A scalar subquery must return:

Question 2 of 5

Which SQL clause is this lesson focused on placing scalar subqueries into?

Question 3 of 5

What will happen if you write a scalar subquery in SELECT that returns 5 rows?

Question 4 of 5

Given the query below, what does avg_score represent?

SELECT student_name, score, (SELECT AVG(score) FROM exams) AS avg_score FROM exams;

Question 5 of 5

Which of the following is not a valid scalar subquery in SELECT?