UNION and UNION ALL

Purpose: Combines the results of two or more SELECT statements. UNION removes duplicate rows, while UNION ALL retains all rows, including duplicates.

Syntax:

SELECT column1 FROM table1
UNION
SELECT column1 FROM table2;

Example:

SELECT first_name FROM employees
UNION
SELECT first_name FROM managers;

Explanation: This retrieves unique first names from both the employees and managers tables. To include duplicates, use UNION ALL.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *