From 324cea2ad602a68c5c1d01c2956ea09c187d3604 Mon Sep 17 00:00:00 2001 From: daKeshra Date: Wed, 7 Aug 2024 17:30:25 +0100 Subject: [PATCH 1/2] Composite indexes in SQL --- .../analyze-composite-index-performance.sql | 9 +++++++++ .../analyze-query-with-no-composite-index.sql | 4 ++++ .../composite-index-in-sql/create-composite-index.sql | 6 ++++++ .../composite-index-in-sql/drop-composite-index.sql | 6 ++++++ 4 files changed, 25 insertions(+) create mode 100644 sql-queries-index/composite-index-in-sql/analyze-composite-index-performance.sql create mode 100644 sql-queries-index/composite-index-in-sql/analyze-query-with-no-composite-index.sql create mode 100644 sql-queries-index/composite-index-in-sql/create-composite-index.sql create mode 100644 sql-queries-index/composite-index-in-sql/drop-composite-index.sql diff --git a/sql-queries-index/composite-index-in-sql/analyze-composite-index-performance.sql b/sql-queries-index/composite-index-in-sql/analyze-composite-index-performance.sql new file mode 100644 index 00000000..3c9ab753 --- /dev/null +++ b/sql-queries-index/composite-index-in-sql/analyze-composite-index-performance.sql @@ -0,0 +1,9 @@ +--analyze query performance after assigning composite index +EXPLAIN SELECT * +FROM Student +WHERE enrollment_date = '2020-01-15' AND gpa > 4.0; + +--optimized query +SELECT * +FROM Student +WHERE enrollment_date = '2020-01-15' AND gpa > 4.0; diff --git a/sql-queries-index/composite-index-in-sql/analyze-query-with-no-composite-index.sql b/sql-queries-index/composite-index-in-sql/analyze-query-with-no-composite-index.sql new file mode 100644 index 00000000..08ef3942 --- /dev/null +++ b/sql-queries-index/composite-index-in-sql/analyze-query-with-no-composite-index.sql @@ -0,0 +1,4 @@ +--analyze query performance on column without composite index assigned, using EXPLAIN +EXPLAIN SELECT * +FROM Student +WHERE birth_date = '2002-05-15' AND gpa > 3.5; diff --git a/sql-queries-index/composite-index-in-sql/create-composite-index.sql b/sql-queries-index/composite-index-in-sql/create-composite-index.sql new file mode 100644 index 00000000..cc7853d2 --- /dev/null +++ b/sql-queries-index/composite-index-in-sql/create-composite-index.sql @@ -0,0 +1,6 @@ +--show index in table +SHOW INDEX FROM Student; + +--create index on multiple columns +CREATE INDEX idx_enrollment_gpa +ON Student (enrollment_date, gpa); diff --git a/sql-queries-index/composite-index-in-sql/drop-composite-index.sql b/sql-queries-index/composite-index-in-sql/drop-composite-index.sql new file mode 100644 index 00000000..b8af7158 --- /dev/null +++ b/sql-queries-index/composite-index-in-sql/drop-composite-index.sql @@ -0,0 +1,6 @@ +--drop the index created +DROP INDEX idx_enrollment_gpa +ON Student; + +--Verify if index is dropped +SHOW INDEX FROM Student; From 0998d206a8e09c3838eed4705b613ee0562be297 Mon Sep 17 00:00:00 2001 From: daKeshra Date: Tue, 17 Sep 2024 19:00:06 -0700 Subject: [PATCH 2/2] updating-data-with-joins-in-sql --- .../update-join-with-case.sql | 16 +++++++++++++ .../update-join-with-where.sql | 23 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 sql-queries-5/updating-data-with-joins-in-sql/update-join-with-case.sql create mode 100644 sql-queries-5/updating-data-with-joins-in-sql/update-join-with-where.sql diff --git a/sql-queries-5/updating-data-with-joins-in-sql/update-join-with-case.sql b/sql-queries-5/updating-data-with-joins-in-sql/update-join-with-case.sql new file mode 100644 index 00000000..5feae60b --- /dev/null +++ b/sql-queries-5/updating-data-with-joins-in-sql/update-join-with-case.sql @@ -0,0 +1,16 @@ +-- update data with case case condition +UPDATE Student +SET graduation_date = CASE + WHEN e.semester = 'FALL' THEN '2024-12-15' + WHEN e.semester = 'SPRING' THEN '2024-06-15' + ELSE NULL + END +FROM Student AS s +JOIN Exam AS e ON s.id = e.student_id; + + +-- display outpt for SPRING semester +SELECT * FROM Student AS s +JOIN Exam AS e ON e.student_id = s.id +WHERE e.semester = 'SPRING' +LIMIT 10; \ No newline at end of file diff --git a/sql-queries-5/updating-data-with-joins-in-sql/update-join-with-where.sql b/sql-queries-5/updating-data-with-joins-in-sql/update-join-with-where.sql new file mode 100644 index 00000000..41e48844 --- /dev/null +++ b/sql-queries-5/updating-data-with-joins-in-sql/update-join-with-where.sql @@ -0,0 +1,23 @@ +-- display top 10 rows of the Student table +SELECT * FROM Student +LIMIT 10; + +-- display top 10 result from a join of Exam +-- and Student table +SELECT * FROM Exam +JOIN Student ON Exam.student_id = Student.id +LIMIT 10; + + +-- UPDATE JOIN with WHERE clause +UPDATE Exam +SET e.grade = 'C' +FROM Exam AS e +JOIN Student AS s ON e.student_id = s.id +WHERE s.national_id = 123345566 AND e.grade IS NULL; + + +-- confirm the output +SELECT * FROM Exam AS e +JOIN Student AS s ON e.student_id = s.id +WHERE national_id = 123345566; \ No newline at end of file