Skip to content

Commit 7736930

Browse files
authored
Merge pull request #285 from Anees1214/main
Why Window Functions Cannot Be Used in WHERE Clauses in SQL
2 parents 65527d8 + 9fe8281 commit 7736930

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--using a Common Table Expression
2+
WITH RankedStudents AS (
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
11+
WHERE r <= 3;
12+
13+
--using a subquery
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
21+
) AS RankedStudents
22+
WHERE r <= 3;

0 commit comments

Comments
 (0)