PostgreSQL WITH Clause
In PostgreSQL, the WITH clause provides a way to write auxiliary statements for use in a larger query.
The WITH clause helps break down large, complex queries into simpler forms for readability. These statements are often called Common Table Expressions (Common Table Express, CTE), and can also be treated as a temporary table that exists for the query.
The WITH clause is especially useful when a subquery is executed multiple times, allowing us to reference it by name (possibly multiple times) within a query.
The WITH clause must be defined before it is used.
Syntax
The basic syntax of a WITH query is as follows:
WITH
name_for_summary_data AS (
SELECT Statement)
SELECT columns
FROM name_for_summary_data
WHERE conditions <=> (
SELECT column
FROM name_for_summary_data)
[ORDER BY columns]
name_for_summary_data is the name of the WITH clause,name_for_summary_data can be the same as an existing table name, and takes precedence.
You can use data INSERT, UPDATE, or DELETE statements in WITH, allowing you to perform several different operations in the same query.
WITH recursion
In a WITH clause, you can use the data output by the query itself.
A Common Table Expression (CTE) has an important advantage: it can reference itself, thereby creating a recursive CTE. A recursive CTE is a common table expression that repeatedly executes the initial CTE to return a subset of data until the complete result set is obtained.
Example
Create the COMPANY table (Download the COMPANY SQL file ), with the following data:
runoobdb# 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)
The following will use a WITH clause to query data from the table above:
With CTE AS
(Select
ID
, NAME
, AGE
, ADDRESS
, SALARY
FROM COMPANY )
Select * From CTE;
The result is as follows:
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)
Next, let's use the RECURSIVE keyword with a WITH clause to write a query that finds SALARY (salary) field data less than 20000 and calculates their sum:
WITH RECURSIVE t(n) AS (
VALUES (0)
UNION ALL
SELECT SALARY FROM COMPANY WHERE SALARY < 20000
)
SELECT sum(n) FROM t;
The result is as follows:
sum
-------
25000
(1 row)
Next, we create a COMPANY1 table similar to the COMPANY table, use a DELETE statement with a WITH clause to delete data from COMPANY where SALARY (salary) field is greater than or equal to 30000, and insert the deleted data into COMPANY1, thereby transferring COMPANY table data into COMPANY1:
CREATE TABLE COMPANY1(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
WITH moved_rows AS (
DELETE FROM COMPANY
WHERE
SALARY >= 30000
RETURNING *
)
INSERT INTO COMPANY1 (SELECT * FROM moved_rows);
The result is as follows:
INSERT 0 3
At this point, the data in the CAMPANY table and the CAMPANY1 table is as follows:
runoobdb=# SELECT * FROM COMPANY;
id | name | age | address | salary
----+-------+-----+------------+--------
1 | Paul | 32 | California | 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
7 | James | 24 | Houston | 10000
(4 rows)
runoobdb=# SELECT * FROM COMPANY1;
id | name | age | address | salary
----+-------+-----+-------------+--------
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall | 45000
(3 rows)