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 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.
Related Questions in SQL & Relational Databases
What are Clustered and Non-Clustered Indexes in SQL, and when should you use each?
Intermediate level • 388 views
Explain SQL ACID properties and the differences between READ COMMITTED and REPEATABLE READ isolation levels.
Intermediate level • 3 views
What is Database Indexing, how does a B+ Tree index work, and when should you avoid creating indexes?
Intermediate level • 3 views
Explain Database Normalization from 1NF through BCNF with real-world anomaly examples.
Intermediate level • 3 views
Explain Redis In-Memory Data Structures and Common Caching Patterns (Cache-Aside, Write-Through, Write-Behind).
Intermediate level • 2 views