Test your knowledge of SQL techniques for finding the median salary in a database table, with questions covering window functions, aggregation, and practical query logic. This quiz is ideal for those preparing for interviews or enhancing SQL analytics skills.
Understanding the Median in Salaries
What does the median salary represent in a data set of employee salaries, for example [40,000, 45,000, 50,000]?
- The average of all salary values
- The middle salary value when all are sorted
- The highest salary in the list
- The sum of all salaries divided by 2
PERCENTILE_CONT Usage
Which SQL function computes a specific percentile, such as the median salary, in a modern SQL Server version?
- PERSENTILE_CNT
- GROUP_BY
- COUNT_DISTINCT
- PERCENTILE_CONT
OFFSET-FETCH For Median
When finding the median salary using OFFSET-FETCH, why is sorting the salary column important?
- Because it deletes duplicate values
- Because it eliminates NULL values
- Because the position of the median depends on sorted order
- Because it increases the total count
Traditional Median Calculation
In older SQL versions, you calculate the median salary by assigning row numbers in both ascending and descending order. What is this method primarily used for?
- Summing all salary values
- Sorting employees alphabetically
- Counting the total number of employees
- Identifying the rows in the middle of the sorted dataset
Handling Even Number of Rows
If an employee table has an even number of salary rows, like [40,000, 45,000, 50,000, 60,000], how is the median salary typically calculated?
- By selecting any random salary
- By picking the lowest salary
- By averaging the two middle salary values
- By summing the first and last salaries
Window Functions and Median
Which SQL clause is necessary to assign a unique sequence to each row, such as when using ROW_NUMBER() to find the median salary?
- ORDER BY
- HAVING
- SUM
- DISTINCT
GROUP BY in Median Calculation
Why might you use 'GROUP BY (SELECT NULL)' in a PERCENTILE_CONT query for median salary?
- To filter out NULL salaries from the results
- To return a single aggregated row without grouping by any column
- To count unique salary values
- To group results by employee department
Identifying Data Skew
Why is the median salary preferred over the average in a dataset with a few very high salaries and many low salaries?
- Because it ignores all duplicate salaries
- Because it always gives a higher result
- Because databases process median faster than average
- Because the median is not affected by extreme outliers
Basic Table Definition
Which of the following columns would NOT be necessary in an employee salary table used for median calculations?
- hire_date
- salary
- department
- employee_id
AVG Function in Median Queries
In the traditional median calculation, why is AVG() used in the final SELECT when there could be two middle salary values?
- To select the highest salary
- Because AVG always returns the median automatically
- To count the number of salaries
- To calculate the mean of the two middle salaries for even row counts