楼主: Myron2017
跳转到指定楼层
上一主题 下一主题
收起左侧

刷题记录帖子

🔗
 楼主| Myron2017 2026-11-23 12:21:36 | 只看该作者
全局:
3118. Friday Purchase III
Medium
Topics
SQL Schema
Pandas Schema
Table: Purchases

+---------------+------+
| Column Name   | Type |
+---------------+------+
| user_id       | int  |
| purchase_date | date |
| amount_spend  | int  |
+---------------+------+
(user_id, purchase_date, amount_spend) is the primary key (combination of columns with unique values) for this table.
purchase_date will range from November 1, 2023, to November 30, 2023, inclusive of both dates.
Each row contains user_id, purchase_date, and amount_spend.
Table: Users

+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id     | int  |
| membership  | enum |
+-------------+------+
user_id is the primary key for this table.
membership is an ENUM (category) type of ('Standard', 'Premium', 'VIP').
Each row of this table indicates the user_id, membership type.
Write a solution to calculate the total spending by Premium and VIP members on each Friday of every week in November 2023.  If there are no purchases on a particular Friday by Premium or VIP members, it should be considered as 0.

Return the result table ordered by week of the month,  and membership in ascending order.

The result format is in the following example.



Example:

Input:

Purchases table:

+---------+---------------+--------------+
| user_id | purchase_date | amount_spend |
+---------+---------------+--------------+
| 11      | 2023-11-03    | 1126         |
| 15      | 2023-11-10    | 7473         |
| 17      | 2023-11-17    | 2414         |
| 12      | 2023-11-24    | 9692         |
| 8       | 2023-11-24    | 5117         |
| 1       | 2023-11-24    | 5241         |
| 10      | 2023-11-22    | 8266         |
| 13      | 2023-11-21    | 12000        |
+---------+---------------+--------------+
Users table:

+---------+------------+
| user_id | membership |
+---------+------------+
| 11      | Premium    |
| 15      | VIP        |
| 17      | Standard   |
| 12      | VIP        |
| 8       | Premium    |
| 1       | VIP        |
| 10      | Standard   |
| 13      | Premium    |
+---------+------------+
Output:

+---------------+-------------+--------------+
| week_of_month | membership  | total_amount |
+---------------+-------------+--------------+
| 1             | Premium     | 1126         |
| 1             | VIP         | 0            |
| 2             | Premium     | 0            |
| 2             | VIP         | 7473         |
| 3             | Premium     | 0            |
| 3             | VIP         | 0            |
| 4             | Premium     | 5117         |
| 4             | VIP         | 14933        |
+---------------+-------------+--------------+
        
Explanation:

During the first week of November 2023, a transaction occurred on Friday, 2023-11-03, by a Premium member amounting to $1,126. No transactions were made by VIP members on this day, resulting in a value of 0.
For the second week of November 2023, there was a transaction on Friday, 2023-11-10, and it was made by a VIP member, amounting to $7,473. Since there were no purchases by Premium members that Friday, the output shows 0 for Premium members.
Similarly, during the third week of November 2023, no transactions by Premium or VIP members occurred on Friday, 2023-11-17, which shows 0 for both categories in this week.
In the fourth week of November 2023, transactions occurred on Friday, 2023-11-24, involving one Premium member purchase of $5,117 and VIP member purchases totaling $14,933 ($9,692 from one and $5,241 from another).
Note: The output table is ordered by week_of_month and membership in ascending order.
  1. # Write your MySQL query statement below
  2. SELECT
  3.     f.week_of_month,
  4.     m.membership,
  5.     COALESCE(SUM(p.amount_spend), 0) AS total_amount
  6. FROM (
  7.     SELECT 1 AS week_of_month, DATE '2023-11-03' AS fri
  8.     UNION ALL SELECT 2, DATE '2023-11-10'
  9.     UNION ALL SELECT 3, DATE '2023-11-17'
  10.     UNION ALL SELECT 4, DATE '2023-11-24'
  11. ) AS f
  12. CROSS JOIN (
  13.     SELECT 'Premium' AS membership
  14.     UNION ALL SELECT 'VIP'
  15. ) AS m
  16. LEFT JOIN Users u
  17.     ON u.membership = m.membership
  18. LEFT JOIN Purchases p
  19.     ON p.user_id = u.user_id
  20.    AND p.purchase_date = f.fri
  21. GROUP BY f.week_of_month, m.membership
  22. ORDER BY f.week_of_month, m.membership;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:22:24 | 只看该作者
