SQL interview Question | Second Highest Salary – Method 1

Question

Write a SQL query to get second highest query using sub query

in progress 0
TheDataMonk 55 years 33 Answers 3566 views Grand Master 0

Answers ( 33 )

  1. Sorry it a private answer.

    10

    SELECT Name, MAX(salary) AS salary FROM Table_name WHERE salary < (SELECT MAX(salary) FROM Table_name);

  2. SELECT MAX(salary)
    FROM(SELECT salary
    FROM table
    WHERE salary < Max(Salary)) as t;

  3. SELECT Name, MAX(salary)
    AS salary
    FROM Table
    WHERE salary < (SELECT MAX(salary)
    FROM
    Table);

  4. SELECT MAX(Salary) FROM Table WHERE Salary < ( SELECT Max(Salary) FROM Table);

  5. SELECT MAX(Salary) FROM Table WHERE Salary < ( SELECT Max(Salary) FROM Table);

  6. Select max(salary) from table where
    Salary < ( select max(salary) from table)

    1

    Select
    From

    where <
    (select from
    )
    Order by DESC
    Limit 1
  7. SELECT MAX(salary) AS salary FROM Table_name WHERE salary != (SELECT MAX(salary) FROM Table_name);

  8. SELECT MAX(salary)
    FROM tablename
    WHERE
    Salary < ( SELECT max(salary) tablename)

  9. select * from (select *,dense_rank() over (order by salary desc) as salary_rank from table ) where salary_rank=2;

  10. select min(salary) from
    (select distinct top 3 salary
    from employees , order by salary desc) as Result
    Order by salary;

  11. SELECT salary
    FROM
    (SELECT
    salary
    FROM table
    ORDER BY salary ASC
    LIMIT 2
    ) AS a
    ORDER BY salary DESC
    LIMIT 1

  12. SELECT MAX(salary)
    FROM table
    WHERE salary < ( select MAX(salary) from table)

  13. select max(salary) from table where salary< (select max(salary) from table);

  14. SELECT Name, MAX(salary)
    AS salary
    FROM Table
    WHERE salary < (SELECT MAX(salary)
    FROM
    Table);

  15. select max(salary) from table
    where salary < (select max(salary) from table);

  16. Consider the table name to be Employee

    SELECT MAX(salary) as Second_Highest_Salary
    FROM Employee
    WHERE Salary < ( SELECT MAX(salary) FROM Employee);

  17. select MAX(salary),
    from table_name ,
    where salary < (
    select MAX(salary),
    from table_name ,
    ) ;

  18. SELECT MAX(salary)
    FROM table
    WHERE salary <(
    SELECT MAX(salary)
    FROM table)

  19. 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;

  20. 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)

  21. SELECT MAX(Salary) FROM TABLE
    WHERE Salary < (SELECT MAX(SALARY) FROM TABLE)

  22. SELECT MIN(Salary) FROM (SELECT Salary
    FROM emp
    ORDER BY Salary DESC
    LIMIT 2)

  23. ELECT MAX(Salary) FROM TABLE
    WHERE Salary < (SELECT MAX(SALARY) FROM TABLE)

  24. SELECT MAX(Salary) From table WHERE Salary < ( SELECT Max(Salary) FROM table)

  25. SELECT MAX(salary)
    FROM (SELECT salary FROM table WHERE (salary<MAX(salary)));

  26. SELECT MIN(salary)
    FROM table
    WHERE salary IN (
    SELECT DISTINCT(SALARY)
    FROM
    table
    ORDER BY salary DESC
    LIMIT 2
    )

    0

    Select min(salary) from table where salary in
    (Select salary from table order by salary desc limit 2)

    0

    SELECT MAX(salary) AS salary
    FROM employee
    WHERE salary < (SELECT MAX(salary)
    FROM employee);

  27. Select Max(Salary) from Employee
    where Salary <
    (Select Max(Salary) from Employee)

  28. select salary from table_name as t1
    where 1 = (select count(distinct salary) from table_name as t2
    where t2.salary>t1.salary)

  29. SELECT MAX(SALARY) FROM TABLE WHERE SALARY < ( SELECT MAX(SALARY) FROM TABLE)

Leave an answer

Browse
Browse