-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_3.sql
More file actions
154 lines (108 loc) · 3.11 KB
/
data_3.sql
File metadata and controls
154 lines (108 loc) · 3.11 KB
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
-- subquery
-- a query within another query
-- query(subquery)
select * from employees;
select first_name, last_name, hourly_pay,
(select AVG(hourly_pay) from employees) as avg_pay
from employees;
select first_name, last_name, hourly_pay
from employees
where hourly_pay > (select avg(hourly_pay) from employees);
select * from transactions;
select first_name, last_name
from customers
where customer_id IN
(select distinct customer_id
from transactions
where customer_id IS NOT NULL);
select first_name, last_name
from customers
where customer_id NOT IN
(select distinct customer_id
from transactions
where customer_id IS NOT NULL);
-- GROUP BY = aggregate all rows by a specific column
-- often used with agregate functions
-- ex. SUM(), MAX(), MIN(), AVG(), COUNT()
CREATE TABLE candies (
color VARCHAR(20),
quantity INT
);
SELECT color, SUM(quantity) AS total_quantity
FROM candies
GROUP BY color;
insert into candies (color, quantity)
values ("red", 50),
("blue", 30),
("green", 70),
("black", 80);
select * from candies;
alter table transactions
add column order_date varchar(50)
after customer_id;
update transactions
set order_date = "2023-01-13"
where transaction_id = 1008;
select sum(amount), order_date
from transactions
group by order_date;
select count(amount), order_date
from transactions
group by order_date;
select sum(amount), customer_id
from transactions
group by customer_id;
select count(amount), customer_id
from transactions
group by customer_id;
select count(amount), customer_id
from transactions
group by customer_id
having count(amount) > 1 AND customer_id IS NOT NULL;
select * from transactions;
-- ROLLUP, extension of the group by clause
-- produces another row and shows the grand total (super-aggregate value)
select * from transactions;
select sum(amount), order_date
from transactions
group by order_date with rollup;
select count(transaction_id), order_date
from transactions
group by order_date with rollup;
select count(transaction_id) as "# of orders", customer_id
from transactions
group by customer_id with rollup;
select * from employees;
select sum(hourly_pay), employee_id
from employees
group by employee_id with rollup;
-- ON DELETE SET NULL = when FK is deleted, replace it with NULL
-- ON DELETE CASCADE = when FK is deleted, delete the row
set foreign_key_checks = 0;
delete from customers
where customer_id = 1;
select * from customers;
select * from transactions;
insert into customers
values (1, "jack", "black", 2, "fish@gmail.com");
alter table transactions
drop foreign key fk_customer_id;
alter table transactions
add constraint fk_customer_id
foreign key(customer_id) references customers(customer_id)
ON DELETE SET NULL;
delete from transactions
where customer_id = 1;
select * from transactions;
select * from customers;
update transactions
set customer_id = 1
where transaction_id = 1008;
alter table transactions
add constraint fk_transaction_id
foreign key(customer_id) references customers(customer_id)
ON DELETE CASCADE;
select * from transactions;
select * from customers;
delete from customers
where customer_id = 1;