Skip to content

Commit cecfbd6

Browse files
authored
Update window-functions-in-where-clause.sql
1 parent 471bfdf commit cecfbd6

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
--using a Common Table Expression
22
WITH RankedStudents AS (
3-
SELECT id, name, national_id, birth_date, enrollment_date, graduation_date, gpa,
4-
RANK() OVER (ORDER BY national_id ASC) AS r FROM Student )
5-
SELECT id, name, national_id, r
6-
FROM RankedStudents
3+
SELECT
4+
id, name, gpa,
5+
RANK() OVER (ORDER BY gpa DESC) AS r
6+
FROM Student
7+
WHERE gpa IS NOT NULL
8+
)
9+
SELECT id, name, gpa, r
10+
FROM RankedStudents
711
WHERE r <= 3;
812

913
--using a subquery
10-
SELECT id, name, national_id, r
11-
FROM ( SELECT id, name, national_id, birth_date, enrollment_date, graduation_date, gpa,
12-
RANK() OVER (ORDER BY national_id ASC) AS r
13-
FROM Student
14+
SELECT *
15+
FROM (
16+
SELECT
17+
id, name, GPA,
18+
RANK() OVER (ORDER BY GPA DESC) AS r
19+
FROM Student
20+
WHERE GPA IS NOT NULL
1421
) AS RankedStudents
1522
WHERE r <= 3;

0 commit comments

Comments
 (0)