-
Notifications
You must be signed in to change notification settings - Fork 0
/
unions.sql
50 lines (48 loc) · 835 Bytes
/
unions.sql
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
44
45
46
47
48
49
50
/*data tables taken from MYSQL workbench*/
/*unions*/
/*UNION combines two queries by rows */
SELECT
order_id,
order_date,
'Active' AS status
FROM orders
WHERE order_date >= '2019-01-01'
UNION
SELECT
order_id,
order_date,
'Archived' AS status
FROM orders
WHERE order_date <= '2019-01-01'
SELECT frist_name
FROM customers
UNION
SELECT name
FROM shippers
-- combines the first_name of both together
-- two tables must contain same number of columns
/*------------------------------------*/
SELECT
customer_id,
first_name,
points,
'Bronze' AS type
FROM customers
WHERE points < 2000
UNION
SELECT
customer_id,
first_name,
points,
'Silver' AS type
FROM customers
WHERE points BETWEEN 2000 AND 3000
UNION
SELECT
customer_id,
first_name,
points,
'Gold' AS type
FROM customers
WHERE points > 3000
ORDER BY first_name