5. Join Employees and Departments
Beginner Mode
Objective
Write an SQL query to retrieve a list of employees who earn more than $50,000. For each qualifying employee, display their ID, first name, last name, job title, hire date, and the name of their department. If an employee is not assigned to any department, the department name should be shown as NULL. The resulting list should be ordered by the hire date in descending order, showing the most recently hired employees first.
Additional information
Tables:
The
employeestable:Column Type Description employee_id Integer Unique identifier for each employee first_name String Employee's first name last_name String Employee's last name department_id Integer Identifier for the department the employee belongs to. This can be NULLif the employee is not assigned to any departmentjob_title String Title of the employee's job salary Integer Employee's salary hire_date Date Date the employee was hired The
departmentstable:Column Type Description department_id Integer Unique identifier for each department department_name String Name of the department location String Location of the department Requirements:
- Use a
LEFT JOINto combineemployeesanddepartmentsbased ondepartment_id. - Filter the employees to include only those with a
salarygreater than 50000. - The
department_nameshould appear asNULLfor employees without an associated department. - Order the results by
hire_datein descending order.
- Use a
Examples
Example 1:
Output:
Input:
| departments | ||
|---|---|---|
| department_id | department_name | location |
| 1 | Engineering | New York |
| 2 | Project Management | Boston |
| 3 | Marketing | Chicago |
| employees | ||||||
|---|---|---|---|---|---|---|
| department_id | employee_id | first_name | hire_date | job_title | last_name | salary |
| 1 | 1 | John | 2020-01-15 | Senior Developer | Smith | 85000 |
| 2 | 2 | Mary | 2021-03-20 | Project Manager | Johnson | 75000 |
| null | 3 | Peter | 2022-06-10 | Consultant | Brown | 65000 |
| 1 | 4 | Sarah | 2021-09-01 | Developer | Davis | 45000 |
| department_name | employee_id | first_name | hire_date | job_title | last_name |
|---|---|---|---|---|---|
| null | 3 | Peter | 2022-06-10 | Consultant | Brown |
| Project Management | 2 | Mary | 2021-03-20 | Project Manager | Johnson |
| Engineering | 1 | John | 2020-01-15 | Senior Developer | Smith |
Code Environment
Sign in or try as guest to run your code.
Essential
SQL 0/33
Git 0/15
Spark 0/20
Snowflake 0/22
Python 0/24
Need more practice in this area? Explore more questions →
Adobe
TCS
X
Accenture
Google
LinkedIn
Samsung
Datadog
Wix
Dropbox
Meta
OpenAI
Hulu
Uber
DoorDash
Anthropic
Amazon
ActivisionBlizzard
Vercel
Crypto.Com
Zscaler
DeutscheBank
Apple
GoDaddy
BMW
PayPal
Snowflake
AMD
Twilio
Atlassian
JPMorgan
NVIDIA
IBM
Databricks
Coinbase
Cisco
Robinhood
Twitter
Microsoft
Palantir
Netflix
VMware
Cloudflare
Stripe
Capital One
Splunk
Intel
SAP
Tesla
GitHub
JaneStreet
Bloomberg
Salesforce
Elastic
CGI
UBS
GitLab
Ubisoft
Slack
Nintendo
EY
Kayak
Lyft
Airbnb
Walmart
Revolut
Visa
Okta
HashiCorp
Instacart
Mastercard