-
Notifications
You must be signed in to change notification settings - Fork 551
/
Copy path12.One To Many.sql
169 lines (148 loc) · 5.37 KB
/
12.One To Many.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
/* data prep*/
CREATE TABLE customers(
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL
);
CREATE TABLE orders(
id INT AUTO_INCREMENT PRIMARY KEY,
order_date DATE,
amount DECIMAL(8,2),
customer_id INT,
FOREIGN KEY(customer_id) REFERENCES customers(id)
);
INSERT INTO customers (first_name, last_name, email)
VALUES ('Boy', 'George', '[email protected]'),
('George', 'Michael', '[email protected]'),
('David', 'Bowie', '[email protected]'),
('Blue', 'Steele', '[email protected]'),
('Bette', 'Davis', '[email protected]');
INSERT INTO orders (order_date, amount, customer_id)
VALUES ('2016/02/10', 99.99, 1),
('2017/11/11', 35.50, 1),
('2014/12/12', 800.67, 2),
('2015/01/03', 12.50, 2),
('1999/04/11', 450.25, 5);
/*customers by spending high to low order */
/*send them loyality programs,etc*/
SELECT first_name, last_name,SUM(amount) AS total_spending
FROM customers
JOIN orders ON customers.id = orders.customer_id
GROUP BY customers.id
ORDER BY total_spending DESC;
/* customers who haven't spent anything or ordered anything yet */
/*we can send out discount cupons.. etc*/
SELECT first_name, last_name,amount
FROM customers
LEFT JOIN orders ON customers.id = orders.customer_id
WHERE orders.customer_id IS NULL;
/*-------------------------------------------*/
/*------------------ Challenges -------------------------*/
CREATE TABLE students(
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(150)
);
CREATE TABLE papers(
title VARCHAR(150) NOT NULL,
grade INT NOT NULL,
student_id INT,
FOREIGN KEY(student_id) REFERENCES students(id) ON DELETE CASCADE
);
INSERT INTO students (first_name) VALUES
('Caleb'), ('Samantha'), ('Raj'), ('Carlos'), ('Lisa');
INSERT INTO papers (student_id, title, grade ) VALUES
(1, 'My First Book Report', 60),
(1, 'My Second Book Report', 75),
(2, 'Russian Lit Through The Ages', 94),
(2, 'De Montaigne and The Art of The Essay', 98),
(4, 'Borges and Magical Realism', 89);
/*----------------------*/
/*
+------------+---------------------------------------+-------+
| first_name | title | grade |
+------------+---------------------------------------+-------+
| Samantha | De Montaigne and The Art of The Essay | 98 |
| Samantha | Russian Lit Through The Ages | 94 |
| Carlos | Borges and Magical Realism | 89 |
| Caleb | My Second Book Report | 75 |
| Caleb | My First Book Report | 60 |
+------------+---------------------------------------+-------+
*/
SELECT first_name, title,grade
FROM students
JOIN papers ON students.id = papers.student_id
ORDER BY grade DESC;
/*
+------------+---------------------------------------+-------+
| first_name | title | grade |
+------------+---------------------------------------+-------+
| Caleb | My First Book Report | 60 |
| Caleb | My Second Book Report | 75 |
| Samantha | Russian Lit Through The Ages | 94 |
| Samantha | De Montaigne and The Art of The Essay | 98 |
| Raj | NULL | NULL |
| Carlos | Borges and Magical Realism | 89 |
| Lisa | NULL | NULL |
+------------+---------------------------------------+-------+
*/
SELECT first_name, title, grade
FROM students
LEFT JOIN papers ON students.id = papers.student_id;
/*
+------------+---------------------------------------+-------+
| first_name | title | grade |
+------------+---------------------------------------+-------+
| Caleb | My First Book Report | 60 |
| Caleb | My Second Book Report | 75 |
| Samantha | Russian Lit Through The Ages | 94 |
| Samantha | De Montaigne and The Art of The Essay | 98 |
| Raj | MISSING | 0 |
| Carlos | Borges and Magical Realism | 89 |
| Lisa | MISSING | 0 |
+------------+---------------------------------------+-------+
*/
SELECT first_name,
IFNULL(title,'MISSING'),
IFNULL(grade,0)
FROM students
LEFT JOIN papers ON students.id = papers.student_id;
/*
+------------+---------+
| first_name | average |
+------------+---------+
| Samantha | 96.0000 |
| Carlos | 89.0000 |
| Caleb | 67.5000 |
| Raj | 0 |
| Lisa | 0 |
+------------+---------+
*/
SELECT first_name,
IFNULL(AVG(grade),0) AS average
FROM students
LEFT JOIN papers ON students.id = papers.student_id
GROUP BY students.id
ORDER BY average DESC;
/*
+------------+---------+----------------+
| first_name | average | passing_status |
+------------+---------+----------------+
| Samantha | 96.0000 | PASSING |
| Carlos | 89.0000 | PASSING |
| Caleb | 67.5000 | FAILING |
| Raj | 0 | FAILING |
| Lisa | 0 | FAILING |
+------------+---------+----------------+
*/
SELECT first_name,
IFNULL(AVG(grade),0) AS average,
CASE
WHEN AVG(grade) >= 75 THEN 'PASSING'
WHEN AVG(grade) IS NULL THEN 'FAILING'
ELSE 'FAILING'
END AS passing_status
FROM students
LEFT JOIN papers ON students.id = papers.student_id
GROUP BY students.id
ORDER BY average DESC;