-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchema.sql
More file actions
183 lines (130 loc) · 4 KB
/
Schema.sql
File metadata and controls
183 lines (130 loc) · 4 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
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
create table customers (
customer_id int PRIMARY KEY auto_increment,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
insert into customers (first_name, last_name)
values
("jack", "black"),
("panda", "kungfu"),
("anabella", "norman");
select * from customers;
drop table transactions;
create table transactions (
transaction_id int PRIMARY KEY auto_increment,
amount decimal (5, 2),
customer_id int,
foreign key(customer_id) REFERENCES customers(customer_id)
);
alter table transactions
drop foreign key transactions_ibfk_1;
alter table transactions
add constraint fk_customer_id
foreign key (customer_id) REFERENCES customers (customer_id);
delete from transactions;
alter table transactions
auto_increment = 1000;
insert into transactions (amount, customer_id)
values (4.99, 3),
(2.89, 2),
(3.38, 3),
(4.99, 1);
delete from customers
where customer_id = 3;
insert into transactions (amount, customer_id)
values (1.00, null);
insert into customers (first_name, last_name)
values("poopy", "puff");
select * from product;
select * from customers;
select transaction_id, amount, first_name, last_name
from transactions
inner join customers on
transactions.customer_id = customers.customer_id;
select * from
transactions LEFT JOIN
customers on
transactions.customer_id = customers.customer_id;
select * from
transactions RIGHT JOIN
customers on
transactions.customer_id = customers.customer_id;
select COUNT(amount) as count
from transactions;
select MAX(amount) as maximum
from transactions;
select MIN(amount) as minimum
from transactions;
select AVG(amount) as average
from transactions;
select SUM(amount) as sum
from transactions;
select * from employees;
select CONCAT(first_name, " ", last_name) AS full_name
from employees;
alter table employees
add column job varchar(25) after hourly_pay;
update employees
set job = "janitor"
where employee_id = 6;
select * from
employees where
hire_date < "2023-01-5" AND job = "cook";
select * from employees
where job = "cook" OR job = "cashier";
select * from
employees where
NOT job = "Manager" AND NOT job = "engineer";
select * from
employees where hire_date BETWEEN "2023-03-15" AND "2024-02-02";
select * from employees
where job IN ("cashier", "cook", "janitor");
-----------------------------
-- JOIN with filtering and aggregation:
SELECT c.first_name, c.country, o.item, SUM(o.amount) AS total_spent
FROM CUSTOMERS c
INNER JOIN ORDERS o ON c.customer_id = o.customer_id
INNER JOIN SHIPPING s ON o.order_id = s.order_id
WHERE s.status = 'Delivered' AND YEAR(o.order_date) = 2023
GROUP BY c.first_name, c.country, o.item
ORDER BY total_spent DESC;
-- Subquery with window function and filtering:
WITH top_sellers AS (
SELECT customer_id, SUM(amount) AS total_sales
FROM ORDERS
GROUP BY customer_id
ORDER BY total_sales DESC
LIMIT 10
)
SELECT c.first_name, c.last_name, ts.total_sales
FROM CUSTOMERS c
INNER JOIN top_sellers ts ON c.customer_id = ts.customer_id;
-- User-defined function with error handling:
DELIMITER //
CREATE FUNCTION calculate_age(birth_date DATE)
RETURNS INT
BEGIN
DECLARE age INT;
SET age = FLOOR(DATEDIFF(CURDATE(), birth_date) / 365.25);
IF age < 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE = 'Invalid birth date';
END IF;
RETURN age;
END //
DELIMITER ;
SELECT first_name, last_name, calculate_age(birth_date) AS age
FROM CUSTOMERS;
-- Common Table Expression (CTE) for recursive data processing:
WITH employee_hierarchy (employee_id, manager_id, level) AS (
SELECT id, manager_id, 1 AS level
FROM EMPLOYEES
WHERE manager_id IS NULL
UNION ALL
SELECT e.id, e.manager_id, h.level + 1
FROM EMPLOYEES e
INNER JOIN employee_hierarchy h ON e.manager_id = h.employee_id
)
SELECT e.first_name, e.last_name, h.level
FROM EMPLOYEES e
INNER JOIN employee_hierarchy h ON e.id = h.employee_id
ORDER BY h.level;