Skip to content
Advertisement

get all students who has got more marks than the average marks of that class

I got two tables below called tbl_student where we store students’ data like id, name, class, roll no. and age, and tbl_marks where we store marks scored in an exam. –

and

And these are some data for the respective tables:

and

Now, I need to get all students in each class who got more marks than the average marks in their respective classes.

I tried it using the RANK() function with the PARTITION BY clause.

Advertisement

Answer

try this :

see the result in dbfiddle.

see the manual about the window functions :

https://www.postgresql.org/docs/current/tutorial-window.html https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-WINDOW https://www.postgresql.org/docs/current/functions-window.html

and also about the aggregate functions which can be used as window functions :

https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-AGGREGATES https://www.postgresql.org/docs/current/functions-aggregate.html

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement