Question

    Which of the following query will return the third-highest salary from an Employee table in SQL?

    A SELECT salary FROM Employee ORDER BY salary DESC LIMIT 2, 1; Correct Answer Incorrect Answer
    B SELECT MAX(salary) FROM Employee WHERE salary NOT IN (SELECT salary FROM Employee ORDER BY salary DESC LIMIT 2); Correct Answer Incorrect Answer
    C SELECT DISTINCT salary FROM Employee ORDER BY salary DESC LIMIT 3, 1; Correct Answer Incorrect Answer
    D SELECT salary FROM Employee WHERE salary = (SELECT salary FROM Employee ORDER BY salary DESC LIMIT 1 OFFSET 2); Correct Answer Incorrect Answer
    E SELECT salary FROM Employee ORDER BY salary LIMIT 3; Correct Answer Incorrect Answer

    Solution

    To get the third-highest salary, the query SELECT salary FROM Employee ORDER BY salary DESC LIMIT 1 OFFSET 2; fetches the salary value after skipping the top two highest salaries. The outer query ensures that we only get the third-highest salary. Why Other Options are Wrong: a) Incorrect usage of LIMIT; it should be OFFSET 2 instead of LIMIT 2, 1. b) This would return multiple rows or result in an error. c) DISTINCT is unnecessary and could result in wrong data if multiple employees have the same salary. e) This query would return the first three salaries in ascending order, not the third-highest.

    Practice Next