From 0e59ef4ae03d3fc2f8f8ddec7abb8bf30dd7ccbe Mon Sep 17 00:00:00 2001 From: daKeshra Date: Tue, 17 Dec 2024 10:19:10 +0100 Subject: [PATCH 1/4] What are common table expressions? --- .../enhancing-query-readability.sql | 16 +++++++++ .../groupingby-scalar-subselect.sql | 16 +++++++++ .../recursive-cte-queries.sql | 13 +++++++ .../reference-resulting-table-mulltiple-times | 34 +++++++++++++++++++ .../substitute-for-view.sql | 20 +++++++++++ 5 files changed, 99 insertions(+) create mode 100644 sql-queries-5/what-are-common-table-expressions/enhancing-query-readability.sql create mode 100644 sql-queries-5/what-are-common-table-expressions/groupingby-scalar-subselect.sql create mode 100644 sql-queries-5/what-are-common-table-expressions/recursive-cte-queries.sql create mode 100644 sql-queries-5/what-are-common-table-expressions/reference-resulting-table-mulltiple-times create mode 100644 sql-queries-5/what-are-common-table-expressions/substitute-for-view.sql diff --git a/sql-queries-5/what-are-common-table-expressions/enhancing-query-readability.sql b/sql-queries-5/what-are-common-table-expressions/enhancing-query-readability.sql new file mode 100644 index 00000000..9da4874f --- /dev/null +++ b/sql-queries-5/what-are-common-table-expressions/enhancing-query-readability.sql @@ -0,0 +1,16 @@ +-- Using a CTE to enhance query readability +WITH ActiveCourse AS ( + SELECT s.id, s.name AS student_name, + c.name AS course_title, + SUM(c.credits) AS course_credits + FROM Student AS s + JOIN Exam AS e + ON e.student_id = s.id + JOIN course AS c + ON c.id = e.course_id + WHERE c.is_active = 'Yes' + GROUP BY s.id + ) + + SELECT * FROM ActiveCourse + WHERE course_credits > 70; \ No newline at end of file diff --git a/sql-queries-5/what-are-common-table-expressions/groupingby-scalar-subselect.sql b/sql-queries-5/what-are-common-table-expressions/groupingby-scalar-subselect.sql new file mode 100644 index 00000000..e47f4237 --- /dev/null +++ b/sql-queries-5/what-are-common-table-expressions/groupingby-scalar-subselect.sql @@ -0,0 +1,16 @@ +-- Grouping by a Column Derived from a Scalar Subselect or Non-Deterministic Function +WITH AgeCategories AS ( + SELECT s.id AS student_id, + s.name AS student_name, + TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) AS age_at_enrollment, + CASE + WHEN TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) < 20 THEN 'Under 20' + WHEN TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) BETWEEN 20 AND 30 THEN '20-30' + ELSE 'Over 30' + END AS age_category + FROM Student AS s + ) + SELECT age_category, + COUNT(student_id) AS student_count + FROM AgeCategories + GROUP BY age_category; \ No newline at end of file diff --git a/sql-queries-5/what-are-common-table-expressions/recursive-cte-queries.sql b/sql-queries-5/what-are-common-table-expressions/recursive-cte-queries.sql new file mode 100644 index 00000000..8d9129cc --- /dev/null +++ b/sql-queries-5/what-are-common-table-expressions/recursive-cte-queries.sql @@ -0,0 +1,13 @@ +-- recursive CTE query +WITH RECURSIVE StudentCourse AS ( + SELECT student_id, course_id, 1 AS level + FROM Exam + WHERE course_id = 'CS111' + UNION ALL + SELECT e.student_id, e.course_id, sh.level + 1 + FROM Exam AS e + INNER JOIN StudentCourse AS sc + ON e.student_id = sh.course_id + ) + SELECT student_id, course_id, level + FROM StudentCourse; \ No newline at end of file diff --git a/sql-queries-5/what-are-common-table-expressions/reference-resulting-table-mulltiple-times b/sql-queries-5/what-are-common-table-expressions/reference-resulting-table-mulltiple-times new file mode 100644 index 00000000..6943da2b --- /dev/null +++ b/sql-queries-5/what-are-common-table-expressions/reference-resulting-table-mulltiple-times @@ -0,0 +1,34 @@ +-- Reference the resulting table multiple times in the same statement +WITH ExamSummary AS ( + SELECT e.student_id, + s.name AS student_name, + COUNT(*) AS total_exams, + SUM(CASE + WHEN e.grade = 'A' THEN 1 + ELSE 0 + END + ) AS a_grades_count, + AVG( CASE + WHEN e.grade = 'A' THEN 4 + WHEN e.grade = 'B' THEN 3 + WHEN e.grade = 'C' THEN 2 + WHEN e.grade = 'D' THEN 1 + ELSE 0 + END ) AS average_grade_points + FROM Exam AS e + JOIN student AS s + ON e.student_id = s.id + GROUP BY e.student_id, s.name + ) + SELECT es1.student_name, + es1.total_exams, + es1.a_grades_count, + es1.average_grade_points, + CASE + WHEN es1.a_grades_count >= 3 THEN 'Top Performer' + ELSE 'Regular Performer' + END AS performance_category + FROM ExamSummary AS es1 + JOIN ExamSummary es2 + ON es1.student_id = es2.student_id + WHERE es1.total_exams > 3; \ No newline at end of file diff --git a/sql-queries-5/what-are-common-table-expressions/substitute-for-view.sql b/sql-queries-5/what-are-common-table-expressions/substitute-for-view.sql new file mode 100644 index 00000000..aadfe2b5 --- /dev/null +++ b/sql-queries-5/what-are-common-table-expressions/substitute-for-view.sql @@ -0,0 +1,20 @@ +-- USING CTE as a substitute for view +WITH AverageGrades AS ( + SELECT e.student_id, s.name AS student_name, + AVG(CASE + WHEN e.grade = 'A+' THEN 5 + WHEN e.grade = 'A' THEN 4 + WHEN e.grade = 'B' THEN 3 + WHEN e.grade = 'C' THEN 2 + WHEN e.grade = 'D' THEN 1 + ELSE 0 + END + ) AS average_grade_points + FROM Exam AS e + JOIN Student AS s + ON e.student_id = s.id + GROUP BY e.student_id + ) + SELECT student_name, average_grade_points + FROM AverageGrades + LIMIT 5; \ No newline at end of file From 8e586d90c43b77f4006ca30bfcb28dd32d922d19 Mon Sep 17 00:00:00 2001 From: daKeshra Date: Tue, 24 Dec 2024 15:32:19 +0100 Subject: [PATCH 2/4] modified recursive cte query --- .../groupingby-scalar-subselect.sql | 6 +-- .../recursive-cte-queries.sql | 43 ++++++++++++++----- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/sql-queries-5/what-are-common-table-expressions/groupingby-scalar-subselect.sql b/sql-queries-5/what-are-common-table-expressions/groupingby-scalar-subselect.sql index e47f4237..2729e798 100644 --- a/sql-queries-5/what-are-common-table-expressions/groupingby-scalar-subselect.sql +++ b/sql-queries-5/what-are-common-table-expressions/groupingby-scalar-subselect.sql @@ -4,9 +4,9 @@ WITH AgeCategories AS ( s.name AS student_name, TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) AS age_at_enrollment, CASE - WHEN TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) < 20 THEN 'Under 20' - WHEN TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) BETWEEN 20 AND 30 THEN '20-30' - ELSE 'Over 30' + WHEN TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) < 18 THEN 'Under 18' + WHEN TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) = 18 THEN '18 Years' + ELSE 'Above 18' END AS age_category FROM Student AS s ) diff --git a/sql-queries-5/what-are-common-table-expressions/recursive-cte-queries.sql b/sql-queries-5/what-are-common-table-expressions/recursive-cte-queries.sql index 8d9129cc..ca7149d5 100644 --- a/sql-queries-5/what-are-common-table-expressions/recursive-cte-queries.sql +++ b/sql-queries-5/what-are-common-table-expressions/recursive-cte-queries.sql @@ -1,13 +1,36 @@ -- recursive CTE query -WITH RECURSIVE StudentCourse AS ( - SELECT student_id, course_id, 1 AS level - FROM Exam - WHERE course_id = 'CS111' +WITH RECURSIVE SemesterProgression AS ( + SELECT e.student_id, + e.semester AS current_semester, + e.exam_date AS current_exam_date, + e.course_id, c.name AS course_name, + e.grade, e.semester AS semester_chain + FROM + Exam AS e + JOIN Course AS c + ON e.course_id = c.id + WHERE e.student_id = 1001 AND e.semester = (SELECT MIN(semester) FROM exam WHERE student_id = 1001) + UNION ALL - SELECT e.student_id, e.course_id, sh.level + 1 - FROM Exam AS e - INNER JOIN StudentCourse AS sc - ON e.student_id = sh.course_id + + SELECT e.student_id, + e.semester AS current_semester, + e.exam_date AS current_exam_date, + e.course_id, c.name AS course_name, + e.grade, + CONCAT(sp.semester_chain, ' -> ', e.semester) AS semester_chain + FROM Exam AS e + JOIN SemesterProgression AS sp + ON e.student_id = sp.student_id + JOIN Course AS c + ON e.course_id = c.id + WHERE e.semester > sp.current_semester ) - SELECT student_id, course_id, level - FROM StudentCourse; \ No newline at end of file + -- query to display the semester progression + SELECT student_id, + current_semester, + current_exam_date, + course_name, grade, + semester_chain + FROM SemesterProgression + ORDER BY current_exam_date; \ No newline at end of file From 784e0a93d1ee085c4c7f0f59115af25a3997a8d6 Mon Sep 17 00:00:00 2001 From: daKeshra Date: Fri, 10 Jan 2025 00:51:35 +0100 Subject: [PATCH 3/4] what are common table expressions? --- .../groupingby-scalar-subselect.sql | 16 -------- .../recursive-cte-queries.sql | 36 ------------------ .../substitute-for-view.sql | 20 ---------- .../enhancing-query-readability.sql | 0 .../groupingby-scalar-subselect.sql | 16 ++++++++ .../recursive-cte-queries.sql | 37 +++++++++++++++++++ ...erence-resulting-table-mulltiple-times.sql | 0 .../substitute-for-view.sql | 13 +++++++ 8 files changed, 66 insertions(+), 72 deletions(-) delete mode 100644 sql-queries-5/what-are-common-table-expressions/groupingby-scalar-subselect.sql delete mode 100644 sql-queries-5/what-are-common-table-expressions/recursive-cte-queries.sql delete mode 100644 sql-queries-5/what-are-common-table-expressions/substitute-for-view.sql rename {sql-queries-5 => sql-queries-8}/what-are-common-table-expressions/enhancing-query-readability.sql (100%) create mode 100644 sql-queries-8/what-are-common-table-expressions/groupingby-scalar-subselect.sql create mode 100644 sql-queries-8/what-are-common-table-expressions/recursive-cte-queries.sql rename sql-queries-5/what-are-common-table-expressions/reference-resulting-table-mulltiple-times => sql-queries-8/what-are-common-table-expressions/reference-resulting-table-mulltiple-times.sql (100%) create mode 100644 sql-queries-8/what-are-common-table-expressions/substitute-for-view.sql diff --git a/sql-queries-5/what-are-common-table-expressions/groupingby-scalar-subselect.sql b/sql-queries-5/what-are-common-table-expressions/groupingby-scalar-subselect.sql deleted file mode 100644 index 2729e798..00000000 --- a/sql-queries-5/what-are-common-table-expressions/groupingby-scalar-subselect.sql +++ /dev/null @@ -1,16 +0,0 @@ --- Grouping by a Column Derived from a Scalar Subselect or Non-Deterministic Function -WITH AgeCategories AS ( - SELECT s.id AS student_id, - s.name AS student_name, - TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) AS age_at_enrollment, - CASE - WHEN TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) < 18 THEN 'Under 18' - WHEN TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) = 18 THEN '18 Years' - ELSE 'Above 18' - END AS age_category - FROM Student AS s - ) - SELECT age_category, - COUNT(student_id) AS student_count - FROM AgeCategories - GROUP BY age_category; \ No newline at end of file diff --git a/sql-queries-5/what-are-common-table-expressions/recursive-cte-queries.sql b/sql-queries-5/what-are-common-table-expressions/recursive-cte-queries.sql deleted file mode 100644 index ca7149d5..00000000 --- a/sql-queries-5/what-are-common-table-expressions/recursive-cte-queries.sql +++ /dev/null @@ -1,36 +0,0 @@ --- recursive CTE query -WITH RECURSIVE SemesterProgression AS ( - SELECT e.student_id, - e.semester AS current_semester, - e.exam_date AS current_exam_date, - e.course_id, c.name AS course_name, - e.grade, e.semester AS semester_chain - FROM - Exam AS e - JOIN Course AS c - ON e.course_id = c.id - WHERE e.student_id = 1001 AND e.semester = (SELECT MIN(semester) FROM exam WHERE student_id = 1001) - - UNION ALL - - SELECT e.student_id, - e.semester AS current_semester, - e.exam_date AS current_exam_date, - e.course_id, c.name AS course_name, - e.grade, - CONCAT(sp.semester_chain, ' -> ', e.semester) AS semester_chain - FROM Exam AS e - JOIN SemesterProgression AS sp - ON e.student_id = sp.student_id - JOIN Course AS c - ON e.course_id = c.id - WHERE e.semester > sp.current_semester - ) - -- query to display the semester progression - SELECT student_id, - current_semester, - current_exam_date, - course_name, grade, - semester_chain - FROM SemesterProgression - ORDER BY current_exam_date; \ No newline at end of file diff --git a/sql-queries-5/what-are-common-table-expressions/substitute-for-view.sql b/sql-queries-5/what-are-common-table-expressions/substitute-for-view.sql deleted file mode 100644 index aadfe2b5..00000000 --- a/sql-queries-5/what-are-common-table-expressions/substitute-for-view.sql +++ /dev/null @@ -1,20 +0,0 @@ --- USING CTE as a substitute for view -WITH AverageGrades AS ( - SELECT e.student_id, s.name AS student_name, - AVG(CASE - WHEN e.grade = 'A+' THEN 5 - WHEN e.grade = 'A' THEN 4 - WHEN e.grade = 'B' THEN 3 - WHEN e.grade = 'C' THEN 2 - WHEN e.grade = 'D' THEN 1 - ELSE 0 - END - ) AS average_grade_points - FROM Exam AS e - JOIN Student AS s - ON e.student_id = s.id - GROUP BY e.student_id - ) - SELECT student_name, average_grade_points - FROM AverageGrades - LIMIT 5; \ No newline at end of file diff --git a/sql-queries-5/what-are-common-table-expressions/enhancing-query-readability.sql b/sql-queries-8/what-are-common-table-expressions/enhancing-query-readability.sql similarity index 100% rename from sql-queries-5/what-are-common-table-expressions/enhancing-query-readability.sql rename to sql-queries-8/what-are-common-table-expressions/enhancing-query-readability.sql diff --git a/sql-queries-8/what-are-common-table-expressions/groupingby-scalar-subselect.sql b/sql-queries-8/what-are-common-table-expressions/groupingby-scalar-subselect.sql new file mode 100644 index 00000000..062f3073 --- /dev/null +++ b/sql-queries-8/what-are-common-table-expressions/groupingby-scalar-subselect.sql @@ -0,0 +1,16 @@ +-- Grouping by a Column Derived from a Scalar Subselect or Non-Deterministic Function +ITH AgeCategories AS ( + SELECT s.id AS student_id, + s.name AS student_name, + TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) AS age_at_enrollment, + CASE + WHEN TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) = 18 THEN '18 Years' + WHEN TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) = 19 THEN '19 Years' + ELSE 'Above 18' + END AS age_category + FROM Student AS s + ) + SELECT age_category, + COUNT(student_id) AS student_count + FROM AgeCategories + GROUP BY age_category; \ No newline at end of file diff --git a/sql-queries-8/what-are-common-table-expressions/recursive-cte-queries.sql b/sql-queries-8/what-are-common-table-expressions/recursive-cte-queries.sql new file mode 100644 index 00000000..cc95e3bb --- /dev/null +++ b/sql-queries-8/what-are-common-table-expressions/recursive-cte-queries.sql @@ -0,0 +1,37 @@ +-- Recursive cte + WITH RECURSIVE EnrollmentProgression AS ( + + SELECT + id AS student_id, + name, + YEAR(enrollment_date) AS active_year + FROM + Student + + UNION ALL + + -- Recursive statement to increment the year until the graduation year + SELECT + ep.student_id, + ep.name, + ep.active_year + 1 AS year + FROM + EnrollmentProgression ep + INNER JOIN + student s ON ep.student_id = s.id + WHERE + ep.active_year < YEAR(s.graduation_date) +) +-- Select all active years for each student's enrollment +SELECT + student_id, + name, + active_year +FROM + EnrollmentProgression +ORDER BY + student_id, active_year +LIMIT 20; + + +This gives us a year-by-year breakdown of a student’s active enrollment. The table below shows all th \ No newline at end of file diff --git a/sql-queries-5/what-are-common-table-expressions/reference-resulting-table-mulltiple-times b/sql-queries-8/what-are-common-table-expressions/reference-resulting-table-mulltiple-times.sql similarity index 100% rename from sql-queries-5/what-are-common-table-expressions/reference-resulting-table-mulltiple-times rename to sql-queries-8/what-are-common-table-expressions/reference-resulting-table-mulltiple-times.sql diff --git a/sql-queries-8/what-are-common-table-expressions/substitute-for-view.sql b/sql-queries-8/what-are-common-table-expressions/substitute-for-view.sql new file mode 100644 index 00000000..09c49578 --- /dev/null +++ b/sql-queries-8/what-are-common-table-expressions/substitute-for-view.sql @@ -0,0 +1,13 @@ +-- USING CTE as a substitute for view +WITH EnrollmentStats AS ( + SELECT YEAR(s.enrollment_date) AS enrollment_year, + AVG(s.gpa) AS average_gpa + FROM Student AS s + WHERE s.gpa IS NOT NULL + GROUP BY YEAR(s.enrollment_date) + ) + SELECT enrollment_year, + ROUND(average_gpa,3) AS average_gpa + FROM EnrollmentStats + WHERE average_gpa > 3.0 + ORDER BY enrollment_year; \ No newline at end of file From be83a74fb52ca140192736165fb856aaf4c3d96e Mon Sep 17 00:00:00 2001 From: Brandon Ward Date: Fri, 10 Jan 2025 00:10:41 -0700 Subject: [PATCH 4/4] Cleanup --- .../groupingby-scalar-subselect.sql | 2 +- .../recursive-cte-queries.sql | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/sql-queries-8/what-are-common-table-expressions/groupingby-scalar-subselect.sql b/sql-queries-8/what-are-common-table-expressions/groupingby-scalar-subselect.sql index 062f3073..e6413b69 100644 --- a/sql-queries-8/what-are-common-table-expressions/groupingby-scalar-subselect.sql +++ b/sql-queries-8/what-are-common-table-expressions/groupingby-scalar-subselect.sql @@ -1,5 +1,5 @@ -- Grouping by a Column Derived from a Scalar Subselect or Non-Deterministic Function -ITH AgeCategories AS ( +WITH AgeCategories AS ( SELECT s.id AS student_id, s.name AS student_name, TIMESTAMPDIFF(YEAR, s.birth_date, s.enrollment_date) AS age_at_enrollment, diff --git a/sql-queries-8/what-are-common-table-expressions/recursive-cte-queries.sql b/sql-queries-8/what-are-common-table-expressions/recursive-cte-queries.sql index cc95e3bb..8d491587 100644 --- a/sql-queries-8/what-are-common-table-expressions/recursive-cte-queries.sql +++ b/sql-queries-8/what-are-common-table-expressions/recursive-cte-queries.sql @@ -32,6 +32,3 @@ FROM ORDER BY student_id, active_year LIMIT 20; - - -This gives us a year-by-year breakdown of a student’s active enrollment. The table below shows all th \ No newline at end of file