Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the second highest grade point average In SQL Query

Tags:

sql

My Data Base is an SQL server And My Query Is Below

SELECT name, MAX(average) AS average
FROM Course
WHERE salary <> (SELECT MAX(salary) 
FROM Course);

I Want Get Second Highest Average in Class

like image 882
Maysam Razzaghi Avatar asked Sep 01 '21 12:09

Maysam Razzaghi


2 Answers

SELECT name, average AS average
FROM Course
WHERE average = (SELECT Top(2) MAX(salary) 
FROM Course)
like image 177
Maysam Razzaghi Avatar answered Sep 22 '22 00:09

Maysam Razzaghi


No dbms specified but for MySQL and PostgreSQL You can use the limit and offset to get the second highest grade. Remove the MAX aggregation on the average column.

SELECT name, average AS average
FROM Course
WHERE salary <> (SELECT MAX(salary) 
FROM Course)
ORDER BY average DESC
LIMIT 1,2;

In MSSQL > 2012 you can use OFFSET and FETCH

OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY;
like image 24
jared Avatar answered Sep 21 '22 00:09

jared