← 返回 netflix 的题目列表User Last Order Date and Order Count SQL Query
类型:online_judge
netflix
Third Question
Suppose you have an orders table with fields order_id, user_id, and order_date, and another users table with fields user_id and name. Write an SQL query to return each user's most recent order date and the number of orders. This information should be displayed in the order of username.
Input Format:
Two tables orders and users
Output Format:
Username, last order date, and order count.
Example:
Given the following data:
orders table:
| order_id | user_id | order_date | |----------|---------|------------| | 1 | 1 | 2022-01-01 | | 2 | 2 | 2022-01-05 | | 3 | 1 | 2022-02-05 |
users table:
| user_id | name | |---------|-------| | 1 | Alice | | 2 | Bob |
The query should return:
| name | last_order_date | order_count | |-------|-----------------|-------------| | Alice | 2022-02-05 | 2 | | Bob | 2022-01-05 | 1 |
Example
Input
orders with rows (order_id=1, user_id=1, order_date='2022-01-01'), (order_id=2, user_id=2, order_date='2022-01-05'), (order_id=3, user_id=1, order_date='2022-02-05') and users with rows (user_id=1, name='Alice'), (user_id=2, name='Bob')