10 SQL Union Interview Questions and Answers
Prepare for your SQL interview with our guide on SQL Union. Enhance your data manipulation skills and tackle complex queries confidently.
Prepare for your SQL interview with our guide on SQL Union. Enhance your data manipulation skills and tackle complex queries confidently.
SQL Union is a powerful tool in database management, allowing users to combine the results of two or more SELECT queries into a single result set. This operation is essential for efficiently handling and analyzing large datasets, making it a critical skill for anyone working with relational databases. Mastery of SQL Union can significantly enhance your ability to perform complex data manipulations and generate comprehensive reports.
This article offers a curated selection of SQL Union interview questions designed to test and improve your understanding of this key concept. By working through these questions, you will gain deeper insights into the practical applications of SQL Union, preparing you to tackle real-world database challenges with confidence.
The SQL UNION operator combines results from multiple SELECT statements into a single result set. Each SELECT must have the same number of columns with similar data types, in the same order.
Example:
SELECT column1, column2, column3 FROM table1 UNION SELECT column1, column2, column3 FROM table2;
This example combines results from table1
and table2
, removing duplicate rows by default. To include duplicates, use UNION ALL:
SELECT column1, column2, column3 FROM table1 UNION ALL SELECT column1, column2, column3 FROM table2;
UNION and UNION ALL combine results from multiple SELECT statements. The key difference is that UNION removes duplicates, while UNION ALL includes all rows, including duplicates. Use UNION for unique results and UNION ALL for faster performance when duplicates are acceptable.
Example:
SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2;
This removes duplicates.
SELECT column1, column2 FROM table1 UNION ALL SELECT column1, column2 FROM table2;
This includes duplicates.
To combine results from three tables without duplicates:
SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2 UNION SELECT column1, column2 FROM table3;
This ensures no duplicate rows in the final result.
The ORDER BY clause sorts the final result set of a UNION operation. It cannot be used within individual SELECT statements. Place it at the end of the UNION to sort the combined results.
Example:
SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2 ORDER BY column1;
This sorts the combined result by column1
.
To include a calculated column in a UNION result:
SELECT column1, column2, (column1 + column2) AS calculated_column FROM table1 UNION SELECT column1, column2, (column1 + column2) AS calculated_column FROM table2;
This adds a calculated column to the combined results.
For a complex query using UNION with nested subqueries and aggregate functions:
SELECT employee_id, employee_name, total_sales FROM ( SELECT e.employee_id, e.employee_name, SUM(s.sales_amount) AS total_sales FROM employees e JOIN sales s ON e.employee_id = s.employee_id GROUP BY e.employee_id, e.employee_name ) AS employee_sales UNION SELECT manager_id AS employee_id, manager_name AS employee_name, total_sales FROM ( SELECT m.manager_id, m.manager_name, SUM(s.sales_amount) AS total_sales FROM managers m JOIN sales s ON m.manager_id = s.manager_id GROUP BY m.manager_id, m.manager_name ) AS manager_sales;
This combines employee and manager sales data.
To ensure compatibility with different data types in a UNION:
SELECT column1, CAST(column2 AS VARCHAR) AS column2 FROM table1 UNION SELECT column1, column2 FROM table2;
This casts column2
to match data types.
To combine results using UNION with JOIN operations:
SELECT a.id, a.name, b.value FROM table1 a JOIN table2 b ON a.id = b.id UNION SELECT c.id, c.name, d.value FROM table3 c JOIN table4 d ON c.id = d.id;
This combines results from joined tables.
Using UNION with subqueries:
SELECT employee_id, employee_name, department FROM ( SELECT employee_id, employee_name, department FROM employees WHERE department = 'Sales' ) AS sales_dept UNION SELECT employee_id, employee_name, department FROM ( SELECT employee_id, employee_name, department FROM employees WHERE department = 'Marketing' ) AS marketing_dept;
This combines results from subqueries for different departments.
The execution plan of a UNION query affects performance due to sorting and merging operations. UNION ALL is generally faster as it skips duplicate removal. To analyze and optimize, use: