Write a SQL query to find the second highest salary from the table emp. (Column name – id, salary)
Assistant
To find the second highest salary from the table emp that has the columns id and salary, you can use a subquery with the MAX function or leverage the LIMIT clause with ORDER BY. Here are methods for achieving this, depending on the SQL dialect you are using:
Method 1: Using Subquery with MAX Function
This method works well with databases like MySQL, SQL Server, Oracle, PostgreSQL, etc. The idea is to find the maximum salary that is less than the maximum salary of the table.
User
If you have a 5-litre bucket and a 3-litre bucket, how would you measure exactly 4 litres ? data analyst interview question
Assistant
Solving this problem involves a step-by-step approach of filling and transferring water between the 5-litre bucket (Bucket A) and the 3-litre bucket (Bucket B) until you end up with exactly 4 litres. Here is how you can do it:
Fill the 5-litre bucket (Bucket A) to the top.
Bucket A: 5 litres
Bucket B: 0 litres
Pour water from the 5-litre bucket (Bucket A) into the 3-litre bucket (Bucket B) until Bucket B is full.
Bucket A: 2 litres (because 5 - 3 = 2)
Bucket B: 3 litres
Empty the 3-litre bucket (Bucket B).
Bucket A: 2 litres
Bucket B: 0 litres
Transfer the remaining 2 litres from the 5-litre bucket (Bucket A) to the 3-litre bucket (Bucket B).
Bucket A: 0 litres
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
SELECT MAX(salary) AS SecondHighestSalary
FROM emp
WHERE salary < (SELECT MAX(salary) FROM emp);
Method 2: Using LIMIT and OFFSET (for MySQL, PostgreSQL)
MySQL, PostgreSQL, and similar databases support the LIMIT clause with an OFFSET. This way, you can skip the highest salary and get the next one.
MySQL:
SELECT salary AS SecondHighestSalary
FROM emp
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
PostgreSQL:
SELECT salary AS SecondHighestSalary
FROM emp
ORDER BY salary DESC
FETCH FIRST 1 ROWS ONLY
OFFSET 1;
Method 3: Using Analytic Functions (for SQL Server, Oracle)
For SQL Server and Oracle, you can use the ROW_NUMBER() or the DENSE_RANK() function. ROW_NUMBER() would work if there are no ties at the top (or you want to ignore ties). DENSE_RANK() is a better choice if you want to handle ties properly.
Using DENSE_RANK():
Oracle:
SELECT salary AS SecondHighestSalary
FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM emp
)
WHERE rnk = 2;
SQL Server:
WITH RankedSalaries AS (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM emp
)
SELECT salary AS SecondHighestSalary
FROM RankedSalaries
WHERE rnk = 2;
Each of these methods allows you to retrieve the second highest salary from your employee table (emp). The best method to use largely depends on the capabilities and specific syntaxes of the database management system you are working with.
Bucket B: 2 litres
Fill the 5-litre bucket (Bucket A) to the top again.
Bucket A: 5 litres
Bucket B: 2 litres
Pour water from the 5-litre bucket (Bucket A) into the 3-litre bucket (Bucket B) just until Bucket B is full.
Bucket A will be left with exactly 4 litres because you can only add 1 more litre to Bucket B (since it already contains 2 litres), thus reducing Bucket A from 5 litres to 4 litres.
Bucket A: 4 litres
Bucket B: 3 litres (full)
So, by following these steps, you now have exactly 4 litres of water in the 5-litre bucket (Bucket A). This task is a common puzzle that tests problem-solving and logical thinking skills.