UNION vs UNION ALL and INTERSECT vs EXCEPT SET Operators in SQL
In the interview UNION vs UNION ALL is the most frequent question. One more related question with this topic is INTERSECT vs EXCEPT as well. In this article, we will walk through all these questions along with the alternative options for them as well.
Please visit our Youtube Channel for more SQL Interview Questions and Answers videos by the below link:
Today's Interview Questions
Q01. What is the difference between UNION vs UNION ALL in SQL?
Q02. What is the difference between INTERSECT vs EXCEPT in SQL?
Q03. What is the difference between UNION vs JOIN in SQL?
Q04. What is the difference between EXCEPT vs MINUS in SQL?
Q05. What is the difference between UNION vs IN Operator in SQL?
Q06. What is the difference between INTERSECT vs IN Clause in SQL?
Q07. What is the difference between EXCEPT vs NOT IN Clause in SQL?
Q08. What is the difference between INTERSECT vs JOIN in SQL?
Q09. What is the difference between EXCEPT vs LEFT JOIN in SQL?
Q10. What are the different SET Operators in SQL?
Introduction
I have two tables, Employee and EmployeeArchive, which contain the employee records. Both the table have the same data type and structure for the columns. The EmployeeArchive has one additional column ArchivedOn and both the table has Id as Primary Key.
For this demonstration, I have kept both the table and data very short and simple. We will use these tables for all the demos here in this article.
UNION vs UNION ALL in SQL
Both UNION and UNION ALL are used to combine the result-set of two SELECT queries into a single result-set. One should have to follow the following rule while using UNION operations:
- The Number of Columns must be the same in both the SELECT statements.
- The Order of Columns must be the same in both the SELECT statements.
- The Type of Columns must be the same in both the SELECT statements.
UNION ALL SET Operator
UNION SET Operator
UNION vs JOIN
Live Demo
INTERSECT vs EXCEPT SET Operator
The SEMI JOINs are U-SQL’s way to filter a rowset based on the inclusion of its rows in another rowset. It is used by the SQL internally for the IN Operator. Semijoins are two types.
INTERSECT vs JOIN vs IN Clause
EXCEPT vs JOIN vs NOT IN Clause
Live Demo
SQL Scripts
-- Create Employee Table
CREATE TABLE Employee
(
[Id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[Name] [varchar](50) NOT NULL,
[Email] [varchar](50) NULL,
[MobileNumber] [nchar](15) NULL
);
-- Create EmployeeArchive Table
CREATE TABLE EmployeeArchive
(
[Id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[Name] [varchar](50) NOT NULL,
[Email] [varchar](50) NULL,
[MobileNumber] [nchar](15) NULL,
[ArchivedOn] [datetime] DEFAULT GETDATE(),
);
-- INSERT data into Employee Table
INSERT INTO Employee(Name, Email, MobileNumber)
VALUES ('Alex', 'alex@gmail.com', '5224578930'),
('Thomas', 'thomas@gmail.com', '5229848931'),
('Peter', 'peter@gmail.com', '5224578831'),
('Anton', 'anton@gmail.com', '5224579937'),
('Brien', 'brien@gmail.com', '5224571979');
-- INSERT data into EmployeeArchive Table
INSERT INTO EmployeeArchive(Name, Email, MobileNumber)
VALUES ('Alex', 'alex@gmail.com', '5224578930'),
('Peter', 'peter@gmail.com', '5224578831'),
('Maria', 'maria@gmail.com', '5224571976');
-- UNION SET OPERATOR
SELECT Name, Email, MobileNumber FROM Employee
UNION
SELECT Name, Email, MobileNumber FROM EmployeeArchive;
-- UNION ALL SET OPERATOR
SELECT Name, Email, MobileNumber FROM Employee
UNION ALL
SELECT Name, Email, MobileNumber FROM EmployeeArchive;
-- UNION SET OPERATOR
SELECT Name, Email, MobileNumber FROM Employee
UNION
SELECT Name, Email, MobileNumber FROM EmployeeArchive;
-- JOIN
SELECT E.Name, E.Email, E.MobileNumber,
EA.Name, EA.Email, EA.MobileNumber
FROM Employee E
FULL JOIN EmployeeArchive EA ON EA.Name = E.Name;
-- INTERSECT SET OPERATOR
SELECT Name, Email, MobileNumber FROM Employee
INTERSECT
SELECT Name, Email, MobileNumber FROM EmployeeArchive;
-- After Reversing Select Statement Order
SELECT Name, Email, MobileNumber FROM EmployeeArchive
INTERSECT
SELECT Name, Email, MobileNumber FROM Employee;
-- Alternatives for INTERSECT SET OPERATOR
SELECT Name, Email, MobileNumber FROM Employee
INTERSECT
SELECT Name, Email, MobileNumber FROM EmployeeArchive;
-- Using IN() Clause
SELECT Name, Email, MobileNumber FROM Employee
WHERE Name IN (SELECT Name FROM EmployeeArchive);
-- Using JOIN
SELECT E.Name, E.Email, E.MobileNumber FROM Employee E
JOIN EmployeeArchive EA ON EA.Name = E.Name;
-- EXCEPT SET OPERATOR
SELECT Name, Email, MobileNumber FROM Employee
EXCEPT
SELECT Name, Email, MobileNumber FROM EmployeeArchive;
-- After Reversing the Select Statement Order
SELECT Name, Email, MobileNumber FROM EmployeeArchive
EXCEPT
SELECT Name, Email, MobileNumber FROM Employee;
-- Alternatives for EXCEPT SET OPERATOR
SELECT Name, Email, MobileNumber FROM Employee
EXCEPT
SELECT Name, Email, MobileNumber FROM EmployeeArchive;
-- Using NOT IN() Clause
SELECT Name, Email, MobileNumber FROM Employee
WHERE Name NOT IN (SELECT Name FROM EmployeeArchive);
-- Using JOIN
SELECT E.Name, E.Email, E.MobileNumber FROM Employee E
LEFT JOIN EmployeeArchive EA ON EA.Name = E.Name
WHERE EA.Id IS NULL;
Recommended Articles
Most Frequently Asked SQL Interview Questions
Most Frequently Asked C# Interview Questions
Foreign Key Interview Questions
Unique Key Interview Questions
Primary Key Interview Questions
Derived Table (DT) Interview Questions
Unicode vs Non-Unicode Data Types Interview Questions
Table Variable (TV) Interview Questions
Table-Valued Type (TVT) Interview Questions
Common Table Expression(CTE) Interview Questions
Temp Table Interview Questions
Static Class Interview Questions and Answers
Sealed Class Interview Questions and Answers
Sealed Method Interview Questions and Answers
Abstract Class vs Interface
Interface Interview Questions and Answers
Thanks for visiting this page. Please follow us on Twitter, Facebook, LinkedIn, Telegram, WhatsApp, Youtube, and Quora for regular updates.
No comments:
Post a Comment
Please do not enter any HTML. JavaScript or spam link in the comment box.