-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSET Operators in SQL.sql
More file actions
43 lines (32 loc) · 1.62 KB
/
SET Operators in SQL.sql
File metadata and controls
43 lines (32 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- SET Operators
/*
Set operators are used to combine the results of two SELECT queries into a single result.
These operators are typically used when you need to merge or compare datasets.
The primary set operators in SQL are:
1. UNION
2. UNION ALL
3. INTERSECT
4. EXCEPT
*/
USE SQL_Server_DB
-- using Student and Teacher table from SQL_Joins file.
SELECT * FROM Student
SELECT * FROM Teacher
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--1. UNION: Combines the result set of two or more SELECT queries, but removes duplicate rows.
SELECT Student_Class FROM Student
UNION
SELECT Teacher_Class FROM Teacher
-- 2. UNION ALL: Combines the result set of two or more SELECT queries, includes all rows including duplicates.
SELECT Student_Class FROM Student
UNION ALL
SELECT Teacher_Class FROM Teacher
-- 3. INTERSECT: Returns only the rows that are common to both result sets.
SELECT Student_Class FROM Student
INTERSECT
SELECT Teacher_Class FROM Teacher
-- 4. EXCEPT: Return rows from the first SELECT query that are not present in the second SELECT query.
SELECT Student_Class FROM Student
EXCEPT
SELECT Teacher_Class FROM Teacher
-- ♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