Skip to content

Commit f8ba5ca

Browse files
authored
Merge pull request #230 from daKeshra/main
keshradamora
2 parents ad11778 + 0998d20 commit f8ba5ca

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- update data with case case condition
2+
UPDATE Student
3+
SET graduation_date = CASE
4+
WHEN e.semester = 'FALL' THEN '2024-12-15'
5+
WHEN e.semester = 'SPRING' THEN '2024-06-15'
6+
ELSE NULL
7+
END
8+
FROM Student AS s
9+
JOIN Exam AS e ON s.id = e.student_id;
10+
11+
12+
-- display outpt for SPRING semester
13+
SELECT * FROM Student AS s
14+
JOIN Exam AS e ON e.student_id = s.id
15+
WHERE e.semester = 'SPRING'
16+
LIMIT 10;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- display top 10 rows of the Student table
2+
SELECT * FROM Student
3+
LIMIT 10;
4+
5+
-- display top 10 result from a join of Exam
6+
-- and Student table
7+
SELECT * FROM Exam
8+
JOIN Student ON Exam.student_id = Student.id
9+
LIMIT 10;
10+
11+
12+
-- UPDATE JOIN with WHERE clause
13+
UPDATE Exam
14+
SET e.grade = 'C'
15+
FROM Exam AS e
16+
JOIN Student AS s ON e.student_id = s.id
17+
WHERE s.national_id = 123345566 AND e.grade IS NULL;
18+
19+
20+
-- confirm the output
21+
SELECT * FROM Exam AS e
22+
JOIN Student AS s ON e.student_id = s.id
23+
WHERE national_id = 123345566;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--analyze query performance after assigning composite index
2+
EXPLAIN SELECT *
3+
FROM Student
4+
WHERE enrollment_date = '2020-01-15' AND gpa > 4.0;
5+
6+
--optimized query
7+
SELECT *
8+
FROM Student
9+
WHERE enrollment_date = '2020-01-15' AND gpa > 4.0;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--analyze query performance on column without composite index assigned, using EXPLAIN
2+
EXPLAIN SELECT *
3+
FROM Student
4+
WHERE birth_date = '2002-05-15' AND gpa > 3.5;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--show index in table
2+
SHOW INDEX FROM Student;
3+
4+
--create index on multiple columns
5+
CREATE INDEX idx_enrollment_gpa
6+
ON Student (enrollment_date, gpa);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--drop the index created
2+
DROP INDEX idx_enrollment_gpa
3+
ON Student;
4+
5+
--Verify if index is dropped
6+
SHOW INDEX FROM Student;

0 commit comments

Comments
 (0)