全局:
3124. Find Longest Calls
Medium
Topics
SQL Schema
Pandas Schema
Table: Contacts

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| first_name  | varchar |
| last_name   | varchar |
+-------------+---------+
id is the primary key (column with unique values) of this table.
id is a foreign key (reference column) to Calls table.
Each row of this table contains id, first_name, and last_name.
Table: Calls

+-------------+------+
| Column Name | Type |
+-------------+------+
| contact_id  | int  |
| type        | enum |
| duration    | int  |
+-------------+------+
(contact_id, type, duration) is the primary key (column with unique values) of this table.
type is an ENUM (category) type of ('incoming', 'outgoing').
Each row of this table contains information about calls, comprising of contact_id, type, and duration in seconds.
Write a solution to find the three longest incoming and outgoing calls.

Return the result table ordered by type, duration, and first_name in descending order and duration must be formatted as HH:MM:SS.

The result format is in the following example.



Example 1:

Input:

Contacts table:

+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 1  | John       | Doe       |
| 2  | Jane       | Smith     |
| 3  | Alice      | Johnson   |
| 4  | Michael    | Brown     |
| 5  | Emily      | Davis     |
+----+------------+-----------+        
Calls table:

+------------+----------+----------+
| contact_id | type     | duration |
+------------+----------+----------+
| 1          | incoming | 120      |
| 1          | outgoing | 180      |
| 2          | incoming | 300      |
| 2          | outgoing | 240      |
| 3          | incoming | 150      |
| 3          | outgoing | 360      |
| 4          | incoming | 420      |
| 4          | outgoing | 200      |
| 5          | incoming | 180      |
| 5          | outgoing | 280      |
+------------+----------+----------+
        
Output:

+-----------+----------+-------------------+
| first_name| type     | duration_formatted|
+-----------+----------+-------------------+
| Alice     | outgoing | 00:06:00          |
| Emily     | outgoing | 00:04:40          |
| Jane      | outgoing | 00:04:00          |
| Michael   | incoming | 00:07:00          |
| Jane      | incoming | 00:05:00          |
| Emily     | incoming | 00:03:00          |
+-----------+----------+-------------------+
        
Explanation:

Alice had an outgoing call lasting 6 minutes.
Emily had an outgoing call lasting 4 minutes and 40 seconds.
Jane had an outgoing call lasting 4 minutes.
Michael had an incoming call lasting 7 minutes.
Jane had an incoming call lasting 5 minutes.
Emily had an incoming call lasting 3 minutes.
Note: Output table is sorted by type, duration, and first_name in descending order.
  1. # Write your MySQL query statement below
  2. WITH ranked AS (
  3.     SELECT
  4.         c.first_name,
  5.         ca.type,
  6.         ca.duration,
  7.         ROW_NUMBER() OVER (
  8.             PARTITION BY ca.type
  9.             ORDER BY ca.duration DESC
  10.         ) AS rn
  11.     FROM Calls ca
  12.     JOIN Contacts c
  13.         ON c.id = ca.contact_id
  14. )
  15. SELECT
  16.     first_name,
  17.     type,
  18.     TIME_FORMAT(SEC_TO_TIME(duration), '%H:%i:%s') AS duration_formatted
  19. FROM ranked
  20. WHERE rn <= 3
  21. ORDER BY type DESC, duration DESC, first_name DESC;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:23:15 | 只看该作者
全局:
3126. Server Utilization Time
Medium
Topics
SQL Schema
Pandas Schema
Table: Servers

+----------------+----------+
| Column Name    | Type     |
+----------------+----------+
| server_id      | int      |
| status_time    | datetime |
| session_status | enum     |
+----------------+----------+
(server_id, status_time, session_status) is the primary key (combination of columns with unique values) for this table.
session_status is an ENUM (category) type of ('start', 'stop').
Each row of this table contains server_id, status_time, and session_status.
Write a solution to find the total time when servers were running. The output should be rounded down to the nearest number of full days.

Return the result table in any order.

The result format is in the following example.



Example:

Input:

Servers table:

+-----------+---------------------+----------------+
| server_id | status_time         | session_status |
+-----------+---------------------+----------------+
| 3         | 2023-11-04 16:29:47 | start          |
| 3         | 2023-11-05 01:49:47 | stop           |
| 3         | 2023-11-25 01:37:08 | start          |
| 3         | 2023-11-25 03:50:08 | stop           |
| 1         | 2023-11-13 03:05:31 | start          |
| 1         | 2023-11-13 11:10:31 | stop           |
| 4         | 2023-11-29 15:11:17 | start          |
| 4         | 2023-11-29 15:42:17 | stop           |
| 4         | 2023-11-20 00:31:44 | start          |
| 4         | 2023-11-20 07:03:44 | stop           |
| 1         | 2023-11-20 00:27:11 | start          |
| 1         | 2023-11-20 01:41:11 | stop           |
| 3         | 2023-11-04 23:16:48 | start          |
| 3         | 2023-11-05 01:15:48 | stop           |
| 4         | 2023-11-30 15:09:18 | start          |
| 4         | 2023-11-30 20:48:18 | stop           |
| 4         | 2023-11-25 21:09:06 | start          |
| 4         | 2023-11-26 04:58:06 | stop           |
| 5         | 2023-11-16 19:42:22 | start          |
| 5         | 2023-11-16 21:08:22 | stop           |
+-----------+---------------------+----------------+
Output:

