What is the difference between UNION
and UNION ALL
in SQL?
adminnewd Changed status to publish September 19, 2024
UNION
: Combines the results of two or more SELECT queries and removes duplicate rows from the result set. It ensures that the output contains only unique rows. If the result sets have duplicate rows, only one of each will be included in the final result.UNION ALL
: Combines the results of two or more SELECT queries but includes all rows, including duplicates. This can be useful when you need to retain duplicate rows in the result set.
Example:-
— Using UNION
SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;
— Using UNION ALL
SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2;
anonymous Answered question September 19, 2024