Objective
Write a SQL query to identify all project assignments where an employee is allocated to multiple projects with overlapping timeframes. The result should include the project ID, employee ID, start date, and end date of each such assignment. Ensure that the output is ordered by project ID and employee ID.
Additional information
- You are provided with a table named
assignmentswith the following columns:
| Column | Type | Description |
|---|---|---|
| project_id | INTEGER | The unique identifier for each project |
| employee_id | INTEGER | The unique identifier for each employee |
| start_date | DATE | The start date of the employee's assignment to the project |
| end_date | DATE | The end date of the employee's assignment to the project |
An employee has overlapping assignments if they are assigned to different projects where the date ranges intersect. Specifically, two assignments overlap if:
- The projects are different (
project_iddiffers). - The start date of one assignment is on or before the end date of the other assignment.
- The end date of one assignment is on or after the start date of the other assignment.
- The projects are different (
The query should return distinct records without duplicates.
Order the final result set first by
project_idin ascending order and then byemployee_idin ascending order.Assume that all dates are valid and
start_dateis always on or beforeend_datefor each assignment.
Examples
Input:
| assignments | |||
|---|---|---|---|
| employee_id | end_date | project_id | start_date |
| 101 | 2023-03-15 | 1 | 2023-01-01 |
| 101 | 2023-05-30 | 2 | 2023-03-01 |
| 102 | 2023-04-15 | 3 | 2023-02-01 |
| 102 | 2023-08-30 | 4 | 2023-06-01 |
| 103 | 2023-03-30 | 5 | 2023-01-15 |
| employee_id | end_date | project_id | start_date |
|---|---|---|---|
| 101 | 2023-03-15 | 1 | 2023-01-01 |
| 101 | 2023-05-30 | 2 | 2023-03-01 |
Code Environment
Sign in or try as guest to run your code.
X
Revolut
Accenture
Adobe
Google
LinkedIn
Samsung
Datadog
Wix
Dropbox
Meta
OpenAI
Hulu
Uber
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