intermediateTop 30 Scenario-Based Questions

How do you efficiently find the median salary in a table?

PostgreSQL: SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) AS median_salary FROM employees; Or using window function: SELECT DISTINCT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) AS median FROM employees; Cross-database approach: SELECT AVG(salary) AS median FROM (SELECT salary, ROW_NUMBER() OVER (ORDER BY salary) AS rn, COUNT(*) OVER () AS cnt FROM employees) t WHERE rn IN (FLO

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.

How do you efficiently find the median salary in a table?

Next Step

Continue to Design a database schema for an e-commerce platform (customers, products, orders, categories, reviews).← Back to all SQL questions