+-------------------+
| total_uptime_days |
+-------------------+
| 1                 |
+-------------------+
Explanation:

For server ID 3:
From 2023-11-04 16:29:47 to 2023-11-05 01:49:47: ~9.3 hours
From 2023-11-25 01:37:08 to 2023-11-25 03:50:08: ~2.2 hours
From 2023-11-04 23:16:48 to 2023-11-05 01:15:48: ~1.98 hours
Total for server 3: ~13.48 hours
For server ID 1:
From 2023-11-13 03:05:31 to 2023-11-13 11:10:31: ~8 hours
From 2023-11-20 00:27:11 to 2023-11-20 01:41:11: ~1.23 hours
Total for server 1: ~9.23 hours
For server ID 4:
From 2023-11-29 15:11:17 to 2023-11-29 15:42:17: ~0.52 hours
From 2023-11-20 00:31:44 to 2023-11-20 07:03:44: ~6.53 hours
From 2023-11-30 15:09:18 to 2023-11-30 20:48:18: ~5.65 hours
From 2023-11-25 21:09:06 to 2023-11-26 04:58:06: ~7.82 hours
Total for server 4: ~20.52 hours
For server ID 5:
From 2023-11-16 19:42:22 to 2023-11-16 21:08:22: ~1.43 hours
Total for server 5: ~1.43 hours
The accumulated runtime for all servers totals approximately 44.46 hours, equivalent to one full day plus some additional hours. However, since we consider only full days, the final output is rounded to 1 full day.
  1. # Write your MySQL query statement below
  2. SELECT
  3.     FLOOR(
  4.         SUM(
  5.             CASE
  6.                 WHEN session_status = 'stop'
  7.                     THEN  TIMESTAMPDIFF(SECOND, '2023-11-01', status_time)
  8.                 ELSE -TIMESTAMPDIFF(SECOND, '2023-11-01', status_time)
  9.             END
  10.         ) / 86400
  11.     ) AS total_uptime_days
  12. FROM Servers;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:23:52 | 只看该作者
全局:
3140. Consecutive Available Seats II
Medium
Topics
SQL Schema
Pandas Schema
Table: Cinema

+-------------+------+
| Column Name | Type |
+-------------+------+
| seat_id     | int  |
| free        | bool |
+-------------+------+
seat_id is an auto-increment column for this table.
Each row of this table indicates whether the ith seat is free or not. 1 means free while 0 means occupied.
Write a solution to find the length of longest consecutive sequence of available seats in the cinema.

Note:

There will always be at most one longest consecutive sequence.
If there are multiple consecutive sequences with the same length, include all of them in the output.
Return the result table ordered by first_seat_id in ascending order.

The result format is in the following example.



Example:

Input:

Cinema table:

+---------+------+
| seat_id | free |
+---------+------+
| 1       | 1    |
| 2       | 0    |
| 3       | 1    |
| 4       | 1    |
| 5       | 1    |
+---------+------+
Output:

+-----------------+----------------+-----------------------+
| first_seat_id   | last_seat_id   | consecutive_seats_len |
+-----------------+----------------+-----------------------+
| 3               | 5              | 3                     |
+-----------------+----------------+-----------------------+
Explanation:

Longest consecutive sequence of available seats starts from seat 3 and ends at seat 5 with a length of 3.
Output table is ordered by first_seat_id in ascending order.
  1. # Write your MySQL query statement below
  2. WITH islands AS (
  3.     SELECT
  4.         seat_id,
  5.         seat_id - ROW_NUMBER() OVER (ORDER BY seat_id) AS grp
  6.     FROM Cinema
  7.     WHERE free = 1
  8. ),
  9. runs AS (
  10.     SELECT
  11.         MIN(seat_id) AS first_seat_id,
  12.         MAX(seat_id) AS last_seat_id,
  13.         COUNT(*)     AS consecutive_seats_len
  14.     FROM islands
  15.     GROUP BY grp
  16. )
  17. SELECT
  18.     first_seat_id,
  19.     last_seat_id,
  20.     consecutive_seats_len
  21. FROM runs
  22. WHERE consecutive_seats_len = (SELECT MAX(consecutive_seats_len) FROM runs)
  23. ORDER BY first_seat_id;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:24:29 | 只看该作者
