SQL Notes
Learn SQL Scalar Subqueries in detail, understand how they return a single value, how they are used in SELECT, WHERE, HAVING, and ORDER BY clauses, and solve advanced SQL problems efficiently.
You've covered simple subqueries, nested ones, correlated ones, EXISTS, and ALL/ANY operators.
All these subqueries return different amounts of data:
One Value
Multiple Values
Multiple Rows
Multiple ColumnsBut lots of operations need just one single value.
Think about:
- Average salary
- Maximum product price
- Minimum student marks
- Total number of orders
When a subquery returns exactly one value, it's called a Scalar Subquery.
These are super common because aggregate functions like AVG(), MAX(), MIN(), SUM(), and COUNT() typically return a single value.
Why "Scalar"?
In math and programming, scalar means a single value (not a vector or array).
Examples:
10 25 100
In SQL:
SELECT AVG(Salary)
FROM Employees;Returns:
65000
One value only. That's scalar.
Characteristics
A scalar subquery:
✅ Returns one row
✅ Returns one column
✅ Can go wherever a single value is expected
You can use it with:
=
>
<
>=
<=
<>and arithmetic operations.
Basic Example
Query:
SELECT EmployeeName
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);The subquery:
SELECT AVG(Salary)
FROM Employees;Returns:
65000
The outer query becomes:
SELECT EmployeeName
FROM Employees
WHERE Salary > 65000;One scalar value drives the comparison.
How They Work
Example:
SELECT ProductName
FROM Products
WHERE Price >
(
SELECT AVG(Price)
FROM Products
);Execution:
The subquery runs first.
Visual Flow
Subquery
│
↓
Single Value
│
↓
Outer Query
│
↓
ResultSimple and direct.
Sample Table
CREATE TABLE Employees (
EmployeeID INT,
EmployeeName VARCHAR(100),
Salary DECIMAL(10,2)
);Insert data:
INSERT INTO Employees VALUES
(1, 'Rahul', 50000),
(2, 'Priya', 60000),
(3, 'Amit', 70000),
(4, 'Neha', 80000);Scalar Subquery with AVG()
Query:
SELECT EmployeeName
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);Subquery returns:
65000
Output:
| EmployeeName |
|---|
| -------------- |
| Amit |
| Neha |
Scalar Subquery with MAX()
Find the highest-paid employee.
Query:
SELECT EmployeeName
FROM Employees
WHERE Salary =
(
SELECT MAX(Salary)
FROM Employees
);Subquery returns:
80000
Output:
| EmployeeName |
|---|
| -------------- |
| Neha |
Scalar Subquery with MIN()
Find the lowest-paid employee.
Query:
SELECT EmployeeName
FROM Employees
WHERE Salary =
(
SELECT MIN(Salary)
FROM Employees
);Output:
| EmployeeName |
|---|
| -------------- |
| Rahul |
Scalar Subquery with COUNT()
Query:
SELECT
(
SELECT COUNT(*)
FROM Employees
) AS TotalEmployees;Output:
| TotalEmployees |
|---|
| ---------------- |
| 4 |
Returns a single numeric value.
Scalar in SELECT Clause
Put a scalar subquery directly in SELECT:
SELECT
EmployeeName,
(
SELECT AVG(Salary)
FROM Employees
) AS AverageSalary
FROM Employees;Output:
| EmployeeName | AverageSalary |
|---|---|
| Rahul | 65000 |
| Priya | 65000 |
| Amit | 65000 |
| Neha | 65000 |
Each row shows the company average.
Scalar in WHERE Clause
Most common.
Example:
SELECT EmployeeName
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);Uses the scalar value as a filter.
Scalar in HAVING Clause
Filter grouped results:
SELECT DepartmentID,
AVG(Salary)
FROM Employees
GROUP BY DepartmentID
HAVING AVG(Salary) >
(
SELECT AVG(Salary)
FROM Employees
);Returns departments above company average.
Scalar in ORDER BY Clause
Uncommon but possible:
SELECT EmployeeName,
Salary
FROM Employees
ORDER BY
(
SELECT AVG(Salary)
FROM Employees
);Not usually recommended.
Scalar in INSERT
Insert the scalar value:
INSERT INTO EmployeeSummary
VALUES
(
(
SELECT COUNT(*)
FROM Employees
)
);The scalar value is inserted.
Scalar in UPDATE
Update based on a condition:
UPDATE Employees
SET Salary = Salary * 1.10
WHERE Salary <
(
SELECT AVG(Salary)
FROM Employees
);Employees below average get a raise.
Real-World: E-Commerce
Want: Products above average price.
Query:
SELECT ProductName
FROM Products
WHERE Price >
(
SELECT AVG(Price)
FROM Products
);Real-World: Banking
Want: Accounts above average balance.
Query:
SELECT AccountName
FROM Accounts
WHERE Balance >
(
SELECT AVG(Balance)
FROM Accounts
);Real-World: HR
Want: Employees earning above company average.
Query:
SELECT EmployeeName,
Salary
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);Scalar with Strings
Scalars aren't just numbers. Strings work too:
SELECT *
FROM Products
WHERE CategoryName =
(
SELECT CategoryName
FROM Categories
WHERE CategoryID = 5
);Scalar with Dates
SELECT *
FROM Orders
WHERE OrderDate >
(
SELECT MIN(OrderDate)
FROM Orders
);Returns orders after the earliest order date.
Error to Avoid: Multiple Rows
If your subquery returns more than one row, SQL will throw an error:
SELECT *
FROM Employees
WHERE Salary >
(
SELECT Salary
FROM Employees
);This returns four salary values, not one. SQL rejects it.
Fix: Use an aggregate function to return one value, or use IN/ANY/EXISTS instead.
Performance
Scalar subqueries in SELECT clauses run once per row of the outer query. For large datasets, this can be slow.
Consider:
- JOINs for better performance
- Pre-calculation in aggregation
Key Takeaways
✅ Scalar subqueries return one value
✅ Work with aggregate functions (AVG, MAX, MIN, COUNT, SUM)
✅ Can appear in SELECT, WHERE, HAVING, ORDER BY
✅ Simple and direct
✅ Perfect for single-value comparisons
✅ Watch out for multiple-row subqueries (error)
✅ May be slow in SELECT (runs per row)
Use scalar subqueries for single-value logic. They're clean and readable.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Scalar Subqueries.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL Complete Guide topic.
Search Terms
sql-complete-guide, sql complete guide, sql, complete, guide, subqueries, scalar, scalar subqueries
Related SQL Complete Guide Topics