Wednesday, 7 October 2020

How to Find Nth Highest Salary using SQL Query in Postgres

Find Nth Highest Salary SQL Query in Postgres

SELECT * FROM beverage beverage1 WHERE (n) = (SELECT COUNT(DISTINCT(beverage2 .price)) FROM beverage beverage2
WHERE beverage1 .price <beverage2 .price);

n -> is an integer number 0 to n for finding nth highest salary . 

Data in beverage table 




Highest Salary SQL Query

SELECT * FROM beverage beverage1 WHERE (0) = (SELECT COUNT(DISTINCT(beverage2 .price)) FROM beverage beverage2
WHERE beverage1 .price < beverage2 .price);










Second  Highest Salary SQL Query

SELECT * FROM beverage beverage1 WHERE (1) = (SELECT COUNT(DISTINCT(beverage.price)) FROM beverage beverage2
WHERE beverage1 .price <beverage2 .price);











Third Highest SQL Query

SELECT * FROM beverage beverage1 WHERE (2) = (SELECT COUNT(DISTINCT(beverage2 .price)) FROM beverage beverage2
WHERE beverage1.price <  beverage2.price);










No comments:

Post a Comment