27. Merge Employee and Department Records
Beginner Mode
Objective
Write a SQL query to find employees whose salaries are above the average salary of their respective departments. The departments should have more than 10 employees. For each qualifying employee, return their name, department name, salary, average salary of the department, and the count of employees in the department who earn more than $75,000. The results should be ordered by department name and salary in descending order.
Additional information
- Ensure that the departments have more than 10 employees.
- The average salary should be rounded to 2 decimal places.
- The query should also count how many employees in each department earn more than $75,000.
- The results should include columns: employee_name, department_name, salary, dept_avg_salary, and high_earners.
Constraints:
- You can assume that
- The
employeestable contains the columns:id,name,department_id, andsalary. - You can assume that
- The
departmentstable contains the columns:idandname. - The query should be efficient and handle large datasets within reasonable time limits.
Examples
Example 1:
Output:
Input:
| departments | |
|---|---|
| id | name |
| 1 | Engineering |
| 2 | Sales |
| 3 | Marketing |
| employees | |||
|---|---|---|---|
| department_id | id | name | salary |
| 1 | 1 | John | 82000 |
| 1 | 2 | Alice | 78000 |
| 1 | 3 | Bob | 95000 |
| 1 | 4 | Carol | 65000 |
| 1 | 5 | David | 72000 |
| 1 | 6 | Eve | 88000 |
| 1 | 7 | Frank | 69000 |
| 1 | 8 | Grace | 77000 |
| 1 | 9 | Henry | 81000 |
| 1 | 10 | Ivy | 73000 |
| 1 | 11 | Jack | 85000 |
| 2 | 12 | Kelly | 92000 |
| 2 | 13 | Liam | 83000 |
| 2 | 14 | Mike | 76000 |
| 2 | 15 | Nina | 89000 |
| 2 | 16 | Oscar | 71000 |
| 2 | 17 | Pam | 94000 |
| 2 | 18 | Quinn | 68000 |
| 2 | 19 | Ray | 86000 |
| 2 | 20 | Sara | 79000 |
| 2 | 21 | Tom | 91000 |
| 2 | 22 | Uma | 74000 |
| 2 | 23 | Victor | 87000 |
| department_name | dept_avg_salary | employee_name | high_earners | salary |
|---|---|---|---|---|
| Engineering | 78636.36 | Bob | 7 | 95000 |
| Engineering | 78636.36 | Eve | 7 | 88000 |
| Engineering | 78636.36 | Jack | 7 | 85000 |
| Engineering | 78636.36 | John | 7 | 82000 |
| Engineering | 78636.36 | Henry | 7 | 81000 |
| Sales | 82500 | Pam | 9 | 94000 |
| Sales | 82500 | Kelly | 9 | 92000 |
| Sales | 82500 | Tom | 9 | 91000 |
| Sales | 82500 | Nina | 9 | 89000 |
| Sales | 82500 | Victor | 9 | 87000 |
| Sales | 82500 | Ray | 9 | 86000 |
| Sales | 82500 | Liam | 9 | 83000 |
Code Environment
Sign in or try as guest to run your code.
Essential
SQL 0/33
Spark 0/20
Snowflake 0/22
Python 0/24
Need more practice in this area? Explore more questions →
Anthropic
Revolut
Accenture
Adobe
Google
LinkedIn
Samsung
Datadog
Wix
Dropbox
Meta
OpenAI
Hulu
Uber
X
DoorDash
Amazon
ActivisionBlizzard
Vercel
Crypto.Com
Zscaler
DeutscheBank
Apple
GoDaddy
GitLab
BMW
PayPal
Snowflake
AMD
Twilio
Atlassian
JPMorgan
NVIDIA
IBM
Databricks
Coinbase
Cisco
Robinhood
Twitter
Microsoft
Palantir
Netflix
VMware
Cloudflare
Stripe
Lyft
Salesforce
GitHub
Bloomberg
Airbnb
Walmart
SAP
HashiCorp
Instacart
Mastercard
Intel
Visa
Tesla