I\'m trying to write a query that will list student(s) enrolled with the maximum total credit points.
Here is my query:
SELECT s.S_ID,
s.S_LAS
If you build your query one join at a time, you will notice, first, that you don't need any of those inline views. You can do it very simply with one CTE definition and a subquery.
WITH
student AS
( SELECT 1 s_id, 'Jones' s_last, 'Sally' s_first FROM dual UNION ALL
SELECT 2, 'Smith', 'Pete' FROM dual UNION ALL
SELECT 3, 'DeThroned', 'Kate' FROM dual
),
enrollment AS
( SELECT 1 s_id, 10 c_sec_id FROM dual UNION ALL
select 1, 11 FROM dual UNION ALL
select 2, 11 FROM dual UNION ALL
SELECT 3, 12 FROM dual
),
course_section AS
( SELECT 10 c_sec_id, 101 course_no FROM dual UNION ALL
SELECT 11, 102 FROM dual UNION ALL
SELECT 12, 103 FROM dual
),
course AS
( SELECT 101 course_id, 1 credits FROM dual UNION ALL
SELECT 102, 2 FROM dual UNION ALL
SELECT 103, 3 FROM dual
),
StudentCredit AS
( SELECT s.s_id, s_last, s_first, sum( c.credits ) AS TotalCredits
FROM student s
JOIN enrollment e
ON e.s_id = s.s_id
JOIN course_section cs
ON cs.c_sec_id = e.c_sec_id
JOIN course c
ON c.course_id = cs.course_no
GROUP BY s.s_id, s_last, s_first
)
SELECT *
FROM StudentCredit
WHERE TotalCredits = (
SELECT MAX( TotalCredits )
FROM StudentCredit
);