PgSQL - Study Notes 17: Subquery

Table of contents

PostgreSQL subquery

Subquery usage in SELECT statement

Subquery usage in INSERT statement

Subquery usage in UPDATE statement

Subquery usage in DELETE statement


PostgreSQL subquery

  • Subquery, also known as inner query or nested query, refers to embedding query statements in the WHERE clause of a PostgreSQL query.
  • The query results of one SELECT statement can be used as the input value of another statement.
  • Subqueries can be used with SELECT, INSERT, UPDATE and DELETE statements and can use operators such as =, <, >, >=, <=, IN, BETWEEN, etc.

Here are a few rules that subqueries must follow:

  • Subqueries must be enclosed in parentheses.
  • A subquery can only have one column in the SELECT clause, unless there are multiple columns in the main query that are compared with the selected columns of the subquery.
  • ORDER BY cannot be used in subqueries, although ORDER BY can be used in the main query.
  • GROUP BY can be used in subqueries and has the same function as ORDER BY.
  • Subqueries return more than one row and can only be used with multivalued operators, such as the IN operator.
  • The BETWEEN operator cannot be used with subqueries, however, BETWEEN can be used within a subquery.

Subquery usage in SELECT statement

Subqueries are often used with SELECT statements. The basic syntax is as follows:

SELECT column_name [, column_name ]
FROM   table1 [, table2 ]
WHERE  column_name OPERATOR
      (SELECT column_name [, column_name ]
      FROM table1 [, table2 ]
      [WHERE])

Subquery usage in INSERT statement

Subqueries can also be used with INSERT statements. The INSERT statement inserts into another table using the data returned by the subquery.

The data selected in the subquery can be modified using any character, date, or numeric function. The basic syntax is as follows:

INSERT INTO table_name [ (column1 [, column2 ]) ]
   SELECT [ *|column1 [, column2 ] ]
   FROM table1 [, table2 ]
   [ WHERE VALUE OPERATOR ]

Subquery usage in UPDATE statement

Subqueries can be used in conjunction with UPDATE statements. When using a subquery through the UPDATE statement, single or multiple columns in the table are updated. The basic syntax is as follows:

UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
   (SELECT COLUMN_NAME
   FROM TABLE_NAME)
   [ WHERE) ]

Subquery usage in DELETE statement

Subqueries can be used in conjunction with the DELETE statement, just like the other statements mentioned above. The basic syntax is as follows:

DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
   (SELECT COLUMN_NAME
   FROM TABLE_NAME)
   [ WHERE) ]

Example:

mydb=# select * from company;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+-----------
  1 | Paul  |  32 | California                                         |  20000 |
  3 | Teddy |  23 | Norway                                             |  20000 |
  5 | David |  27 | South-Hall                                         |  85000 |
  8 | Paul  |  24 | Houston                                            |  20000 |
  9 | James |  44 | Norway                                             |   5000 |
 10 | James |  45 | Texas                                              |   5000 |
  6 | Kim   |  22 |                                                    |        |
  7 | James |  24 |                                                    |        |
(8 行记录)

/*1、在 SELECT 语句中使用子查询:*/
mydb=# SELECT * FROM COMPANY WHERE ID IN (SELECT ID FROM COMPANY  WHERE SALARY > 45000) ;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+-----------
  5 | David |  27 | South-Hall                                         |  85000 |
(1 行记录)

/*假设 COMPANY1 的结构与 COMPANY 表相似,且可使用相同的 CREATE TABLE 进行创建,只是表名改为 COMPANY1。*/
/*2、现在把整个 COMPANY 表复制到 COMPANY1,语法如下:*/
mydb=# INSERT INTO COMPANY1 SELECT * FROM COMPANY  WHERE ID IN (SELECT ID FROM COMPANY) ;
INSERT 0 8

/*假设,我们有 COMPANY1 表,是 COMPANY 表的备份。*/
/*2、下面的实例把 COMPANY 表中所有 AGE 大于 27 的客户的 SALARY 更新为原来的 0.50 倍:*/
mydb=# UPDATE COMPANY SET SALARY = SALARY * 0.50 WHERE AGE IN (SELECT AGE FROM COMPANY1 WHERE AGE >= 27 );
UPDATE 4
mydb=# select * from company1;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+-----------
  3 | Teddy |  23 | Norway                                             |  20000 |
  8 | Paul  |  24 | Houston                                            |  20000 |
  6 | Kim   |  22 |                                                    |        |
  7 | James |  24 |                                                    |        |
  1 | Paul  |  32 | California                                         |  10000 |
  5 | David |  27 | South-Hall                                         |  42500 |
  9 | James |  44 | Norway                                             |   2500 |
 10 | James |  45 | Texas                                              |   2500 |
(8 行记录)

/*假设,我们有 COMPANY_BKP 表,是 COMPANY 表的备份。/*
/*4、下面的实例删除 COMPANY 表中所有 AGE 大于或等于 27 的客户记录:*/
mydb=# DELETE FROM COMPANY WHERE AGE IN (SELECT AGE FROM COMPANY1 WHERE AGE > 27 );
DELETE 3
mydb=# select * from company;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+-----------
  5 | David |  27 | South-Hall                                         |  21250 |
  3 | Teddy |  23 | Norway                                             |  20000 |
  8 | Paul  |  24 | Houston                                            |  20000 |
  6 | Kim   |  22 |                                                    |        |
  7 | James |  24 |                                                    |        |
(5 行记录)

Guess you like

Origin blog.csdn.net/qq_41361442/article/details/124867527