-
Notifications
You must be signed in to change notification settings - Fork 551
/
Copy path06.Writing Complex Query.sql
190 lines (154 loc) · 4.49 KB
/
06.Writing Complex Query.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*************************************/
/* Subqueries */
/**************************************/
/* products that are more expensive than lettuce */
USE mosh_sql_store;
SELECT *
FROM products
WHERE unit_price > (
SELECT unit_price FROM products
WHERE name REGEXP('lettuce')
);
/* find employees who earn more than average */
USE mosh_sql_hr;
SELECT *
FROM employees
WHERE salary > (
SELECT AVG(salary) FROM employees
);
/*************************************/
/* IN */
/**************************************/
USE mosh_sql_store;
/* products that have never been ordered*/
SELECT p.product_id, p.name, oi.order_id
FROM products p
LEFT JOIN order_items oi ON oi.product_id = p.product_id
WHERE oi.product_id IS NULL;
SELECT p.product_id, p.name
FROM products p
WHERE p.product_id NOT IN(
SELECT DISTINCT product_id
FROM order_items
);
/* find clients without invoices */
USE mosh_sql_invoicing;
SELECT *
FROM clients
WHERE client_id NOT IN (
SELECT DISTINCT client_id
FROM invoices
);
/*************************************/
/* Subqueries Vs Joins */
/**************************************/
/*customers who have ordered lettuce*/
SELECT DISTINCT c.customer_id, c.first_name, c.last_name
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
JOIN order_items oi ON oi.order_id = o.order_id
JOIN products p ON p.product_id = oi.product_id
WHERE p.name LIKE '%lettuce%';
/*************************************/
/* ALL keyword */
/*************************************/
/*invoices larger than invoices of client 3*/
USE mosh_sql_invoicing;
SELECT *
FROM invoices
WHERE invoice_total > ALL (
SELECT invoice_total FROM invoices
WHERE client_id = 3
);
SELECT *
FROM invoices
WHERE invoice_total > (
SELECT MAX(invoice_total) FROM invoices
WHERE client_id = 3
);
/*************************************/
/* ANY keyword */
/*************************************/
/*clients with at least 2 invoices*/
SELECT *
FROM clients
WHERE client_id = ANY (
SELECT client_id
FROM invoices
GROUP BY client_id
HAVING COUNT(client_id) >= 2
);
/*************************************/
/* Correlated Subqueries */
/*************************************/
/*employees whose salary is above the average in their office*/
USE mosh_sql_hr;
SELECT *
FROM employees e
WHERE salary > (
SELECT AVG(salary)
FROM employees
WHERE office_id = e.office_id
GROUP BY office_id
);
/*client's own invoices that are larger than client's average invoice amount*/
USE mosh_sql_invoicing;
SELECT *
FROM invoices i
WHERE invoice_total > (
SELECT AVG(invoice_total)
FROM invoices
WHERE client_id = i.client_id
);
/***********************************************************************************/
/* EXISTS Operator */
/* if result set of sub queries is too large, we better use EXISTS rather than IN */
/***********************************************************************************/
/*clients that have an inovice*/
SELECT *
FROM clients c
WHERE EXISTS (
SELECT client_id
FROM invoices
WHERE client_id = c.client_id
);
/*products never been ordered */
SELECT *
FROM products p
WHERE NOT EXISTS(
SELECT product_id
FROM order_items
WHERE product_id = p.product_id
);
SELECT *
FROM products p
WHERE product_id NOT IN(
SELECT product_id
FROM order_items
);
/*************************************/
/* Subqueries in SELECT */
/*************************************/
USE mosh_sql_invoicing;
SELECT client_id,name,
(SELECT SUM(invoice_total) FROM invoices WHERE client_id = c.client_id) AS total_sales,
(SELECT AVG(invoice_total) FROM invoices WHERE client_id = c.client_id) AS average,
(SELECT total_sales - average) AS difference
FROM clients c;
SELECT c.client_id, c.name, SUM(i.invoice_total) AS total_sales,
AVG(i.invoice_total) AS average, SUM(i.invoice_total) - AVG(i.invoice_total) AS difference
FROM invoices i
RIGHT JOIN clients c ON c.client_id = i.client_id
GROUP BY 1;
/*************************************/
/* Subqueries in FROM */
/*************************************/
SELECT *
FROM (
SELECT c.client_id, c.name, SUM(i.invoice_total) AS total_sales,
AVG(i.invoice_total) AS average, SUM(i.invoice_total) - AVG(i.invoice_total) AS difference
FROM invoices i
RIGHT JOIN clients c ON c.client_id = i.client_id
GROUP BY 1
) AS tbl1
WHERE total_sales IS NOT NULL;