Using subqueries
A subquery is a query within a query. In other words, a subquery is a SQL query nested inside a larger query. It may occur in a SELECT
, FROM
, or WHERE
clause. In PostgreSQL, a subquery can be nested inside a SELECT
, INSERT
, UPDATE
, DELETE
, SET
, or DO
statement or inside another subquery. It is usually added within the WHERE
clause of another SQL SELECT
statement. You can use comparison operators, such as >
, <
, or =
. Comparison operators can also be a multiple-row operator, such as IN
, ANY
, SOME
, or ALL
. It can be treated as an inner query that is an SQL query placed as a part of another query called as outer query. The inner query is executed before its parent query so that the results of the inner query can be passed to the outer query.
The following statement illustrates the subquery syntax:
SELECT column list FROM table WHERE table.columnname expr_operator (SELECT column FROM table)
The query inside the brackets is called the inner query. The query that contains the subquery is called the outer query.
PostgreSQL executes the query that contains a subquery in the following sequence:
- First, it executes the subquery
- Second, it gets the results and passes it to the outer query
- Third, it executes the outer query
Let's consider an example where you want to find employee_id
, first_name
, last_name
, and salary
for employees whose salary is higher than the average salary throughout the company.
We can do this in two steps:
- First, find the average salary from the
employee
table. - Then, use the answer in the second
SELECT
statement to find employees who have a highersalary
from the result (which is the average salary).
SELECT avg(salary) from employee; Result: 25000 SELECT employee_id,first_name,last_name,salary FROM employee WHERE salary > 25000;
This does seem rather inelegant. What we really want to do is pass the result of the first query straight into the second query without needing to remember it, and type it back for a second query.
The solution is to use a subquery. We put the first query in brackets, and use it as part of
a WHERE
clause to the second query, as follows:
SELECT employee_id,first_name,last_name,salary FROM employee WHERE salary > (Select avg(salary) from employee);
PostgreSQL runs the query in brackets first, that is, the average of salary. After getting the answer, it then runs the outer query, substituting the answer from the inner query, and tries to find the employees whose salary
is higher than the average.
Note
Note: A subquery that returns exactly one column value from one row is called a scalar subquery. The SELECT
query is executed and the single returned value is used in the surrounding value expression. It is an error to use a query that returns more than one row or column as a scalar subquery. If the subquery returns no rows during a particular execution, it is not an error, and the scalar result is taken to be null. The subquery can refer to variables from the surrounding query, which will act as constants during any one evaluation of the subquery.