Subqueries

Course- PostgreSQL >

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

What is a subquery in PostgreSQL?

In PostgreSQL, 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 PostgreSQL, a subquery is also called an INNER QUERY or INNER SELECT.
  • In PostgreSQL, 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 p.product_id, p.product_name

FROM products p

WHERE p.category_id IN

   (SELECT c.category_id

    FROM categories c

    WHERE c.category_id > 25

    AND c.category_name like 'S%');

The subquery portion of the SELECT statement above is:

(SELECT c.category_id

 FROM categories c

 WHERE c.category_id > 25

 AND c.category_name like 'S%');

This subquery allows you to find all category_id values from the categories table that have a category_id greater than 25 and the category_name starts with 'S'. 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 p.product_id, p.product_name

FROM products p

INNER JOIN categories c

ON p.category_id = c.category_id

WHERE c.category_id > 25

AND c.category_name like 'S%';

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 products.product_name, subquery1.category_name

FROM products,

 (SELECT categories.category_id, categories.category_name, COUNT(category_id) AS total

  FROM categories

  GROUP BY categories.category_id, categories.category_name) subquery1

WHERE subquery1.category_id = products.category_id;

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

(SELECT categories.category_id, categories.category_name, COUNT(category_id) AS total

 FROM categories

 GROUP BY categories.category_id, categories.category_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.product_name,

  (SELECT MAX(product_id)

   FROM products p2

   WHERE p1.product_id = p2.product_id) subquery2

FROM products p1;

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

(SELECT MAX(product_id)

 FROM products p2

 WHERE p1.product_id = p2.product_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.