SQL Advanced (Analytics)
Back to SUBQUERIES - SCALAR, CORRELATED

Subqueries in MySQL | Intermediate MySQL

11 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.
Loading progress...

Key moments

  1. Subquery Fundamentals — An introduction defines subqueries as nested queries and outlines their usage in WHERE, SELECT, and FROM clauses.
  2. WHERE Clause Subqueries — A nested query filters demographic records using department IDs retrieved from the salary table.
  3. Operand Column Error — Demonstrating a multi-column subquery reveals the single-column constraint required by comparison operands.
  4. SELECT Clause Subqueries — A scalar subquery appends an overall average salary next to individual employee salaries without GROUP BY.
  5. FROM Clause Derived Tables — Nesting an aggregated query inside a FROM clause illustrates derived tables and the mandatory alias rule.
  6. Derived Column Aliasing — Resolving unknown column errors highlights the difference between backtick escaping and explicit column aliasing.
PDF notes

Frequently asked questions

Why does MySQL throw 'Operand should contain 1 column(s)' in a WHERE subquery?

The comparison operator expects a single scalar or one-dimensional list, but the inner SELECT projected two or more columns.

Why use a SELECT scalar subquery instead of a direct AVG() function?

Directly calling AVG() requires a GROUP BY which collapses rows, whereas a scalar subquery preserves individual row granularity alongside the aggregate.

Why is a table alias mandatory for subqueries in the FROM clause?

The SQL parser treats the subquery as a temporary derived table and requires an identifier to bind columns and resolve ambiguous references.

When should derived tables be replaced with CTEs or temporary tables?

Derived tables are ideal for quick inline summaries, but Common Table Expressions (CTEs) or temp tables offer better readability and reusability for complex multi-step queries.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.