Skip to content

Commit 104edff

Browse files
committed
add one to many relationship
1 parent 14e714c commit 104edff

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

Ultimate MySQL/12.One To Many.sql

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/* data prep*/
2+
CREATE TABLE customers(
3+
id INT AUTO_INCREMENT PRIMARY KEY,
4+
first_name VARCHAR(100) NOT NULL,
5+
last_name VARCHAR(100) NOT NULL,
6+
email VARCHAR(100) NOT NULL
7+
);
8+
9+
CREATE TABLE orders(
10+
id INT AUTO_INCREMENT PRIMARY KEY,
11+
order_date DATE,
12+
amount DECIMAL(8,2),
13+
customer_id INT,
14+
FOREIGN KEY(customer_id) REFERENCES customers(id)
15+
);
16+
17+
INSERT INTO customers (first_name, last_name, email)
18+
VALUES ('Boy', 'George', '[email protected]'),
19+
('George', 'Michael', '[email protected]'),
20+
('David', 'Bowie', '[email protected]'),
21+
('Blue', 'Steele', '[email protected]'),
22+
('Bette', 'Davis', '[email protected]');
23+
24+
INSERT INTO orders (order_date, amount, customer_id)
25+
VALUES ('2016/02/10', 99.99, 1),
26+
('2017/11/11', 35.50, 1),
27+
('2014/12/12', 800.67, 2),
28+
('2015/01/03', 12.50, 2),
29+
('1999/04/11', 450.25, 5);
30+
31+
/*customers by spending high to low order */
32+
/*send them loyality programs,etc*/
33+
SELECT first_name, last_name,SUM(amount) AS total_spending
34+
FROM customers
35+
JOIN orders ON customers.id = orders.customer_id
36+
GROUP BY customers.id
37+
ORDER BY total_spending DESC;
38+
39+
40+
/* customers who haven't spent anything or ordered anything yet */
41+
/*we can send out discount cupons.. etc*/
42+
SELECT first_name, last_name,amount
43+
FROM customers
44+
LEFT JOIN orders ON customers.id = orders.customer_id
45+
WHERE orders.customer_id IS NULL;
46+
47+
/*-------------------------------------------*/
48+
49+
/*------------------ Challenges -------------------------*/
50+
CREATE TABLE students(
51+
id INT AUTO_INCREMENT PRIMARY KEY,
52+
first_name VARCHAR(150)
53+
);
54+
55+
56+
CREATE TABLE papers(
57+
title VARCHAR(150) NOT NULL,
58+
grade INT NOT NULL,
59+
student_id INT,
60+
FOREIGN KEY(student_id) REFERENCES students(id) ON DELETE CASCADE
61+
);
62+
63+
INSERT INTO students (first_name) VALUES
64+
('Caleb'), ('Samantha'), ('Raj'), ('Carlos'), ('Lisa');
65+
66+
INSERT INTO papers (student_id, title, grade ) VALUES
67+
(1, 'My First Book Report', 60),
68+
(1, 'My Second Book Report', 75),
69+
(2, 'Russian Lit Through The Ages', 94),
70+
(2, 'De Montaigne and The Art of The Essay', 98),
71+
(4, 'Borges and Magical Realism', 89);
72+
73+
74+
/*----------------------*/
75+
/*
76+
+------------+---------------------------------------+-------+
77+
| first_name | title | grade |
78+
+------------+---------------------------------------+-------+
79+
| Samantha | De Montaigne and The Art of The Essay | 98 |
80+
| Samantha | Russian Lit Through The Ages | 94 |
81+
| Carlos | Borges and Magical Realism | 89 |
82+
| Caleb | My Second Book Report | 75 |
83+
| Caleb | My First Book Report | 60 |
84+
+------------+---------------------------------------+-------+
85+
*/
86+
SELECT first_name, title,grade
87+
FROM students
88+
JOIN papers ON students.id = papers.student_id
89+
ORDER BY grade DESC;
90+
91+
/*
92+
+------------+---------------------------------------+-------+
93+
| first_name | title | grade |
94+
+------------+---------------------------------------+-------+
95+
| Caleb | My First Book Report | 60 |
96+
| Caleb | My Second Book Report | 75 |
97+
| Samantha | Russian Lit Through The Ages | 94 |
98+
| Samantha | De Montaigne and The Art of The Essay | 98 |
99+
| Raj | NULL | NULL |
100+
| Carlos | Borges and Magical Realism | 89 |
101+
| Lisa | NULL | NULL |
102+
+------------+---------------------------------------+-------+
103+
*/
104+
SELECT first_name, title, grade
105+
FROM students
106+
LEFT JOIN papers ON students.id = papers.student_id;
107+
108+
109+
/*
110+
+------------+---------------------------------------+-------+
111+
| first_name | title | grade |
112+
+------------+---------------------------------------+-------+
113+
| Caleb | My First Book Report | 60 |
114+
| Caleb | My Second Book Report | 75 |
115+
| Samantha | Russian Lit Through The Ages | 94 |
116+
| Samantha | De Montaigne and The Art of The Essay | 98 |
117+
| Raj | MISSING | 0 |
118+
| Carlos | Borges and Magical Realism | 89 |
119+
| Lisa | MISSING | 0 |
120+
+------------+---------------------------------------+-------+
121+
*/
122+
SELECT first_name,
123+
IFNULL(title,'MISSING'),
124+
IFNULL(grade,0)
125+
FROM students
126+
LEFT JOIN papers ON students.id = papers.student_id;
127+
128+
129+
/*
130+
+------------+---------+
131+
| first_name | average |
132+
+------------+---------+
133+
| Samantha | 96.0000 |
134+
| Carlos | 89.0000 |
135+
| Caleb | 67.5000 |
136+
| Raj | 0 |
137+
| Lisa | 0 |
138+
+------------+---------+
139+
*/
140+
SELECT first_name,
141+
IFNULL(AVG(grade),0) AS average
142+
FROM students
143+
LEFT JOIN papers ON students.id = papers.student_id
144+
GROUP BY students.id
145+
ORDER BY average DESC;
146+
147+
148+
/*
149+
+------------+---------+----------------+
150+
| first_name | average | passing_status |
151+
+------------+---------+----------------+
152+
| Samantha | 96.0000 | PASSING |
153+
| Carlos | 89.0000 | PASSING |
154+
| Caleb | 67.5000 | FAILING |
155+
| Raj | 0 | FAILING |
156+
| Lisa | 0 | FAILING |
157+
+------------+---------+----------------+
158+
*/
159+
SELECT first_name,
160+
IFNULL(AVG(grade),0) AS average,
161+
CASE
162+
WHEN AVG(grade) >= 75 THEN 'PASSING'
163+
WHEN AVG(grade) IS NULL THEN 'FAILING'
164+
ELSE 'FAILING'
165+
END AS passing_status
166+
FROM students
167+
LEFT JOIN papers ON students.id = papers.student_id
168+
GROUP BY students.id
169+
ORDER BY average DESC;

0 commit comments

Comments
 (0)