**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;
Architectural Logic & Trade-offs:
Scalability: For tables >10M rows, ensure salary is indexed if using subquery; DENSE_RANK may require full sort—consider partition pruning if filtered by dept/region.
Edge Cases: Empty table, single row, all identical salaries—DENSE_RANK returns NULL for rk=2 when none exists.
Want feedback on your answer?
Paste your answer to this question and our AI Coach scores it, finds gaps, and shows you the FAANG-level version.
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 →SQL joins are the most tested topic in data engineering interviews. Most candidates know INNER and LEFT JOIN but stumble on CROSS APPLY, anti-joins, and self-joins. This guide covers them all with real interview patterns.
13 min read →See exactly why most candidates fail this question — and the FAANG-level answer that gets offers.
Paste your answer and get instant AI feedback with a FAANG-level improved version.
Analyze My Answer — FreeAccording 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.