Sunday, September 27, 2020

Find out second highest salary in Employee Table

 -- Second highest salary

SELECT MAX(salary) FROM Employee WHERE Salary NOT IN ( SELECT Max(Salary) FROM Employee);


This solution uses subquery to first exclude the maximum salary from the data set and then again finds maximum salary, which is effectively the second maximum salary from the Employee table.

Find out the name and salary of employee with 3rd highest salary

Using Postgres

CREATE TABLE EMPLOYEE (
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
SALARY REAL
)

INSERT INTO COMPANY (ID, NAME, SALARY,)
VALUES (2, 'Allen', 25000);

INSERT INTO EMPLOYEE (ID, NAME, SALARY)
VALUES (1, 'Rohit', 50000);

INSERT INTO EMPLOYEE (ID, NAME, SALARY)
VALUES (3, 'Krishna', 70000);

INSERT INTO EMPLOYEE (ID, NAME, SALARY)
VALUES (4, 'Ram', 80000);

INSERT INTO EMPLOYEE (ID, NAME, SALARY)
VALUES (5, 'Ganesh', 90000);

SELECT * FROM EMPLOYEE