Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Set Operators

Group Members

• Zhair Ahmad (Leader ) - SP22-BCS-125


• Kashif Shahzad - SP22-BCS-134
• Mohsin Haider - SP22-BCS-136
• Muhammad Hussian - SP22-BCS-138
Introduction
Set Operators
• The SET operators in SQL combine and compare
the results of two or more SELECT statement
queries into a single result.
• Instead of combining columns as in SQL joins,
they function by merging rows from two or more
tables.
• Mathematical operations and set operators are
similar.
SET Operators vs JOIN (continued...)

JOIN Set Operators


SET Operators vs JOIN (continued...)

JOIN Set Operators

• Combine rows from two or more tables based on a • Combine results of two or more SELECT queries.
related column.

• Require a relationship between tables, usually defined by • Do not require relationships; can combine unrelated
a foreign key. datasets.

• Can be more performance-intensive due to the need to • Can be more straightforward but may involve sorting and
match rows based on keys. removing duplicates, affecting performance.
Type Of Set Operators

Union Union all Intersect Minus/ Except


DATA SETS
<query1> set operator <query2>

Table1: Movies Table2: Books


Union Set Output:

Work
Operator
• The UNION set operator in SQL is used when you need to
combine the results obtained from two or more SELECT
statements.
• Important: UNION statements only return UNIQUE
values.

SELECT * FROM Movies


UNION
SELECT * FROM Books;
NOTE

Note that "Harry Potter" – an item which appears in both tables – is shown only once in the results.
Like we mentioned earlier, the UNION set operator does not return duplicate values.
Union All Set Output:

Work
Operator
• UNION ALL is very similar to UNION, but with one
exception: UNION ALL returns all data from all tables, no
matter if it is a duplicate or not.
• Let's do the same operation as in the UNION example and
see what we get:

SELECT * FROM Books


UNION ALL
SELECT * FROM Movies;
But when should we use UNION and when UNION
ALL?
Firstly, know that there is a huge difference in efficiency between them.

Union Union All


• Use when unique results are needed. • Use when all results are needed.
• Slower due to duplicate removal. • Faster due to no duplicate removal.

e.g: You are creating a report that requires e.g: You are consolidating transactions where
unique records from multiple tables. duplicates are meaningful and should be
preserved.
Except Set Operator
Work

• The EXCEPT operator in SQL combines the result sets of Output:


two SELECT statements and returns only the rows
appearing in the first result set but not in the second.
• EXCEPT filters out the rows present in the second result
set from the first result set.

SELECT * FROM BOOKS


EXCEPT
SELECT * FROM MOVIES;
NOTE

Now "Harry Potter" doesn't appear in the results table; it's the title of a book and a movie. Thanks to
the MINUS set operator we are able to see only those titles that occur in the first table and are not
present in the second.

By the way, some databases use the keyword MINUS instead of EXCEPT. Don't worry – the
function and results are exactly the same.
INTERSET Set
Work
Operator
• The INTERSECT operator in SQL is used to combine the Output:
result sets of two SELECT statements and returns only the
rows that appear in both result sets.

SELECT * FROM BOOKS


INTERSECT
SELECT * FROM MOVIES;
Guidelines For Set Operators

• The SELECT lists in both queries must have the same number of columns.
• The data types of each column in the second query must match the data type of
the corresponding column in the first query.
• The ORDER BY clause can only appear at the very end of the statement, not
before a set operator or within individual SELECT statement
ORDER BY Clause
Work

• Sorting: The ORDER BY clause sorts the entire result set Output:
produced by the combined query.
• Consistency: Ensure the columns being combined have the
same data type and are named consistently across queries.

SELECT Title FROM Movies


UNION
SELECT Title FROM Books
ORDER BY Title;
Thank You
F o r Yo u r A t t e n t i o n

You might also like