**Using subquery with MAX**: ```sql SELECT MAX(salary) AS second_highest FROM employee WHERE salary < (SELECT MAX(salary) FROM employee); ``` **Using LIMIT/OFFSET** (MySQL, PostgreSQL): ```sql SELECT DISTINCT salary FROM employee ORDER BY salary DESC LIMIT 1 OFFSET 1; ```...
Pro-Move: Clarify 'second-highest' with ties (DENSE_RANK vs RANK). Red Flag: Using OFFSET 1 without DISTINCT when duplicates exist—wrong result.
This medium-level SQL question appears frequently in data engineering interviews at companies like Accenture, BCG, Cognizant, and 3 others. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (partition, sql, window) will help you answer variations of this question confidently.
Break this problem into components. Identify the core trade-offs involved, then walk the interviewer through your reasoning step by step. Demonstrate awareness of edge cases and production considerations - this is what separates good answers from great ones. The expert answer includes a code example that demonstrates the implementation pattern.
Using subquery with MAX:
SELECT MAX(salary) AS second_highest
FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee);
Using LIMIT/OFFSET (MySQL, PostgreSQL):
SELECT DISTINCT salary
FROM employee
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
Using DENSE_RANK (ANSI SQL, most robust):
SELECT salary
FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rk
FROM employee
) t
WHERE rk = 2;
This answer is partially locked
Unlock the full expert answer with code examples and trade-offs
Practice real interviews with AI feedback, track progress, and get interview-ready faster.
Pro starts at $19/mo - cancel anytime
Trusted by 10,000+ aspiring data engineers
Practice the 44 most asked data engineering questions at Incedo. Covers Spark/Big Data, SQL, Behavioral and more.
8 min read →Practice the 36 most asked data engineering questions at BCG. Covers Spark/Big Data, SQL, Cloud/Tools and more.
8 min read →Practice the 33 most asked data engineering questions at Accenture. Covers SQL, Spark/Big Data, Behavioral and more.
8 min read →Master 487 sql questions with expert answers. Real questions from 97+ companies.
60 min read →Window functions and CTEs are the #1 tested SQL topics at Amazon, Google, and Databricks. This guide covers every pattern you'll face with production-ready answers.
18 min read →According to DataEngPrep.tech, this is one of the most frequently asked SQL interview questions, reported at 6 companies. DataEngPrep.tech maintains a curated database of 1,863+ real data engineering interview questions across 7 categories, verified by industry professionals.