11. Self-Join to Identify Missing Supervisors
Beginner Mode
Objective
Write a SQL query to identify employees whose supervisors are not present in the employee database. The query should return the employee's ID, name, and their supervisor's ID, sorted by the employee's ID.
Additional information
- You have a table named
employeeswith the following columns:employee_id,name, andsupervisor_id. - An employee with a
supervisor_idequal tonullmeans they do not have a supervisor. - The output should only include employees where the
supervisor_idis not present in theemployee_idcolumn of any record. - The output should be sorted by
employee_idin ascending order.
Examples
Example 1:
Output:
Input:
| employees | ||
|---|---|---|
| employee_id | name | supervisor_id |
| 1 | John Smith | null |
| 2 | Mary Johnson | 1 |
| 3 | James Brown | 7 |
| 4 | Patricia Davis | 1 |
| 5 | Robert Wilson | 9 |
| 6 | Lisa Anderson | 2 |
| employee_id | employee_name | supervisor_id |
|---|---|---|
| 3 | James Brown | 7 |
| 5 | Robert Wilson | 9 |
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 →
Meta
Revolut
Accenture
Adobe
Google
LinkedIn
Samsung
Datadog
Wix
Dropbox
OpenAI
Hulu
Uber
X
DoorDash
Anthropic
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