The UNION operator in SQL is used to combine the result sets of two or more SELECT statements. It removes duplicate rows between the various SELECT statements. Here are some tricky interview questions that test your understanding of the UNION operator:
1. Basic Understanding:
- Question: What is the difference between
UNIONandUNION ALL? - Answer:
UNIONremoves duplicate rows from the combined result set, whereasUNION ALLincludes all rows, even duplicates.
2. Column Order and Type:
- Question: What happens if the columns in the
SELECTstatements used in aUNIONquery are not in the same order? - Answer: The columns must be in the same order for all
SELECTstatements in aUNION. If they are not, SQL will throw a syntax error or produce incorrect results because the database engine matches columns by position, not by name.
3. Different Data Types:
- Question: Can you use
UNIONto combine result sets where the corresponding columns have different data types? - Answer: The corresponding columns in the
SELECTstatements must have compatible data types. For example, you can combine anINTwith aFLOAT, but not anINTwith aVARCHARunless an explicit conversion is done.
4. Order of Execution:
- Question: How does SQL handle the
ORDER BYclause in aUNIONquery? - Answer: The
ORDER BYclause applies to the entire result set, not to individualSELECTstatements. If you want to order the combined results, you must place theORDER BYat the end of the lastSELECTstatement in theUNIONquery.
5. Performance Considerations:
- Question: Given two large tables, what are some performance considerations when using
UNIONvs.UNION ALL? - Answer:
UNIONhas a performance overhead because it eliminates duplicates, which requires sorting or a hashing operation.UNION ALLis faster because it does not perform duplicate elimination.
6. Using UNION with NULLs:
- Question: What happens when
NULLvalues are present in the columns being combined with aUNION? - Answer:
UNIONtreatsNULLvalues as equivalent when removing duplicates, so if two rows differ only by havingNULLvalues in some columns, one of those rows will be removed from the final result set.
7. Complex Query with WHERE Clauses:
- Question: Consider two tables,
EmployeesandContractors, both having columnsID,Name, andDepartment. Write aUNIONquery that retrieves allEmployeesfrom theITdepartment and allContractorsfrom theHRdepartment. - Answer:
SELECT ID, Name, Department FROM Employees WHERE Department = 'IT'
UNION
SELECT ID, Name, Department FROM Contractors WHERE Department = 'HR';
8. Subqueries with UNION:
- Question: How would you use a
UNIONin conjunction with a subquery to retrieve the top 5 highest-paid employees and the top 5 highest-paid contractors from two different tables? - Answer:
(SELECT TOP 5 ID, Name, Salary FROM Employees ORDER BY Salary DESC)
UNION
(SELECT TOP 5 ID, Name, Salary FROM Contractors ORDER BY Salary DESC);
9. Handling Inconsistent Data Across Tables:
- Question: Suppose you have two tables with inconsistent data (e.g., one table uses uppercase names, and the other uses lowercase). How can you write a
UNIONquery that treats names case-insensitively? - Answer:
SELECT LOWER(Name) AS Name FROM Table1
UNION
SELECT LOWER(Name) AS Name FROM Table2;
10. Detecting and Handling Inconsistencies:
- Question: How would you modify a
UNIONquery to detect and flag inconsistencies in corresponding columns from the two tables being combined? - Answer:
SELECT Name, 'Source: Table1' AS Source FROM Table1
UNION
SELECT Name, 'Source: Table2' AS Source FROM Table2
WHERE Name NOT IN (SELECT Name FROM Table1);
This query flags names present in Table2 but not in Table1, indicating a possible inconsistency.
No comments:
Post a Comment