Easy Tutorial
❮ Postgresql Group By Postgresql Having ❯

PostgreSQL Subquery

A subquery, also known as an inner query or nested query, refers to embedding a query within the WHERE clause of a PostgreSQL query.

The result of a SELECT statement can serve as an input value for another statement.

Subqueries can be used with SELECT, INSERT, UPDATE, and DELETE statements, along with operators such as =, <, >, >=, <=, IN, BETWEEN, etc.

Here are several rules that subqueries must follow:

Subquery Usage in SELECT Statements

Subqueries are commonly 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])

Example

Create a COMPANY table (download COMPANY SQL file), with the following data content:

tutorialprodb# select * from COMPANY;
 id | name  | age | address   | salary
----+-------+-----+-----------+--------
  1 | Paul  |  32 | California|  20000
  2 | Allen |  25 | Texas     |  15000
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
  6 | Kim   |  22 | South-Hall|  45000
  7 | James |  24 | Houston   |  10000
(7 rows)

Now, let's use a subquery in a SELECT statement:

tutorialprodb=# SELECT * FROM COMPANY WHERE ID IN (SELECT ID FROM COMPANY  WHERE SALARY > 45000) ;

The result is as follows:

id | name  | age |  address    | salary
----+-------+-----+-------------+--------
  4 | Mark  |  25 | Rich-Mond   |  65000
  5 | David |  27 | Texas       |  85000
(2 rows)

Subquery Usage in INSERT Statements

Subqueries can also be used with INSERT statements. The INSERT statement uses data returned by the subquery to insert into another table.

The data selected in the subquery can be modified by any character, date, or number functions.

The basic syntax is as follows:

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

Example

Assume COMPANY_BKP has the same structure as the COMPANY table and can be created using the same CREATE TABLE, just with the table name changed to COMPANY_BKP. Now, copy the entire COMPANY table to COMPANY_BKP with the following syntax:

tutorialprodb=# INSERT INTO COMPANY_BKP SELECT * FROM COMPANY  WHERE ID IN (SELECT ID FROM COMPANY) ;

Subquery Usage in UPDATE Statements

Subqueries can be combined with UPDATE statements. When using a subquery with an UPDATE statement, single or multiple columns in the table are updated.

The basic syntax is as follows:

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

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

Example

Suppose we have a COMPANY_BKP table, which is a backup of the COMPANY table.

The following example updates the SALARY of all customers in the COMPANY table whose AGE is greater than 27 to 50% of their original salary:

tutorialprodb=# UPDATE COMPANY SET SALARY = SALARY * 0.50 WHERE AGE IN (SELECT AGE FROM COMPANY_BKP WHERE AGE >= 27 );

This will affect two rows, and the final records in the COMPANY table will be as follows:

id | name  | age | address     | salary
----+-------+-----+-------------+--------
  2 | Allen |  25 | Texas       |  15000
  3 | Teddy |  23 | Norway      |  20000
  4 | Mark  |  25 | Rich-Mond   |  65000
  6 | Kim   |  22 | South-Hall  |  45000
  7 | James |  24 | Houston     |  10000
  1 | Paul  |  32 | California  |  10000
  5 | David |  27 | Texas       |  42500
(7 rows)

Using Subqueries in DELETE Statements

Subqueries can be used with DELETE statements, 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

Suppose we have a COMPANY_BKP table, which is a backup of the COMPANY table.

The following example deletes all customer records from the COMPANY table where AGE is greater than or equal to 27:

tutorialprodb=# DELETE FROM COMPANY WHERE AGE IN (SELECT AGE FROM COMPANY_BKP WHERE AGE > 27 );

This will affect two rows, and the final records in the COMPANY table will be as follows:

id | name  | age | address     | salary
----+-------+-----+-------------+--------
  2 | Allen |  25 | Texas       |  15000
  3 | Teddy |  23 | Norway      |  20000
  4 | Mark  |  25 | Rich-Mond   |  65000
  6 | Kim   |  22 | South-Hall  |  45000
  7 | James |  24 | Houston     |  10000
  5 | David |  27 | Texas       |  42500
(6 rows)
❮ Postgresql Group By Postgresql Having ❯