SQL Boy
TutorialsPlayground

Format & Validate

SQL FormatterSQL MinifierSyntax Validator

Convert

JSON to SQLCSV to SQLSQL to JSONRegex to SQLExcel to SQLSQL Dialect Convertersoon

Visualize

ER Diagram GeneratorSQL Schema Diff

Generate

SQL Mock Data Generator

Analyze

SQL Query ExplainerSQL Query Analyzer

Workflow hubs

FormattingConversionSchemaAnalysis
View all tools
Daily ChallengeInterviewsCheat SheetBlog

© 2026 SQL Boy. Built for developers.

ContactPrivacyTermsAI Usage PolicyEditorial StandardsAboutRSS Feed

New conversation

What can I help with?

Ask about this page, get a hint on a challenge, or explore SQL concepts.

I know what's on this page and can give answers grounded in SQL Boy content.

Current page

Second Highest Salary

/challenges/second-highest-salary

Usage status will load after login
SQL Boy
TutorialsPlayground

Format & Validate

SQL FormatterSQL MinifierSyntax Validator

Convert

JSON to SQLCSV to SQLSQL to JSONRegex to SQLExcel to SQLSQL Dialect Convertersoon

Visualize

ER Diagram GeneratorSQL Schema Diff

Generate

SQL Mock Data Generator

Analyze

SQL Query ExplainerSQL Query Analyzer

Workflow hubs

FormattingConversionSchemaAnalysis
View all tools
Daily ChallengeInterviewsCheat SheetBlog
SQL Challenges/Second Highest Salary
Challenge DetailMediumReviewed 2026-04-20

Second Highest Salary

Find the second highest salary from the employees table.

SubqueriesWindow FunctionsAggregation

This challenge now meets the current minimum standard for sitemap inclusion and independent indexing.

2025-12-10Medium

Second Highest Salary

Find the second highest salary from the employees table.

Available Tables:
SQL Editor
Loading...

What This Challenge Is Testing

This is one of the most common SQL interview questions because it forces you to think about distinct value tiers instead of raw row counts. The same pattern appears in leaderboards, pricing analysis, and compensation reports where the business question is about the second highest unique value, not just the second row after sorting.

Topic Tags

SubqueriesWindow FunctionsAnalytics

Schema At A Glance

employees
idsalary

Solution Approaches

01

Nested MAX with a subquery

SELECT
  MAX(salary) AS second_highest
FROM employees
WHERE salary < (
  SELECT MAX(salary)
  FROM employees
)

The inner query finds the global maximum salary. The outer query then restricts the table to salaries below that maximum and takes the maximum of the remaining values. This is compact, easy to explain in an interview, and works well when you only need the second distinct tier.

02

DENSE_RANK for explicit value tiers

WITH ranked_salaries AS (
  SELECT
    salary,
    DENSE_RANK() OVER (ORDER BY salary DESC) AS salary_rank
  FROM employees
)
SELECT
  MAX(salary) AS second_highest
FROM ranked_salaries
WHERE salary_rank = 2

DENSE_RANK makes the uniqueness rule explicit by assigning the same rank to tied salary values. This is more verbose than the nested MAX version, but it scales naturally if the follow-up asks for the third, fourth, or top N distinct salary tiers.

Common Pitfalls

The most common mistake is using ORDER BY salary DESC LIMIT 1 OFFSET 1 on the raw table. That returns the second row, not the second distinct salary, so duplicate top salaries break the answer. Another mistake is forgetting that the correct result may be NULL when there is no second unique salary.

Frequently Asked Questions

Why is DENSE_RANK better than ROW_NUMBER here?

ROW_NUMBER numbers physical rows, so duplicate top salaries would get rank 1 and 2 even though they are the same salary value. DENSE_RANK collapses ties into the same tier, which matches the business meaning of second highest distinct salary.

What happens if every employee has the same salary?

There is no second distinct salary. The nested MAX approach returns NULL because the filtered set is empty, which is the correct result for this question shape.

Concept Guides

Use these guides when you want the fuller explanation behind the SQL pattern this challenge is testing.

SQL Subqueries Explained: Queries Within Queries

Unlock the power of nested queries. Learn when to use subqueries in SELECT, WHERE, and FROM clauses with practical, runnable examples.

Understanding SQL Window Functions: A Visual Guide

Master ROW_NUMBER, RANK, and running totals with interactive visualizations. See exactly how window functions process your data row by row.

Ranking Data with SQL: RANK, DENSE_RANK, and ROW_NUMBER Explained

Building leaderboards, finding top performers, or paginating results? Master the three SQL ranking functions and understand exactly when to use each one.

Topic Hubs

If this problem exposed a deeper weakness, use a topic hub to move through a curated cluster instead of random article hopping.

Advanced SQL

Advanced query patterns

Use this path when you are moving beyond beginner SELECT queries into CTEs, window functions, and conditional logic.

Practice Path

If you want the rotating practice flow, open this same question from the daily stream. If you want interview-style packaging, continue into the interview pool after this rep.

Back to ChallengesOpen In DailyBrowse Interviews

Related Challenges

Rank ScoresMedium

Assign tied scores the same rank while keeping the next rank consecutive with no gaps.

Customers Who Bought All ProductsMedium

Return only the customers whose distinct purchased product set covers every product listed in the catalog.

Trips and UsersHard

Compute daily cancellation rate after excluding any trip where either the client or driver is banned.

© 2026 SQL Boy. Built for developers.

ContactPrivacyTermsAI Usage PolicyEditorial StandardsAboutRSS Feed