PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
The UNION
operator in PostgreSQL is used to combine the result sets of two or more SELECT
statements. The combined result set will include all rows from both queries, but it will eliminate duplicate rows unless otherwise specified.
Here are some essential points and considerations about the UNION
operator in PostgreSQL:
Basic Usage:
SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2;
Requirements:
SELECT
statements must be the same.Eliminating Duplicates:
By default, the UNION
operator removes duplicates from the combined result set. If you want to keep all duplicates, use the UNION ALL
operator:
SELECT column1, column2 FROM table1 UNION ALL SELECT column1, column2 FROM table2;
Using UNION ALL
can be faster since the database doesn't have to search for and eliminate duplicates.
Ordering:
If you want to order the combined result set, you can add an ORDER BY
clause at the end:
SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2 ORDER BY column1;
Note: You cannot have an ORDER BY
clause for individual SELECT
statements within a UNION
, but you can have one for the entire combined result set.
Limiting Results:
You can use the LIMIT
and OFFSET
clauses after the UNION
to limit the combined result set:
SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2 ORDER BY column1 LIMIT 10 OFFSET 5;
Combining with Other Operators:
In addition to UNION
, PostgreSQL also supports other set operators like INTERSECT
(returns only the rows that appear in both result sets) and EXCEPT
(returns rows from the first query that aren't present in the second query).
Use Cases:
The UNION
operator is useful in scenarios where you want to retrieve related data from multiple tables and present it as a single result set. For instance, you might have separate tables for storing user details from different sources (e.g., users_from_source1
, users_from_source2
) but want a single list of all users.
Performance Consideration:
UNION
eliminates duplicates, it might involve additional processing. When you're sure that the result sets don't overlap or when duplicates are acceptable, UNION ALL
can offer better performance.In summary, the UNION
operator in PostgreSQL allows for combining result sets from multiple SELECT
queries into a single set, eliminating duplicates by default. Proper knowledge of its usage and nuances can help in creating efficient and effective queries.
PostgreSQL UNION operator example:
Use the UNION
operator to combine the results of two or more SELECT
statements:
SELECT column1 FROM table1 UNION SELECT column1 FROM table2;
How to use UNION operator in PostgreSQL:
The UNION
operator is used to combine the results of two or more SELECT
statements into a single result set.
Combine results of two queries in PostgreSQL:
Combine results using the UNION
operator:
SELECT column1 FROM table1 UNION SELECT column1 FROM table2;
PostgreSQL multiple SELECT statements with UNION:
Combine multiple SELECT
statements using UNION
:
SELECT column1 FROM table1 UNION SELECT column1 FROM table2 UNION SELECT column1 FROM table3;
Ordering results with UNION in PostgreSQL:
Order the combined result set using the ORDER BY
clause:
SELECT column1 FROM table1 UNION SELECT column1 FROM table2 ORDER BY column1;
Filtering data in UNION operator in PostgreSQL:
Use a WHERE
clause to filter data in each SELECT
statement:
SELECT column1 FROM table1 WHERE condition1 UNION SELECT column1 FROM table2 WHERE condition2;
Limiting rows in UNION in PostgreSQL:
Use LIMIT
to restrict the number of rows in the combined result set:
SELECT column1 FROM table1 UNION SELECT column1 FROM table2 LIMIT 10;
UNION operator and data types in PostgreSQL:
Ensure that the data types in corresponding columns of the SELECT
statements match for a successful UNION
.