全局:
3150. Invalid Tweets II
Easy
Topics
SQL Schema
Pandas Schema
Table: Tweets

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| tweet_id       | int     |
| content        | varchar |
+----------------+---------+
tweet_id is the primary key (column with unique values) for this table.
This table contains all the tweets in a social media app.
Write a solution to find invalid tweets. A tweet is considered invalid if it meets any of the following criteria:

It exceeds 140 characters in length.
It has more than 3 mentions.
It includes more than 3 hashtags.
Return the result table ordered by tweet_id in ascending order.

The result format is in the following example.



Example:

Input:

Tweets table:

  +----------+-----------------------------------------------------------------------------------+
  | tweet_id | content                                                                           |
  +----------+-----------------------------------------------------------------------------------+
  | 1        | Traveling, exploring, and living my best life @janesmith @SaraJohnson @LisaTaylor |
  |          | @MikeBrown #Foodie #Fitness #Learning                                             |
  | 2        | Just had the best dinner with friends! #Foodie #Friends #Fun                      |
  | 4        | Working hard on my new project #Work #Goals #Productivity #Fun                    |
  +----------+-----------------------------------------------------------------------------------+
  
Output:

  +----------+
  | tweet_id |
  +----------+
  | 1        |
  | 4        |
  +----------+
  
Explanation:

tweet_id 1 contains 4 mentions.
tweet_id 4 contains 4 hashtags.
Output table is ordered by tweet_id in ascending order.
  1. # Write your MySQL query statement below
  2. SELECT tweet_id
  3. FROM Tweets
  4. WHERE CHAR_LENGTH(content) > 140
  5.    OR CHAR_LENGTH(content) - CHAR_LENGTH(REPLACE(content, '@', '')) > 3
  6.    OR CHAR_LENGTH(content) - CHAR_LENGTH(REPLACE(content, '#', '')) > 3
  7. ORDER BY tweet_id;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:25:12 | 只看该作者
全局:
3156. Employee Task Duration and Concurrent Tasks
Hard
Topics
SQL Schema
Pandas Schema
Table: Tasks

+---------------+----------+
| Column Name   | Type     |
+---------------+----------+
| task_id       | int      |
| employee_id   | int      |
| start_time    | datetime |
| end_time      | datetime |
+---------------+----------+
(task_id, employee_id) is the primary key for this table.
Each row in this table contains the task identifier, the employee identifier, and the start and end times of each task.
Write a solution to find the total duration of tasks for each employee and the maximum number of concurrent tasks an employee handled at any point in time. The total duration should be rounded down to the nearest number of full hours.

Return the result table ordered by employee_id ascending order.

The result format is in the following example.



Example:

Input:

Tasks table:

+---------+-------------+---------------------+---------------------+
| task_id | employee_id | start_time          | end_time            |
+---------+-------------+---------------------+---------------------+
| 1       | 1001        | 2023-05-01 08:00:00 | 2023-05-01 09:00:00 |
| 2       | 1001        | 2023-05-01 08:30:00 | 2023-05-01 10:30:00 |
| 3       | 1001        | 2023-05-01 11:00:00 | 2023-05-01 12:00:00 |
| 7       | 1001        | 2023-05-01 13:00:00 | 2023-05-01 15:30:00 |
| 4       | 1002        | 2023-05-01 09:00:00 | 2023-05-01 10:00:00 |
| 5       | 1002        | 2023-05-01 09:30:00 | 2023-05-01 11:30:00 |
| 6       | 1003        | 2023-05-01 14:00:00 | 2023-05-01 16:00:00 |
+---------+-------------+---------------------+---------------------+
Output:

+-------------+------------------+----------------------+
| employee_id | total_task_hours | max_concurrent_tasks |
+-------------+------------------+----------------------+
| 1001        | 6                | 2                    |
| 1002        | 2                | 2                    |
| 1003        | 2                | 1                    |
+-------------+------------------+----------------------+
Explanation:

