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.
Saved automatically
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 returns one value even when several employees share it. If there are fewer than two distinct non-NULL salaries, this query returns no rows. That differs from a query returning one row containing NULL, so choose the behavior the API expects. State the supported database dialect and confirm window-function availability.
Related Questions in SQL & Relational Databases
What are Clustered and Non-Clustered Indexes in SQL, and when should you use each?
Intermediate level • 443 views
Explain Redis In-Memory Data Structures and Common Caching Patterns (Cache-Aside, Write-Through, Write-Behind).
Intermediate level • 63 views
How do INNER JOIN and LEFT JOIN differ when a customer has no orders?
Easy level • 59 views
A LEFT JOIN report counts one order for customers who have none. What caused it?
Medium level • 57 views
Explain SQL ACID properties and the differences between READ COMMITTED and REPEATABLE READ isolation levels.
Intermediate level • 55 views