Updated on 2025-10-23 GMT+08:00

Compound Queries

A query that contains a compound operator is a compound query. All compound queries have the same priority. The number of columns and expressions in each query result in a set operation must be the same, and the types must be compatible.

UNION

Common set operations include:

  • UNION: a union set of the two query result sets where duplicates are removed.
  • UNION ALL: a union set of the two queries where the results of the two queries are simply combined.

UNION Examples

-- Create a table and insert data into the table.
m_db=# CREATE TABLE test1(c11 INT, c12 VARCHAR(20));
m_db=# INSERT INTO test1 VALUES (1,'a'),(2,'b'),(4,'d');

m_db=# CREATE TABLE test2(c21 INT, c22 VARCHAR(20)); 
m_db=# INSERT INTO test2 VALUES (1,'a'),(3,'c');
  • UNION
    m_db=# SELECT * FROM test1 UNION SELECT * FROM test2;
     c11 | c12 
    -----+-----
       4 | d
       3 | c
       1 | a
       2 | b
    (4 rows)
  • UNION ALL
    m_db=# SELECT * FROM test1 UNION ALL SELECT * FROM test2;
     c11 | c12 
    -----+-----
       1 | a
       2 | b
       4 | d
       1 | a
       3 | c
    (5 rows)

EXCEPT

Common intersection operations include:

  • EXCEPT: removes the intersection of the two query results from the first query result and deduplicates the results.
  • EXCEPT ALL:removes the intersection of the two query results from the first query result but does not deduplicate the results.
  • EXCEPT DISTINCT: removes the intersection of the two query results from the first query result and deduplicates the results.

EXCEPT Examples

-- Create a table and insert data into the table.
m_db=# CREATE TABLE a(m int,n int);
m_db=# INSERT INTO a(m,n) VALUES(1,2);
m_db=# INSERT INTO a(m,n) VALUES(2,3);
m_db=# INSERT INTO a(m,n) VALUES(3,4);

m_db=# CREATE TABLE b(m int,n int);
m_db=# INSERT INTO b(m,n) VALUES(1,2);
m_db=# INSERT INTO b(m,n) VALUES(1,3);
m_db=# INSERT INTO b(m,n) VALUES(3,4);

m_db=# CREATE TABLE c(m int,n int);
m_db=# INSERT INTO c(m,n) VALUES(1,3);
m_db=# INSERT INTO c(m,n) VALUES(1,3);
m_db=# INSERT INTO c(m,n) VALUES(3,4);
  • EXCEPT
    m_db=# SELECT * FROM a EXCEPT SELECT * FROM b;
     m | n
    ---+---
     2 | 3
    (1 row)
    
    m_db=# SELECT * FROM a EXCEPT SELECT * FROM c;
     m | n
    ---+---
     2 | 3
     1 | 2
    (2 rows)
    
    m_db=# SELECT * FROM b EXCEPT SELECT * FROM a;
     m | n
    ---+---
     1 | 3
    (1 row)
    -- Deduplicate the query results.
    m_db=# SELECT * FROM c EXCEPT SELECT * FROM a;
     m | n
    ---+---
     1 | 3
    (1 row)
  • EXCEPT ALL
    m_db=# SELECT * FROM a EXCEPT ALL SELECT * FROM b;
     m | n
    ---+---
     2 | 3
    (1 row)
    
    m_db=# SELECT * FROM a EXCEPT ALL SELECT * FROM c;
     m | n
    ---+---
     2 | 3
     1 | 2
    (2 rows)
    
    m_db=# SELECT * FROM b EXCEPT ALL SELECT * FROM a;
     m | n
    ---+---
     1 | 3
    (1 row)
    
    m_db=# SELECT * FROM c EXCEPT ALL SELECT * FROM a;
     m | n
    ---+---
     1 | 3
     1 | 3
    (2 rows)
  • EXCEPT DISTINCT
    m_db=# SELECT * FROM a EXCEPT DISTINCT SELECT * FROM b;
     m | n
    ---+---
     2 | 3
    (1 row)
    
    m_db=# SELECT * FROM a EXCEPT DISTINCT SELECT * FROM c;
     m | n
    ---+---
     2 | 3
     1 | 2
    (2 rows)
    
    m_db=# SELECT * FROM b EXCEPT DISTINCT SELECT * FROM a;
     m | n
    ---+---
     1 | 3
    (1 row)
    -- Deduplicate the query results.
    m_db=# SELECT * FROM c EXCEPT DISTINCT SELECT * FROM a;
     m | n
    ---+---
     1 | 3
    (1 row)
    
    m_db=# DROP TABLE a,b,c;