For employee ID 1001:
Task 1 and Task 2 overlap from 08:30 to 09:00 (30 minutes).
Task 7 has a duration of 150 minutes (2 hours and 30 minutes).
Total task time: 60 (Task 1) + 120 (Task 2) + 60 (Task 3) + 150 (Task 7) - 30 (overlap) = 360 minutes = 6 hours.
Maximum concurrent tasks: 2 (during the overlap period).
For employee ID 1002:
Task 4 and Task 5 overlap from 09:30 to 10:00 (30 minutes).
Total task time: 60 (Task 4) + 120 (Task 5) - 30 (overlap) = 150 minutes = 2 hours and 30 minutes.
Total task hours (rounded down): 2 hours.
Maximum concurrent tasks: 2 (during the overlap period).
For employee ID 1003:
No overlapping tasks.
Total task time: 120 minutes = 2 hours.
Maximum concurrent tasks: 1.
Note: Output table is ordered by employee_id in ascending order.
  1. # Write your MySQL query statement below
  2. WITH ordered AS (
  3.     SELECT
  4.         employee_id,
  5.         task_id,
  6.         start_time,
  7.         end_time,
  8.         MAX(end_time) OVER (
  9.             PARTITION BY employee_id
  10.             ORDER BY start_time, end_time, task_id
  11.             ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
  12.         ) AS prev_max_end
  13.     FROM Tasks
  14. ),
  15. flagged AS (
  16.     SELECT
  17.         employee_id,
  18.         start_time,
  19.         end_time,
  20.         SUM(CASE
  21.                 WHEN prev_max_end IS NULL OR start_time > prev_max_end THEN 1
  22.                 ELSE 0
  23.             END) OVER (
  24.             PARTITION BY employee_id
  25.             ORDER BY start_time, end_time, task_id
  26.             ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  27.         ) AS grp
  28.     FROM ordered
  29. ),
  30. merged AS (
  31.     SELECT
  32.         employee_id,
  33.         TIMESTAMPDIFF(SECOND, MIN(start_time), MAX(end_time)) AS secs
  34.     FROM flagged
  35.     GROUP BY employee_id, grp
  36. ),
  37. total AS (
  38.     SELECT
  39.         employee_id,
  40.         FLOOR(SUM(secs) / 3600) AS total_task_hours
  41.     FROM merged
  42.     GROUP BY employee_id
  43. ),
  44. concurrency AS (
  45.     SELECT
  46.         a.employee_id,
  47.         a.task_id,
  48.         COUNT(*) AS cnt
  49.     FROM Tasks a
  50.     JOIN Tasks b
  51.         ON  b.employee_id = a.employee_id
  52.         AND b.start_time <= a.start_time
  53.         AND b.end_time   >  a.start_time
  54.     GROUP BY a.employee_id, a.task_id
  55. ),
  56. max_conc AS (
  57.     SELECT employee_id, MAX(cnt) AS max_concurrent_tasks
  58.     FROM concurrency
  59.     GROUP BY employee_id
  60. )
  61. SELECT
  62.     t.employee_id,
  63.     t.total_task_hours,
  64.     m.max_concurrent_tasks
  65. FROM total t
  66. JOIN max_conc m
  67.     ON m.employee_id = t.employee_id
  68. ORDER BY t.employee_id;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:25:56 | 只看该作者
全局:
3166. Calculate Parking Fees and Duration
Medium
Topics
SQL Schema
Pandas Schema
Table: ParkingTransactions

+--------------+-----------+
| Column Name  | Type      |
+--------------+-----------+
| lot_id       | int       |
| car_id       | int       |
| entry_time   | datetime  |
| exit_time    | datetime  |
| fee_paid     | decimal   |
+--------------+-----------+
(lot_id, car_id, entry_time) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the ID of the parking lot, the ID of the car, the entry and exit times, and the fee paid for the parking duration.
Write a solution to find the total parking fee paid by each car across all parking lots, and the average hourly fee (rounded to 2 decimal places) paid by each car. Also, find the parking lot where each car spent the most total time.

Return the result table ordered by car_id in ascending order.

Note: Test cases are generated in such a way that an individual car cannot be in multiple parking lots at the same time.

The result format is in the following example.



Example:

Input:

ParkingTransactions table:

+--------+--------+---------------------+---------------------+----------+
| lot_id | car_id | entry_time          | exit_time           | fee_paid |
+--------+--------+---------------------+---------------------+----------+
| 1      | 1001   | 2023-06-01 08:00:00 | 2023-06-01 10:30:00 | 5.00     |
| 1      | 1001   | 2023-06-02 11:00:00 | 2023-06-02 12:45:00 | 3.00     |
| 2      | 1001   | 2023-06-01 10:45:00 | 2023-06-01 12:00:00 | 6.00     |
| 2      | 1002   | 2023-06-01 09:00:00 | 2023-06-01 11:30:00 | 4.00     |
| 3      | 1001   | 2023-06-03 07:00:00 | 2023-06-03 09:00:00 | 4.00     |
| 3      | 1002   | 2023-06-02 12:00:00 | 2023-06-02 14:00:00 | 2.00     |
+--------+--------+---------------------+---------------------+----------+
Output:

