|
Voiced by Amazon Polly |
Introduction
If you’ve spent some time writing SQL queries, you’ve probably used subqueries in one form or another. They help you break down complex problems, pull intermediate results, or filter data more intelligently. But one specific type of subquery often confuses people when they first encounter it: the scalar subquery.
Scalar subqueries are simple once you understand how they behave, and they can make your SQL code cleaner and more expressive. In this guide, we will walk you through what they are, how they work, and where you should use them or avoid them.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
What Is a Scalar Subquery?
A scalar subquery is a subquery that returns exactly one value. Not one row with multiple columns. Not multiple rows. Just one single value.
Think of it as a tiny SQL query embedded inside a larger one, working like a variable or a lookup value. Because the database expects a single value, scalar subqueries are allowed almost anywhere you can put a literal value:
- In the
SELECTlist - In the
WHEREclause - In the
CASEexpression - Even inside functions
Example:
|
1 2 3 |
SELECT first_name, last_name, (SELECT COUNT(*) FROM orders) AS total_orders FROM customers; |
This tiny subquery returns a single number: the total count of orders. That number is repeated for every row in the output. Simple and effective.
Why Use Scalar Subqueries?
You use scalar subqueries when you need a value that depends on other data in the database, and you don’t want to join another table unnecessarily.
Some common reasons to use them are:
- Avoiding complex joins
Sometimes, a join can make your result heavy, especially when you only need one value from another table. A scalar subquery keeps the query tidy.
- Calculating values per row
Scalar subqueries can reference columns from the outer query.
- Replacing window functions in systems that don’t fully support them
In older databases or specific environments, scalar subqueries can solve problems that window functions normally handle.
How Scalar Subqueries Work in the SELECT Clause?
A very common use is embedding calculations directly in your result set.
Example: Getting the total amount spent by each customer
|
1 2 3 4 5 |
SELECT c.customer_id, c.name, (SELECT SUM(amount) FROM orders o WHERE o.customer_id = c.customer_id) AS total_spent FROM customers c; |
This example shows the power of correlation. The subquery uses customer_id from the outer query. Every customer gets evaluated independently.
Using Scalar Subqueries in the WHERE Clause
Scalar subqueries are also very helpful for filtering.
Example: Customers who spent more than the average
|
1 2 3 4 5 |
SELECT customer_id, name FROM customers WHERE (SELECT SUM(amount) FROM orders o WHERE o.customer_id = customers.customer_id) > (SELECT AVG(total_spent) FROM yearly_sales_summary); |
Here, each customer’s total spending is compared against a single overall average retrieved by the subquery.
Using Scalar Subqueries in CASE Expressions
You can also use scalar subqueries to enrich or label your results.
Example: Tagging customers
|
1 2 3 4 5 6 7 |
SELECT c.customer_id, c.name, CASE WHEN (SELECT COUNT(*) FROM orders o WHERE o.customer_id = c.customer_id) > 10 THEN 'Loyal' ELSE 'New' END AS segment FROM customers c; |
This avoids writing an extra join or creating a temporary table just to perform a small comparison.
Performance Considerations
Scalar subqueries are convenient, but you need to be mindful of performance. When written poorly, they can behave like loops, running the same small query repeatedly for each row of the outer query.
This is usually not an issue when:
- The table sizes are small
- Indexes are in place
- The subquery is simple
However, problems arise when:
- The outer table has millions of rows
- The subquery does heavy aggregation
- There are missing indexes on join columns
For example, a scalar subquery in a SELECT clause inside a query returning 1 million rows means the subquery could run 1 million times unless the database optimizer rewrites it.
How to avoid slow scalar subqueries
- Add proper indexes: Especially on columns referenced in the subquery’s
WHEREclause. - Use JOINs when possible: If you can rewrite the subquery logically as a join with grouping, it may perform faster.
- Use Common Table Expressions (CTEs): You can precompute expensive aggregates.
- Check the execution plan: This tells you how often the subquery is being executed.
When Should You Prefer Scalar Subqueries?
Use them when:
- You need a single value for each row
- The logic is easier to read than a join
- The dataset is not too large
- The subquery references only indexed columns
Avoid them when:
- The outer table is huge
- The subquery does heavy computation
- The database engine doesn’t optimize correlated subqueries well
Conclusion
Scalar subqueries are one of those SQL features that feel intimidating at first glance, but once you understand them, you see how elegant they are. They let you embed small, powerful lookups directly inside your query, reducing complexity and keeping your logic self-contained.
If you’re comfortable with basic SQL and joins, scalar subqueries are a natural next step toward writing smarter and more expressive queries.
Drop a query if you have any questions regarding Scalar subqueries and we will get back to you quickly.
Empowering organizations to become ‘data driven’ enterprises with our Cloud experts.
- Reduced infrastructure costs
- Timely data-driven decisions
About CloudThat
CloudThat is an award-winning company and the first in India to offer cloud training and consulting services worldwide. As a Microsoft Solutions Partner, AWS Advanced Tier Training Partner, and Google Cloud Platform Partner, CloudThat has empowered over 850,000 professionals through 600+ cloud certifications winning global recognition for its training excellence including 20 MCT Trainers in Microsoft’s Global Top 100 and an impressive 12 awards in the last 8 years. CloudThat specializes in Cloud Migration, Data Platforms, DevOps, IoT, and cutting-edge technologies like Gen AI & AI/ML. It has delivered over 500 consulting projects for 250+ organizations in 30+ countries as it continues to empower professionals and enterprises to thrive in the digital-first world.
FAQs
1. What happens if a scalar subquery returns more than one row?
ANS: – SQL won’t allow it. The moment a scalar subquery returns more than one row, the query fails with an error. This usually means your filtering condition needs to be tightened.
2. Should scalar subqueries replace window functions?
ANS: – Not usually. Window functions are more efficient for tasks such as ranking, running totals, and complex analytics. Scalar subqueries can fill the gap when your system doesn’t support window functions or when the requirement is small and simple.
WRITTEN BY Aehteshaam Shaikh
Aehteshaam works as a SME at CloudThat, specializing in AWS, Python, SQL, and data analytics. He has built end-to-end data pipelines, interactive dashboards, and optimized cloud-based analytics solutions. Passionate about analytics, ML, generative AI, and cloud computing, he loves turning complex data into actionable insights and is always eager to learn new technologies.
Login

December 5, 2025
PREV
Comments