Skip to content
Advertisement

How to select the 16 highest values out of 18 values SQL?

Ok so I have changed it from columns to rows instead 😉

Im saving grades for students. I have a MySQL table called “grade” with three columns called “subject”, “points” and “userId” with 18 rows with different subjects where each subject can have one of the values 20, 17.5, 15 or 12.5

So all users have the same “subjects” but can have different “points”

I need to select and sum the values from the 16 “subjects” that have the highest values out of the 18 “subjects”.

How do I do that? Any input really appreciated, thanks.

Advertisement

Answer

You can use a subquery to get 16 rows with the most points:

If you want this per user, you would use window functions:

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