Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What are common table expressions - [email protected] #296

Merged
merged 7 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;