-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheda-practice.sql
More file actions
323 lines (239 loc) · 7.15 KB
/
eda-practice.sql
File metadata and controls
323 lines (239 loc) · 7.15 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
create DATABASE if not EXISTS practice;
USE practice;
create table if not exists students (
id TINYINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) UNIQUE,
age TINYINT CHECK (age >= 18),
city VARCHAR(20) NOT NULL,
course VARCHAR(20)
);
insert into students (name, age, city, course) values
("bob", 25, "New Jerysey", "Mathematics"),
("alice", 22, "New York", "Physics"),
("charlie", 23, "Los Angeles", "Chemistry"),
("dave", 24, "Chicago", "Biology"),
("eve", 21, "Houston", "Mathematics"),
("frank", 26, "Phoenix", "Physics");
select city, count(name) from students GROUP BY city;
------------------eda with assignment 2 ---------------------
select count(city), count(age) from students GROUP BY city, age;
SELECT age, city, COUNT(*) AS number_of_students
FROM students
GROUP BY age, city;
SELECT city, GROUP_CONCAT(DISTINCT age ORDER BY age) AS distinct_ages
FROM students
GROUP BY city;
-- how many students are from each age group?
select age, count(age) as num_students from students group by age order by age;
-- how many students are from each city?
select city, count(*) as num_students from students group by city order by city;
select * from students where age > (select avg(age) from students);
select * from students where age in (select min(age) from students);
select city, count(*) from students GROUP BY city HAVING max(age);
-----------------------------------------------------------------
alter table students add column score tinyint UNIQUE;
UPDATE students set score = 50 where id = 7;
CREATE table backup AS SELECT * FROM students;
ALTER table spec_info change column course spec VARCHAR(30);
UPDATE practice.spec_info
set spec = CASE student_id
WHEN 1 THEN 'CSE'
WHEN 2 THEN 'AM'
WHEN 3 THEN 'IC'
WHEN 4 THEN 'OC'
WHEN 5 THEN 'MS'
WHEN 6 THEN 'TM'
WHEN 7 THEN 'EC'
ELSE spec
END;
alter table spec_info drop column city;
alter table spec_info change column student_id std_id tinyint AUTO_INCREMENT FOREIGN KEY REFERENCES students(id);
show tables;
select * from spec_info;
select * from students;
----------------- JOINS -----------------
-- Inner Join
SELECT
students.id,
students.name,
students.age,
students.course,
spec_info.spec,
spec_info.score
FROM students
JOIN spec_info
ON students.id = spec_info.std_id;
select * from students join spec_info on students.id = spec_info.std_id;
-- Full Outer Join
SELECT
students.id,
students.name,
students.age,
students.course,
spec_info.spec,
spec_info.score
FROM students
LEFT JOIN spec_info ON students.id = spec_info.std_id
UNION
SELECT
students.id,
students.name,
students.age,
students.course,
spec_info.spec,
spec_info.score
FROM spec_info
RIGHT JOIN students ON students.id = spec_info.std_id;
select * from students full outer join spec_info on students.id = spec_info.std_id; -- dosn't work in MySQL
-- Left Join
SELECT
students.id,
students.name,
students.age,
students.course,
spec_info.spec,
spec_info.score
FROM students
LEFT JOIN spec_info ON students.id = spec_info.std_id;
-- Right Join
SELECT
students.id,
students.name,
students.age,
students.course,
spec_info.spec,
spec_info.score
FROM spec_info
RIGHT JOIN students ON students.id = spec_info.std_id;
-- Union
select id, score
from students
union
select std_id, score
from spec_info
ORDER BY score DESC;
-- Union All
select id, score
from students
union all
select std_id, score
from spec_info
ORDER BY score DESC;
-- Intersect
select id, score
from students
intersect
select std_id, score
from spec_info
ORDER BY score DESC;
-- Cross Join
SELECT
students.id,
students.name,
students.age,
students.course
FROM students
CROSS JOIN spec_info;
------------- Special Cases ---------------
-- Self Join
SELECT
s1.id AS student_id,
s1.name AS student_name,
s2.id AS friend_id,
s2.name AS friend_name
FROM students s1
JOIN students s2 ON s1.id <> s2.id;
select *
from students s1
join students s2
on s1.id <> s2.id; -- dont even try to do this because it will return a Cartesian product of the table with itself
-- Natural Join
SELECT
students.id,
students.name,
students.age,
students.course,
spec_info.spec,
spec_info.score
FROM students
NATURAL JOIN spec_info;
-- Semi Join
SELECT * FROM students
WHERE EXISTS (
SELECT 1 FROM spec_info WHERE students.id = spec_info.std_id
);
-- A - B
SELECT
students.id,
students.name,
students.age,
students.course,
students.score,
spec_info.spec
FROM students
LEFT JOIN spec_info ON students.id = spec_info.std_id
WHERE spec_info.std_id IS NULL;
-- Anti Left Join
SELECT s.*, b.spec
FROM students s
LEFT JOIN spec_info b ON s.id = b.std_id
WHERE b.std_id IS NULL;
-- B - A
SELECT
students.id,
students.name,
students.age,
students.course,
spec_info.spec,
spec_info.score
FROM students
RIGHT JOIN spec_info ON students.id = spec_info.std_id
WHERE students.id IS NULL;
-- Anti Right Join
SELECT s.*, b.spec
FROM spec_info b
RIGHT JOIN students s ON s.id = b.std_id
WHERE s.id IS NULL;
----------- Concatenation Function -----------------
select id, concat(name, ' ', age) as student_info
from students;
select id, concat(course, ' - ', spec) as course_spec_info
from students
join spec_info on students.id = spec_info.std_id;
----------------- String Functions ----------------
select id, name, LENGTH(name) as name_length
from students
WHERE id in (2, 4, 7);
select SUBSTRING(name, 1) as name_prefix, age
from students;
------------ Date and Time Functions -----------------
SELECT NOW() AS current_datetime; -- returns the current date and time
SELECT DATE('2025-06-11 05:30:00') AS date_value; -- extracts date part from a datetime value
SELECT TIME('2025-06-11 20:45:00') AS time_value; -- extracts time part from a datetime value
-- Date Format: formats date and time values as specified
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d') AS today_date;
SELECT DATE_FORMAT(NOW(), '%H:%i:%s') AS now_time;
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') AS current_date_time; -- this line works same as timestamp
-- timestamp function - combines date and time into a single value
SELECT TIMESTAMP('2025-06-11','20:45:00') AS timestamp_value;
SELECT TIMESTAMP("2025-06-12 00:00:00") AS timestamp_value;
-- Date Arithmetic Functions
SELECT DATE_ADD('2025-06-11', INTERVAL 5 DAY) AS date_plus_5_days; -- adds 5 days to a date
SELECT DATE_SUB('2025-06-11', INTERVAL 3 DAY) AS date_minus_3_days; -- subtracts 3 days from a date
SELECT DATE_ADD('2025-06-11', INTERVAL 2 MONTH) AS date_plus_2_months; -- adds 2 months to a date
SELECT DATE_SUB('2025-06-11', INTERVAL 1 YEAR) AS date_minus_1_year; -- subtracts 1 year from a date
---------------------------------------------------------
create table login (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
insert into login (username, password) values
('admin', 'admin123'),
('admin_jr', 'password'),
('admin_sr', '12345678'),
('admin_1', 'qwerty'),
('admin_2', 'asdfghjkl'),
('admin_3', 'zxcvbnm');
select * from login;