Subquery

SQL · SQL Subqueries

Theory

A subquery is a SELECT written inside another statement, in brackets. The inner query runs and its result feeds the outer query. WHERE salary > (SELECT AVG(salary) FROM employees) first works out the average salary, ₹63,000 for the table below, and then keeps employees earning more than that.

Subqueries come in three shapes. A scalar subquery returns one value and can be used anywhere a single value fits, like in SELECT or with = and >. A list subquery returns one column of many rows, used with IN, ANY, ALL or EXISTS. A table subquery returns rows and columns and sits in FROM as a derived table, which needs an alias. A scalar subquery that returns more than one row raises an error. A non-correlated subquery does not depend on the outer row, so it can be evaluated just once.

Why we use it

Many questions depend on another result first: above-average earners, customers in the top city, products never ordered. Subqueries let you compute that intermediate answer inside the same query.

When to use it

Use subqueries when one step's result feeds another, especially for comparisons with aggregates. For multi-step logic, CTEs (WITH) are often easier to read. When you need columns from both tables, a JOIN is usually better.

How it works

  1. Outer query needs a value or list
  2. Run the inner query in brackets
  3. Single value returned? Yes: Use it as a scalar in the comparison. No: Use it as a list or table.
  4. Outer query filters or joins using that result
  5. Return the outer query's rows

Syntax

SELECT columns FROM table
WHERE column = (SELECT ... FROM ...);

Example

SELECT name FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic