Step 10
5 min read

Scalar Subqueries

Learn subqueries that return a single value - super simple!

What is a Scalar Subquery?

Students Table
idnameage
1John18
2Mary22
3Peter20
3 rows

A scalar subquery is a query inside another query that returns ONE single value (one row, one column).

Simple analogy: Like asking "What is the average price?" and getting one number: 50.

Basic Example

Students Table
idnameage
1John18
2Mary22
3Peter20
3 rows

Find students older than the average age:

SELECT name, age FROM students WHERE age > (SELECT AVG(age) FROM students);

How it works:

  1. Inner query calculates average: 20
  2. Outer query finds students where age > 20

Real Example

Students Table
idnameage
1John18
2Mary22
3Peter20
3 rows

Find products more expensive than average:

SELECT product_name, price FROM products WHERE price > (SELECT AVG(price) FROM products);

Another Example

Students Table
idnameage
1John18
2Mary22
3Peter20
3 rows

Find the oldest student:

SELECT name, age FROM students WHERE age = (SELECT MAX(age) FROM students);

Simple! Inner query finds max age, outer query finds who has that age.

Summary

Students Table
idnameage
1John18
2Mary22
3Peter20
3 rows

Scalar subquery = Returns ONE value Use in WHERE clause to compare with calculated values

Finished this topic?

Mark it complete to track your progress and maintain your streak!

SkillsetMaster - AI, Web Development & Data Analytics Courses