← 返回 amazon 的题目列表SQL: Use LEFT JOIN to Find Missing Relationships
类型:online_judge
Given two tables:
users(user_id INT, signup_date DATE)
orders(order_id INT, user_id INT, order_date DATE, amount DECIMAL(10,2))
Write an SQL query to return users who signed up between 2025-01-01 and 2025-01-31 but placed no orders in the same period. Output:
user_id
signup_date
Requirements:
Only consider users whose signup_date is within the period.
If a user has any order in the period, they must be excluded.
Order by user_id ascending.
Data scale: up to 10M rows per table.
Validation cases:
Signed up in range and no orders -> included
Signed up in range and has an order in range -> excluded
Signed up out of range -> excluded
Has orders outside the range but none inside -> still included.
Example
Input
users:
(1,'2025-01-03')
(2,'2025-01-10')
(3,'2024-12-31')
orders:
(101,2,'2025-01-12',10.0)
(102,1,'2025-02-01',20.0)
Output
1 2025-01-03