Skip to main content
SQL โ€ข LESSON 13

SQL Subqueries

How can we filter records using a dynamically computed value from another query?

Intermediateโฑ 2 Minutesโญ 230 XP
๐Ÿค” THE QUESTION

How can we filter records using a dynamically computed value from another query?

๐Ÿ’ก WHAT IS IT?

A scalar subquery is a nested query enclosed in parentheses that computes an intermediate value.

๐ŸŽฏ WHAT IS IT USED FOR?

Finding records above average, comparing against dynamic thresholds, multi-tier data lookup.

๐Ÿ’ป EXAMPLE
SELECT *
FROM employees
WHERE salary > (
  SELECT AVG(salary)
  FROM employees
);

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for SQL Subqueries.

  • โœ“ Scalar Subquery
  • โœ“ Nested SELECT
  • โœ“ Dynamic thresholds
  • โœ“ AVG() in subquery