Code. Models. Analysis. Decisions.

Writing Subqueries in MySQL

How to Use Subqueries for SQL

Subqueries, also known as inner queries are essentially queries within another query. Subqueries are set off by parentheses, directing the database engine to execute the parenthetical expression first. You may nest as many queries within one another as needed to transverse the data model.

Subqueries are in some ways similar to JOINS, and indeed many JOINS can be rewritten as subqueries, and vice versa. In terms of efficiency, JOINS are typically more optimal than an equivalent subquery. In terms of structure, a subquery is generally part of the WHERE clause, however as you will see in one of the tutorial examples, you can execute a subquery anywhere an expression is accepted.

You may also be interested in:

Creating Databases & Loading Data

Basic MySQL Queries


Using MySQL Text Functions

Statistics with MySQL

Download the data and code.