I want to rank the students based on the subject, and their total overall for that particular subject. Query works for all subjects but when I specify the where
clause, I only get the rank of the user specified in the where
clause and rank is always 1 seems it’s not checking the other ids
SELECT
Student_id,
Subject,
Total_Overal,
RANK() OVER (PARTITION BY Subject ORDER BY Total_Overal DESC) as StudentRank
FROM
exams
WHERE
Student_id = "155676"
AND Subject = "French";
2
Answers
Because the where clause results in only one row of results, the ranking will always be 1. You need to add another layer of SELECT around:
If you understand SQL order of execution, you will understand why you get what you get.
WHERE
is evaluated beforeSELECT
.https://vladmihalcea.com/sql-operation-order/