Verified Jobs Live
SQL & Relational Databases Medium Level ~3 min read

Write a query for the second-highest distinct non-NULL salary. What happens if it does not exist?

Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Model Answer & Deep-Dive Explanation

Rank distinct salary values in descending order with DENSE_RANK and select rank 2. Filtering NULL before ranking avoids dialect-dependent placement of NULL salaries. DISTINCT on the final selected salary retu s one value even when several employees share it. If there are fewer than two distinct non-NULL salaries, this query retu s no rows. That differs from a query retu ing one row containing NULL, so choose the behavior the API expects. State the supported database dialect and confirm window-function availability.