Skip to main content

Posts

Showing posts from July, 2024

SQL Problem - Find 3rd highest employee salary

SQL Problem - Find 3rd highest employee salary

Problem Statement Create an SQL query to retrieve all details of employees who receive the third-highest salary. Ensure the query returns all columns from the employees table. Additional Requirement: Do not use the LIMIT keyword in your query. Sample Input: Table: employees: Sample Output Solution: Approach 1: Using Sub Query: select * from employees where salary = ( select distinct ( salary ) from employees   order by salary desc limit 1 offset 2 ) The subquery finds the third-highest distinct salary by ordering the salaries in descending order, skipping the first two, and then selecting the next one. The main query then retrieves all employees who have this third-highest salary. Approach 2: Using Nested Inner Queries SELECT * FROM EMPLOYEES WHERE SALARY = (     SELECT MAX ( SALARY ) FROM EMPLOYEES WHERE SALARY < (         SELECT MAX ( SALARY ) FROM EMPLOYEES WHERE SALARY < (           ...