官术网_书友最值得收藏!

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:

  1. First, find the average salary from the employee table.
  2. Then, use the answer in the second SELECT statement to find employees who have a higher salary 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.

主站蜘蛛池模板: 张家界市| 巴彦县| 石狮市| 江津市| 六盘水市| 石泉县| 临漳县| 景宁| 嘉祥县| 仪陇县| 庆云县| 衡水市| 秦安县| 蒙自县| 景德镇市| 闸北区| 昭通市| 凤台县| 甘孜县| 朝阳市| 常德市| 巧家县| 清河县| 吉水县| 封开县| 山西省| 定兴县| 新平| 山西省| 东海县| 石城县| 辉南县| 黑龙江省| 通州市| 平和县| 阿拉善左旗| 江油市| 莎车县| 新闻| 五家渠市| 涪陵区|