Union Operators in MySQL and PostgreSQL đ
Joins and Union operators are used heavily in Structured Query Language (SQL) for querying the data in relational databases such MySQL and PostgreSQL.
In this article, I will discuss Union Operators such as UNION
, UNION ALL
, EXCEPT
, INTERSECT
.
đ Agenda
- Using
UNION
- Using
UNION ALL
- Using
EXCEPT
- Using
INTERSECT
Operator - Understanding Order of Evaluation
Letâs understand each of them.
đUsing UNION
UNION
is typically used to combine results from multiple tables (2 or more).
In MySQL and PostgreSQL, the UNION
keyword to combine the results of two or more SELECT
statements.
It is an intelligent operator as the output of the UNION
doesnât contain any Duplicate rows. It identifies duplicate rows and then removes them before returning the final output of the query.
Can UNION
be used anytime? NO.
UNION
can be used only if the following conditions are met in each SELECT
statements â
- Each
SELECT
statement should have the same number of columns - Each
SELECT
statement should have the same number of column expressions (same order and same applied functions such as aggregation) - Each column should have the same data type and should have the same order
Note: Column names donât have to be same.
UNION
adds more rows to existing rows. Thatâs why the column number, order, expression and data type must âmatchâ. It can also be used to combine two or more SELECT
statements querying the same table or different tables.
However, if one is combining results from a single table, it is better to write a single query using the appropriate
WHERE
clause orCASE
statement, etc.
Syntax of Join and Union
UNION
Syntax in MySQL (Image by Author)Other clauses such as WHERE
, JOIN
, etc.. can also be used with UNION
.
However, only one ORDER BY
clause is allowed for the whole query, and it should be at the very end.
What is the difference in Join and Union? The difference between a JOIN
and a UNION
is that JOIN
links together multiple tables within a single query, whereas UNION
stacks the results of multiple queries.
đUsing UNION ALL
The UNION ALL
operator is used to combine the results of two SELECT
statements including duplicate rows.
If you know with certainty that no duplicate rows are possible, use
UNION ALL
to improve performance as it is faster thanUNION
.
Why UNION
is slower? Because, UNION
does an additional sort behind the scenes to identify the duplicates while UNION ALL
doesnât perform this sorting in the backend.
Other clauses such as WHERE
, JOIN
, etc.. can also be used with UNION ALL.
However, only one ORDER BY
clause is allowed for the whole query, and it should be at the very end.
đUsing âEXCEPTâ
In addition to using a UNION
to combine the rows of multiple tables, one can use EXCEPT
and INTERSECT
to combine the rows in different ways.
Use EXCEPT
to âsubtractâ the results of one query from another query.
MySQL does not support
EXCEPT
. Instead, one can use theNOT IN
keywords as a workaround.PostgreSQL also supports
EXCEPT ALL
, which does not remove duplicates.EXCEPT
removes all occurrences of a value, whileEXCEPT ALL
removes specific instances.
đUsing INTERSECT
Use INTERSECT
to find the rows in common between two queries.
MySQL does not support INTERSECT
. Instead, you can use an INNER JOIN
as a workaround.
PostgreSQL also supports
INTERSECT ALL
, which preserves duplicate values.
đUnderstanding Order of Evaluation
Unless otherwise specified, union operators are performed in top-down order, except that
INTERSECT
takes precedence overUNION
andEXCEPT
.
When writing a statement with multiple union operators (UNION
, EXCEPT
, INTERSECT
), use parentheses to specify the order in which the operations should occur.
SELECT * FROM staffEXCEPT(SELECT * FROM residents
UNION
SELECT * FROM pets);
Thank you for reading this article.