+--------+----------------+----------------+---------------+
| car_id | total_fee_paid | avg_hourly_fee | most_time_lot |
+--------+----------------+----------------+---------------+
| 1001   | 18.00          | 2.40           | 1             |
| 1002   | 6.00           | 1.33           | 2             |
+--------+----------------+----------------+---------------+
Explanation:

For car ID 1001:
From 2023-06-01 08:00:00 to 2023-06-01 10:30:00 in lot 1: 2.5 hours, fee 5.00
From 2023-06-02 11:00:00 to 2023-06-02 12:45:00 in lot 1: 1.75 hours, fee 3.00
From 2023-06-01 10:45:00 to 2023-06-01 12:00:00 in lot 2: 1.25 hours, fee 6.00
From 2023-06-03 07:00:00 to 2023-06-03 09:00:00 in lot 3: 2 hours, fee 4.00
Total fee paid: 18.00, total hours: 7.5, average hourly fee: 2.40, most time spent in lot 1: 4.25 hours.
For car ID 1002:
From 2023-06-01 09:00:00 to 2023-06-01 11:30:00 in lot 2: 2.5 hours, fee 4.00
From 2023-06-02 12:00:00 to 2023-06-02 14:00:00 in lot 3: 2 hours, fee 2.00
Total fee paid: 6.00, total hours: 4.5, average hourly fee: 1.33, most time spent in lot 2: 2.5 hours.
Note: Output table is ordered by car_id in ascending order.
  1. # Write your MySQL query statement below
  2. WITH lot_time AS (
  3.     SELECT
  4.         car_id,
  5.         lot_id,
  6.         SUM(TIMESTAMPDIFF(SECOND, entry_time, exit_time)) AS lot_secs
  7.     FROM ParkingTransactions
  8.     GROUP BY car_id, lot_id
  9. ),
  10. ranked AS (
  11.     SELECT
  12.         car_id,
  13.         lot_id,
  14.         ROW_NUMBER() OVER (
  15.             PARTITION BY car_id
  16.             ORDER BY lot_secs DESC, lot_id
  17.         ) AS rn
  18.     FROM lot_time
  19. ),
  20. totals AS (
  21.     SELECT
  22.         car_id,
  23.         SUM(fee_paid) AS total_fee_paid,
  24.         SUM(TIMESTAMPDIFF(SECOND, entry_time, exit_time)) AS total_secs
  25.     FROM ParkingTransactions
  26.     GROUP BY car_id
  27. )
  28. SELECT
  29.     t.car_id,
  30.     t.total_fee_paid,
  31.     ROUND(t.total_fee_paid / (t.total_secs / 3600), 2) AS avg_hourly_fee,
  32.     r.lot_id AS most_time_lot
  33. FROM totals t
  34. JOIN ranked r
  35.     ON r.car_id = t.car_id
  36.    AND r.rn = 1
  37. ORDER BY t.car_id;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:26:35 | 只看该作者
