Set Operations

tags: #sql

What are set operations in SQL?

Set operations are used in SQL to combine or compare the results of two or more SELECT statements.

SELECT column1, column2, ...
FROM table1
<set operation>
SELECT column1, column2, ...
FROM table2

Types of set operations

  1. UNION: Combines the result sets of two or more SELECT statements into a single result set, eliminating any duplicate rows.
SELECT name, salary FROM employees WHERE <condition>...
UNION
SELECT name, salary FROM contractors WHERE <condition>...;
  1. UNION ALL: Similar to UNION, but it does not eliminate duplicate rows from the result set.

  2. INTERSECT: Returns only the rows that appear in both result sets of two or more SELECT statements.

  3. EXCEPT: Returns only the rows that appear in the first result set, but not in the second result set of two SELECT statements.
Powered by Forestry.md