WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

SQL Server · Basic · question 13 of 100

Can you describe the difference between UNION and UNION ALL?

📕 Buy this interview preparation book: 100 SQL Server questions & answers — PDF + EPUB for $5

In SQL Server, ‘UNION‘ and ‘UNION ALL‘ are used to combine data from two or more tables.

‘UNION‘ returns only distinct rows from the combined tables, whereas ‘UNION ALL‘ returns all rows, including duplicates, from the combined tables.

Here’s an example to illustrate the difference between the two:

Let’s say we have two tables, ‘employees‘ and ‘staff‘, with the following data:

employees table:
id   name    department
1    John    Sales
2    Jane    Marketing
3    Bob     IT

staff table:
id   name    department
1    John    Sales
3    Bob     IT
4    Sue     HR

If we execute the following ‘UNION‘ query:

SELECT id, name, department FROM employees
UNION
SELECT id, name, department FROM staff

The result would be:

id   name    department
1    John    Sales
2    Jane    Marketing
3    Bob     IT
4    Sue     HR

Notice that only distinct rows are returned, so John and Bob are not duplicated.

If we execute the following ‘UNION ALL‘ query:

SELECT id, name, department FROM employees
UNION ALL
SELECT id, name, department FROM staff

The result would be:

id   name    department
1    John    Sales
2    Jane    Marketing
3    Bob     IT
1    John    Sales
3    Bob     IT
4    Sue     HR

Notice that all rows are returned, including duplicates. In this case, John and Bob appear twice since they are in both tables.

In general, ‘UNION‘ is useful when you want to eliminate duplicates from the combined results, while ‘UNION ALL‘ is useful when you want to include duplicates. However, ‘UNION‘ requires more processing to remove duplicates, so it can be slower than ‘UNION ALL‘ for large datasets.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic SQL Server interview — then scores it.
📞 Practice SQL Server — free 15 min
📕 Buy this interview preparation book: 100 SQL Server questions & answers — PDF + EPUB for $5

All 100 SQL Server questions · All topics