全局:
3172. Second Day Verification
Easy
Topics
SQL Schema
Pandas Schema
Table: emails

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| email_id    | int      |
| user_id     | int      |
| signup_date | datetime |
+-------------+----------+
(email_id, user_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the email ID, user ID, and signup date.
Table: texts

+---------------+----------+
| Column Name   | Type     |
+---------------+----------+
| text_id       | int      |
| email_id      | int      |
| signup_action | enum     |
| action_date   | datetime |
+---------------+----------+
(text_id, email_id) is the primary key (combination of columns with unique values) for this table.
signup_action is an enum type of ('Verified', 'Not Verified').
Each row of this table contains the text ID, email ID, signup action, and action date.
Write a Solution to find the user IDs of those who verified their sign-up on the second day.

Return the result table ordered by user_id in ascending order.

The result format is in the following example.



Example:

Input:

emails table:

+----------+---------+---------------------+
| email_id | user_id | signup_date         |
+----------+---------+---------------------+
| 125      | 7771    | 2022-06-14 09:30:00|
| 433      | 1052    | 2022-07-09 08:15:00|
| 234      | 7005    | 2022-08-20 10:00:00|
+----------+---------+---------------------+
texts table:

+---------+----------+--------------+---------------------+
| text_id | email_id | signup_action| action_date         |
+---------+----------+--------------+---------------------+
| 1       | 125      | Verified     | 2022-06-15 08:30:00|
| 2       | 433      | Not Verified | 2022-07-10 10:45:00|
| 4       | 234      | Verified     | 2022-08-21 09:30:00|
+---------+----------+--------------+---------------------+
   
Output:

+---------+
| user_id |
+---------+
| 7005    |
| 7771    |
+---------+
Explanation:

User with user_id 7005 and email_id 234 signed up on 2022-08-20 10:00:00 and verified on second day of the signup.
User with user_id 7771 and email_id 125 signed up on 2022-06-14 09:30:00 and verified on second day of the signup.
  1. # Write your MySQL query statement below
  2. SELECT DISTINCT e.user_id
  3. FROM emails e
  4. JOIN texts t
  5.     ON t.email_id = e.email_id
  6. WHERE t.signup_action = 'Verified'
  7.   AND DATEDIFF(t.action_date, e.signup_date) = 1
  8. ORDER BY e.user_id;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:27:23 | 只看该作者
全局:
3182. Find Top Scoring Students
Medium
Topics
SQL Schema
Pandas Schema
Table: students

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| student_id  | int      |
| name        | varchar  |
| major       | varchar  |
+-------------+----------+
student_id is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the student ID, student name, and their major.
Table: courses

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| course_id   | int      |
| name        | varchar  |
| credits     | int      |
| major       | varchar  |
+-------------+----------+
course_id is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the course ID, course name, the number of credits for the course, and the major it belongs to.
Table: enrollments

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| student_id  | int      |
| course_id   | int      |
| semester    | varchar  |
| grade       | varchar  |
+-------------+----------+
(student_id, course_id, semester) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the student ID, course ID, semester, and grade received.
Write a solution to find the students who have taken all courses offered in their major and have achieved a grade of A in all these courses.

Return the result table ordered by student_id in ascending order.

The result format is in the following example.



Example:

Input:

students table:

+------------+------------------+------------------+
| student_id | name             | major            |
+------------+------------------+------------------+
| 1          | Alice            | Computer Science |
| 2          | Bob              | Computer Science |
| 3          | Charlie          | Mathematics      |
| 4          | David            | Mathematics      |
+------------+------------------+------------------+
courses table:

+-----------+-----------------+---------+------------------+
| course_id | name            | credits | major            |
+-----------+-----------------+---------+------------------+
| 101       | Algorithms      | 3       | Computer Science |
| 102       | Data Structures | 3       | Computer Science |
| 103       | Calculus        | 4       | Mathematics      |
| 104       | Linear Algebra  | 4       | Mathematics      |
+-----------+-----------------+---------+------------------+
enrollments table:

+------------+-----------+----------+-------+
| student_id | course_id | semester | grade |
+------------+-----------+----------+-------+
| 1          | 101       | Fall 2023| A     |
| 1          | 102       | Fall 2023| A     |
| 2          | 101       | Fall 2023| B     |
| 2          | 102       | Fall 2023| A     |
| 3          | 103       | Fall 2023| A     |
| 3          | 104       | Fall 2023| A     |
| 4          | 103       | Fall 2023| A     |
| 4          | 104       | Fall 2023| B     |
+------------+-----------+----------+-------+
Output:

+------------+
| student_id |
+------------+
| 1          |
| 3          |
+------------+
Explanation:

Alice (student_id 1) is a Computer Science major and has taken both "Algorithms" and "Data Structures", receiving an 'A' in both.
Bob (student_id 2) is a Computer Science major but did not receive an 'A' in all required courses.
Charlie (student_id 3) is a Mathematics major and has taken both "Calculus" and "Linear Algebra", receiving an 'A' in both.
David (student_id 4) is a Mathematics major but did not receive an 'A' in all required courses.
Note: Output table is ordered by student_id in ascending order.
  1. # Write your MySQL query statement below
  2. SELECT s.student_id
  3. FROM students s
  4. JOIN enrollments e
  5.     ON e.student_id = s.student_id
  6. JOIN courses c
  7.     ON c.course_id = e.course_id
  8.    AND c.major = s.major
  9. WHERE e.grade = 'A'
  10. GROUP BY s.student_id, s.major
  11. HAVING COUNT(DISTINCT c.course_id) = (
  12.     SELECT COUNT(*)
  13.     FROM courses c2
  14.     WHERE c2.major = s.major
  15. )
  16. ORDER BY s.student_id;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-23 12:28:13 | 只看该作者
全局:
3188. Find Top Scoring Students II
Hard
Topics
SQL Schema
Pandas Schema
Table: students

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| student_id  | int      |
| name        | varchar  |
| major       | varchar  |
+-------------+----------+
student_id is the primary key for this table.
Each row contains the student ID, student name, and their major.
Table: courses

+-------------+-------------------+
| Column Name | Type              |      
+-------------+-------------------+
| course_id   | int               |   
| name        | varchar           |      
| credits     | int               |           
| major       | varchar           |      
| mandatory   | enum              |      
+-------------+-------------------+
course_id is the primary key for this table.
mandatory is an enum type of ('Yes', 'No').
Each row contains the course ID, course name, credits, major it belongs to, and whether the course is mandatory.
Table: enrollments

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| student_id  | int      |
| course_id   | int      |
| semester    | varchar  |
| grade       | varchar  |
| GPA         | decimal  |
+-------------+----------+
(student_id, course_id, semester) is the primary key (combination of columns with unique values) for this table.
Each row contains the student ID, course ID, semester, and grade received.
Write a solution to find the students who meet the following criteria:

Have taken all mandatory courses and at least two elective courses offered in their major.
Achieved a grade of A in all mandatory courses and at least B in elective courses.
Maintained an average GPA of at least 2.5 across all their courses (including those outside their major).
Return the result table ordered by student_id in ascending order.



Example:

Input:

students table:

+------------+------------------+------------------+
| student_id | name             | major            |
+------------+------------------+------------------+
| 1          | Alice            | Computer Science |
| 2          | Bob              | Computer Science |
| 3          | Charlie          | Mathematics      |
| 4          | David            | Mathematics      |
+------------+------------------+------------------+

courses table:

+-----------+-------------------+---------+------------------+----------+
| course_id | name              | credits | major            | mandatory|
+-----------+-------------------+---------+------------------+----------+
| 101       | Algorithms        | 3       | Computer Science | yes      |
| 102       | Data Structures   | 3       | Computer Science | yes      |
| 103       | Calculus          | 4       | Mathematics      | yes      |
| 104       | Linear Algebra    | 4       | Mathematics      | yes      |
| 105       | Machine Learning  | 3       | Computer Science | no       |
| 106       | Probability       | 3       | Mathematics      | no       |
| 107       | Operating Systems | 3       | Computer Science | no       |
| 108       | Statistics        | 3       | Mathematics      | no       |
+-----------+-------------------+---------+------------------+----------+

enrollments table:

+------------+-----------+-------------+-------+-----+
| student_id | course_id | semester    | grade | GPA |
+------------+-----------+-------------+-------+-----+
| 1          | 101       | Fall 2023   | A     | 4.0 |
| 1          | 102       | Spring 2023 | A     | 4.0 |
| 1          | 105       | Spring 2023 | A     | 4.0 |
| 1          | 107       | Fall 2023   | B     | 3.5 |
| 2          | 101       | Fall 2023   | A     | 4.0 |
| 2          | 102       | Spring 2023 | B     | 3.0 |
| 3          | 103       | Fall 2023   | A     | 4.0 |
| 3          | 104       | Spring 2023 | A     | 4.0 |
| 3          | 106       | Spring 2023 | A     | 4.0 |
| 3          | 108       | Fall 2023   | B     | 3.5 |
| 4          | 103       | Fall 2023   | B     | 3.0 |
| 4          | 104       | Spring 2023 | B     | 3.0 |
+------------+-----------+-------------+-------+-----+

Output:

+------------+
| student_id |
+------------+
| 1          |
| 3          |
+------------+

Explanation:

Alice (student_id 1) is a Computer Science major and has taken both Algorithms and Data Structures, receiving an A in both. She has also taken Machine Learning and Operating Systems as electives, receiving an A and B respectively.
Bob (student_id 2) is a Computer Science major but did not receive an A in all required courses.
Charlie (student_id 3) is a Mathematics major and has taken both Calculus and Linear Algebra, receiving an A in both. He has also taken Probability and Statistics as electives, receiving an A and B respectively.
David (student_id 4) is a Mathematics major but did not receive an A in all required courses.
Note: Output table is ordered by student_id in ascending order.
  1. # Write your MySQL query statement below
  2. SELECT s.student_id
  3. FROM students s
  4. JOIN enrollments e
  5.     ON e.student_id = s.student_id
  6. JOIN courses c
  7.     ON c.course_id = e.course_id
  8. GROUP BY s.student_id, s.major
  9. HAVING COUNT(DISTINCT CASE
  10.                 WHEN c.major = s.major
  11.                  AND c.mandatory = 'Yes'
  12.                  AND e.grade = 'A'
  13.                 THEN c.course_id
  14.             END)
  15.          = (SELECT COUNT(*)
  16.             FROM courses m
  17.             WHERE m.major = s.major
  18.               AND m.mandatory = 'Yes')
  19.    AND COUNT(DISTINCT CASE
  20.                 WHEN c.major = s.major
  21.                  AND c.mandatory = 'No'
  22.                  AND e.grade IN ('A', 'B')
  23.                 THEN c.course_id
  24.             END) >= 2
  25.    AND AVG(e.GPA) >= 2.5
  26. ORDER BY s.student_id;
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表