Second Highest Value
An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- easy
- Seniority
- L3
- Asked in
- Calix
Problem
Find the second-highest metric value across the entire employee metrics table. Multiple employees might share the top spot; the second-highest is the next unique value below them. Return a single number.
Almost the top. Not quite.
Tables
employee_metrics
Columns: metric_id INTEGER, department TEXT, metric_name TEXT, metric_value REAL, fiscal_quarter TEXT, fiscal_year INTEGER
| metric_id | department | metric_name | metric_value | fiscal_quarter | fiscal_year |
|---|---|---|---|---|---|
| 141 | Product | attrition_rate | 7.3 | Q2 | 2025 |
| 182 | Design | avg_tenure_months | 14.6 | Q3 | 2026 |
| 223 | Marketing | satisfaction_score | 21.9 | Q4 | 2024 |
| 264 | Sales | training_hours | 29.2 | Q1 | 2025 |
| 305 | HR | promotions | 36.5 | Q2 | 2026 |
| 346 | Finance | open_positions | 43.8 | Q3 | 2024 |
| 387 | Legal | time_to_hire_days | 51.1 | Q4 | 2025 |
| 428 | Operations | diversity_ratio | 58.4 | Q1 | 2026 |
| 469 | Data | remote_percentage | 65.7 | Q2 | 2024 |
| 510 | Engineering | headcount | 73 | Q3 | 2025 |
Expected output
| metric_value |
|---|
| 98.6 |
Worked solution and explanation
Why this problem exists in real interviews
This challenge asks you to apply DENSE_RANK for gapless ranking to the employee_metrics table, simulating a real analytics workflow. Pay attention to the metric_value column as they drive the aggregation and output.
Break down the requirements
Step 1: Apply the WHERE filter
Filter rows before any aggregation. This ensures only qualifying data enters the computation, keeping the result correct and the scan minimal.
Step 2: Rank results with a window function
DENSE_RANK() assigns consecutive ranks without gaps. Unlike ROW_NUMBER(), tied values share the same rank, which matters when the prompt asks for ties.
Step 3: Use a subquery to find the reference value
The scalar subquery computes a single value (like the maximum) that the outer query filters against. This avoids a self-join.
The solution
SELECT metric_value
FROM (SELECT DISTINCT metric_value, DENSE_RANK() OVER (ORDER BY metric_value DESC) AS rnk FROM employee_metrics) ranked
WHERE rnk = 2Common follow-up questions
- What would happen to your result if `employee_metrics.metric_value` contained duplicate values that you did not expect? (Tests whether the candidate considers data quality issues in `metric_value` and uses DISTINCT or deduplication where needed.)
- The `metric_value` column in `employee_metrics` is heavily skewed toward a few popular values. How would data skew affect parallel execution of your query? (Tests understanding of skew in `employee_metrics.metric_value` and its impact on distributed query performance.)
- What is the default window frame for your window function, and would explicitly setting ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW change anything? (Tests knowledge of implicit vs explicit window frame specifications.)