Skip to content

Commit fda9e04

Browse files
committedApr 5, 2020
add retrieve data JOINS, Union
1 parent 0b85854 commit fda9e04

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/******************************************/
2+
/* JOINS */
3+
/******************************************/
4+
5+
SELECT oi.order_id,p.name,oi.quantity,oi.unit_price
6+
FROM order_items oi
7+
JOIN products p ON p.product_id = oi.product_id;
8+
9+
10+
/******************************************/
11+
/* SELF JOINS */
12+
/******************************************/
13+
USE mosh_sql_hr;
14+
15+
SELECT * FROM employees
16+
LIMIT 5;
17+
18+
/*employee and his/her manager */
19+
SELECT e1.employee_id, e1.first_name, e1.last_name,
20+
COALESCE(CONCAT(e2.first_name,e2.last_name), 'Top Manager') AS manager
21+
FROM employees e1
22+
LEFT JOIN employees e2 ON e1.reports_to = e2.employee_id;
23+
24+
25+
/******************************************/
26+
/* Joining Multiple Tables */
27+
/******************************************/
28+
USE mosh_sql_store;
29+
30+
SELECT o.order_id, o.order_date, c.first_name, c.last_name, os.name
31+
FROM orders o
32+
JOIN customers c ON c.customer_id = o.customer_id
33+
JOIN order_statuses os ON os.order_status_id = o.status;
34+
35+
/*payment and customer details*/
36+
USE mosh_sql_invoicing;
37+
38+
SELECT c.client_id, c.name,p.invoice_id,p.date,p.amount,pm.name
39+
FROM payments p
40+
JOIN payment_methods pm ON pm.payment_method_id = p.payment_method
41+
JOIN clients c ON c.client_id = p.client_id;
42+
43+
44+
/******************************************/
45+
/* Implicit Join */
46+
/******************************************/
47+
48+
SELECT c.client_id, c.name,p.invoice_id,p.date,p.amount,pm.name
49+
FROM payments p, payment_methods pm, clients c
50+
WHERE pm.payment_method_id = p.payment_method
51+
AND c.client_id = p.client_id;
52+
53+
/******************************************/
54+
/* Outer Joins */
55+
/******************************************/
56+
USE mosh_sql_store;
57+
58+
SELECT c.customer_id, c.first_name,c.last_name,o.order_id
59+
FROM customers c
60+
LEFT JOIN orders o ON o.customer_id = c.customer_id
61+
ORDER BY 1;
62+
63+
/*products and how many time it has been ordered*/
64+
SELECT p.product_id, p.name, oi.quantity
65+
FROM products p
66+
LEFT JOIN order_items oi ON oi.product_id = p.product_id;
67+
68+
/* product which has never been ordered */
69+
SELECT p.product_id, p.name, oi.quantity
70+
FROM products p
71+
LEFT JOIN order_items oi ON oi.product_id = p.product_id
72+
WHERE oi.product_id IS NULL;
73+
74+
/* order, customer name, status where status is in processed or shipped */
75+
SELECT o.order_date, o.order_id, c.first_name, s.name, os.name
76+
FROM orders o
77+
JOIN customers c ON o.customer_id = c.customer_id
78+
LEFT JOIN shippers s ON s.shipper_id = o.shipper_id
79+
LEFT JOIN order_statuses os ON os.order_status_id = o.status;
80+
81+
/******************************************/
82+
/* USING */
83+
/******************************************/
84+
SELECT *
85+
FROM order_items oi
86+
JOIN order_item_notes oin
87+
USING(order_id,product_id);
88+
89+
90+
/*client and payment methods */
91+
USE mosh_sql_invoicing;
92+
93+
SELECT p.date, c.name, p.amount, pm.name
94+
FROM payments p
95+
JOIN clients c USING (client_id)
96+
JOIN payment_methods pm ON pm.payment_method_id = p.payment_method;
97+
98+
99+
/******************************************************************/
100+
/* Natural Joins */
101+
/* it allows sql engine to pick up the keys by itself for joining */
102+
/******************************************************************/
103+
USE mosh_sql_store;
104+
105+
SELECT o.order_id, c.first_name
106+
FROM orders o
107+
NATURAL JOIN customers c;
108+
109+
110+
/******************************************************************/
111+
/* CORSS JOIN */
112+
/******************************************************************/
113+
/* cross join between shippers and products using implicit and explict*/
114+
SELECT s.name, p.name
115+
FROM shippers s , products p;
116+
117+
SELECT s.name, p.name
118+
FROM shippers s
119+
CROSS JOIN products p;
120+
121+
122+
/******************************************************************/
123+
/* UNION */
124+
/******************************************************************/
125+
126+
SELECT *, 'Active' AS status
127+
FROM orders
128+
WHERE order_date >= CURDATE() - INTERVAL 1 YEAR
129+
UNION
130+
SELECT *, 'Archived' AS status
131+
FROM orders
132+
WHERE order_date < CURDATE() - INTERVAL 1 YEAR;
133+
134+
135+
/* customer and point status */
136+
SELECT customer_id, first_name, points,
137+
CASE
138+
WHEN points > 3000 THEN 'Gold'
139+
WHEN points >= 2000 AND points <= 3000 THEN 'Silver'
140+
ELSE 'Bronze'
141+
END AS type
142+
FROM customers
143+
ORDER BY 2;

0 commit comments

Comments
 (0)
Please sign in to comment.