Mysql Subqueries

MySQL: Subqueries

This tutorial explains how to use subqueries in MySQL with syntax and examples.

What is a subquery in MySQL?

In MySQL, a subquery is a query within a query. You can create subqueries within your SQL statements. These subqueries can reside in the WHERE clause, the FROM clause, or the SELECT clause.

Note

  • In MySQL, a subquery is also called an INNER QUERY or INNER SELECT.
  • In MySQL, the main query that contains the subquery is also called the OUTER QUERY or OUTER SELECT.

WHERE clause

Most often, the subquery will be found in the WHERE clause. These subqueries are also called nested subqueries.

For example:

SELECT c.contact_id, c.last_name
FROM contacts c
WHERE c.site_name IN
   (SELECT a.site_name
    FROM address_book a
    WHERE a.address_book_id  50);

The subquery portion of the SELECT statement above is:

(SELECT a.site_name
 FROM address_book a
 WHERE a.address_book_id  50);

This subquery allows you to find all site_name values from the address_book table that have an address_book_id less than 50. The subquery is then used to filter the results from the main query using the IN condition.

This subquery could have alternatively been written as an INNER join as follows:

SELECT c.contact_id, c.last_name
FROM contacts c
INNER JOIN address_book a
ON c.site_name = a.site_name
WHERE a.address_book_id  50;

This INNER JOIN would run more efficiently than the original subquery. It is important to note, though, that not all subqueries can be rewritten using joins.

FROM clause

A subquery can also be found in the FROM clause. These are called inline views.

For example:

SELECT contacts.last_name, subquery1.total_size
FROM contacts,
 (SELECT site_name, SUM(file_size) AS total_size
  FROM pages
  GROUP BY site_name) subquery1
WHERE subquery1.site_name = contacts.site_name;

In this example, we've created a subquery in the FROM clause as follows:

(SELECT site_name, SUM(file_size) AS total_size
 FROM pages
 GROUP BY site_name) subquery1

This subquery has been aliased with the name subquery1. This will be the name used to reference this subquery or any of its fields.

SELECT clause

A subquery can also be found in the SELECT clause. These are generally used when you wish to retrieve a calculation using an aggregate function such as the SUM, COUNT, MIN, MAX , or AVG function, but you do not want the aggregate function to apply to the main query.

For example:

SELECT p1.site_name,
  (SELECT MAX(file_size)
   FROM pages p2
   WHERE p1.site_id = p2.site_id) subquery2
FROM pages p1;

In this example, we've created a subquery in the SELECT clause as follows:

(SELECT MAX(file_size)
 FROM pages p2
 WHERE p1.site_id = p2.site_id) subquery2

The subquery has been aliased with the name subquery2. This will be the name used to reference this subquery or any of its fields.

The trick to placing a subquery in the SELECT clause is that the subquery must return a single value. This is why an aggregate function such as the SUM, COUNT, MIN, MAX, or AVG function is commonly used in the subquery.