-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-SQL-Set_Operator
More file actions
40 lines (31 loc) · 1.08 KB
/
13-SQL-Set_Operator
File metadata and controls
40 lines (31 loc) · 1.08 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
SET Operators in SQL
Set operators are used to combine results from two or more SELECT queries.
The two most common ones are:
1️⃣ UNION
UNION combines results and removes duplicates.
2️⃣ UNION ALL
UNION ALL combines results and keeps duplicates.
🔹UNION and UNION ALL
Union aand Union ALL will combine the results from two queries, but the difference is 'UNION' will not allow duplicates but 'UNION ALL' will allow duplicates.
Example:
-- find the customers from chennai and delhi
SELECT * FROM cust_dimen
WHERE city='Chennai'
UNION ALL
SELECT * FROM cust_dimen
WHERE city='Delhi';
-- Show all records from both tables, including matched and unmatched rows, by combining LEFT JOIN and RIGHT JOIN results.
cust_dimen cd LEFT JOIN market_fact_full mff
ON cd.cust_id=mff.cust_id
UNION
SELECT * FROM
cust_dimen cd RIGHT JOIN market_fact_full mff
ON cd.cust_id=mff.cust_id;
-- find customers from Nagpur and Mumbai
SELECT cust_id, city, customer_segment
FROM cust_dimen
WHERE city='Nagpur'
UNION ALL
SELECT cust_id, city, customer_name, customer_segment
FROM cust_dimen
WHERE city='Mumbai';