The UNION operator is used in SQL to combine the result sets of two or more SELECT statements.
It removes duplicate rows between the combined result sets by default.
SELECT column1, column2, ...
FROM table1
WHERE condition
UNION
SELECT column1, column2, ...
FROM table2
WHERE condition;- Each
SELECTstatement must have the same number of columns. - Columns must have compatible data types and appear in the same order.
SELECT first_name, last_name
FROM employees
WHERE department = 'Sales'
UNION
SELECT first_name, last_name
FROM employees
WHERE department = 'Marketing';- This query returns a combined list of employees from the Sales and Marketing departments, without duplicates.
UNIONremoves duplicate rows by default.- If you want to include duplicates, use
UNION ALL. - The column names in the final result come from the first
SELECTstatement.
SELECT city
FROM customers
UNION
SELECT city
FROM suppliers;- This query returns a distinct list of cities from both customers and suppliers.
Describe the problem, challenge, or topic discussed in a video related to SELECT FROM.
What concept was explained or what exercise was solved?
-- Write your SQL code attempt or solution related to SQL COMMAND
SQL COMMANDSQL COMMANDExplanation - Explain what you learned, any key takeaways, or how you solved the problem related to COMMAND._