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
WHERE clause. In PostgreSQL, a subquery can be nested inside a
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
=. Comparison operators can also be a multiple-row operator, such as
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
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
Then, use the answer in the second
SELECTstatement to find employees who have a higher
salaryfrom 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
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: 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.