SQL interview Question | Second Highest Salary – Method 1
Question
Write a SQL query to get second highest query using sub query
in progress
0
SQL
55 years
33 Answers
3566 views
Grand Master 0
Answers ( 33 )
Sorry it a private answer.
SELECT Name, MAX(salary) AS salary FROM Table_name WHERE salary < (SELECT MAX(salary) FROM Table_name);
SELECT MAX(salary)
FROM(SELECT salary
FROM table
WHERE salary < Max(Salary)) as t;
SELECT Name, MAX(salary)
AS salary
FROM Table
WHERE salary < (SELECT MAX(salary)
FROM
Table);
SELECT MAX(Salary) FROM Table WHERE Salary < ( SELECT Max(Salary) FROM Table);
SELECT MAX(Salary) FROM Table WHERE Salary < ( SELECT Max(Salary) FROM Table);
Select max(salary) from table where
Salary < ( select max(salary) from table)
Select
From
(select from
Order by DESC
Limit 1
SELECT MAX(salary) AS salary FROM Table_name WHERE salary != (SELECT MAX(salary) FROM Table_name);
SELECT MAX(salary)
FROM tablename
WHERE
Salary < ( SELECT max(salary) tablename)
select * from (select *,dense_rank() over (order by salary desc) as salary_rank from table ) where salary_rank=2;
select min(salary) from
(select distinct top 3 salary
from employees , order by salary desc) as Result
Order by salary;
SELECT salary
FROM
(SELECT
salary
FROM table
ORDER BY salary ASC
LIMIT 2
) AS a
ORDER BY salary DESC
LIMIT 1
SELECT MAX(salary)
FROM table
WHERE salary < ( select MAX(salary) from table)
select max(salary) from table where salary< (select max(salary) from table);
SELECT Name, MAX(salary)
AS salary
FROM Table
WHERE salary < (SELECT MAX(salary)
FROM
Table);
select max(salary) from table
where salary < (select max(salary) from table);
Consider the table name to be Employee
SELECT MAX(salary) as Second_Highest_Salary
FROM Employee
WHERE Salary < ( SELECT MAX(salary) FROM Employee);
select MAX(salary),
from table_name ,
where salary < (
select MAX(salary),
from table_name ,
) ;
SELECT MAX(salary)
FROM table
WHERE salary <(
SELECT MAX(salary)
FROM table)
With SubQuery:
SELECT salary from
(SELECT salary FROM Employee ORDER BY salary DESC LIMIT 2)
ORDER BY salary ASC LIMIT 1;
Without SubQuery:
SELECT salary
FROM Employee
ORDER BY salary DESC
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY;
Let us say we have an employee table with two columns namely ID, Salary. I would be writing a generalized query below for finding out the Nth highest salary. N could be anything 1,2,3…..
select salary from employee e1 where N – 1 = (select count(distinct salary) from employee e2 where e2.salary > e1.salary)
SELECT MAX(Salary) FROM TABLE
WHERE Salary < (SELECT MAX(SALARY) FROM TABLE)
SELECT MIN(Salary) FROM (SELECT Salary
FROM emp
ORDER BY Salary DESC
LIMIT 2)
ELECT MAX(Salary) FROM TABLE
WHERE Salary < (SELECT MAX(SALARY) FROM TABLE)
SELECT MAX(Salary) From table WHERE Salary < ( SELECT Max(Salary) FROM table)
SELECT MAX(salary)
FROM (SELECT salary FROM table WHERE (salary<MAX(salary)));
SELECT MIN(salary)
FROM table
WHERE salary IN (
SELECT DISTINCT(SALARY)
FROM
table
ORDER BY salary DESC
LIMIT 2
)
Select min(salary) from table where salary in
(Select salary from table order by salary desc limit 2)
SELECT MAX(salary) AS salary
FROM employee
WHERE salary < (SELECT MAX(salary)
FROM employee);
Select Max(Salary) from Employee
where Salary <
(Select Max(Salary) from Employee)
select salary from table_name as t1
where 1 = (select count(distinct salary) from table_name as t2
where t2.salary>t1.salary)
SELECT MAX(SALARY) FROM TABLE WHERE SALARY < ( SELECT MAX(SALARY) FROM TABLE)