The UNION ALL keyword in SQL is used to combine the results of two or more SELECT statements. Unlike UNION, which removes duplicate rows, UNION ALL includes all rows from each SELECT statement, even if they are duplicates.
Syntax:
SELECT column1, column2, ...
FROM table1
WHERE condition
UNION ALL
SELECT column1, column2, ...
FROM table2
WHERE condition;
Key Points:
- Combines Results:
UNION ALLcombines the result sets from two or moreSELECTqueries. - Includes Duplicates: It includes all records, even if some of them are duplicated across the different
SELECTqueries. - Column Matching: The number and data types of the columns in each
SELECTstatement must match. - Performance:
UNION ALLis generally faster thanUNIONbecause it does not have to remove duplicates.
Example:
SELECT name FROM employees
WHERE department = 'Sales'
UNION ALL
SELECT name FROM employees
WHERE department = 'Marketing';
This query combines the list of employees from the 'Sales' and 'Marketing' departments, including any duplicate names that might appear in both departments.
The UNION ALL keyword in SQL is used to combine the results of two or more SELECT queries. Unlike the UNION keyword, which eliminates duplicate rows from the result set, UNION ALL includes all rows, even duplicates.
Here is a basic example:
SELECT column1, column2 FROM table1
UNION ALL
SELECT column1, column2 FROM table2;
This will return all rows from both table1 and table2, including any duplicates that may exist between the two tables.
No comments:
Post a Comment