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

刷题记录帖子

🔗
 楼主| Myron2017 2026-11-22 11:35:34 | 只看该作者
全局:
2362. Generate the Invoice
Hard
Topics
SQL Schema
Pandas Schema
Table: Products

+-------------+------+
| Column Name | Type |
+-------------+------+
| product_id  | int  |
| price       | int  |
+-------------+------+
product_id contains unique values.
Each row in this table shows the ID of a product and the price of one unit.


Table: Purchases

+-------------+------+
| Column Name | Type |
+-------------+------+
| invoice_id  | int  |
| product_id  | int  |
| quantity    | int  |
+-------------+------+
(invoice_id, product_id) is the primary key (combination of columns with unique values) for this table.
Each row in this table shows the quantity ordered from one product in an invoice.


Write a solution to show the details of the invoice with the highest price. If two or more invoices have the same price, return the details of the one with the smallest invoice_id.

Return the result table in any order.

The result format is shown in the following example.



Example 1:

Input:
Products table:
+------------+-------+
| product_id | price |
+------------+-------+
| 1          | 100   |
| 2          | 200   |
+------------+-------+
Purchases table:
+------------+------------+----------+
| invoice_id | product_id | quantity |
+------------+------------+----------+
| 1          | 1          | 2        |
| 3          | 2          | 1        |
| 2          | 2          | 3        |
| 2          | 1          | 4        |
| 4          | 1          | 10       |
+------------+------------+----------+
Output:
+------------+----------+-------+
| product_id | quantity | price |
+------------+----------+-------+
| 2          | 3        | 600   |
| 1          | 4        | 400   |
+------------+----------+-------+
Explanation:
Invoice 1: price = (2 * 100) = $200
Invoice 2: price = (4 * 100) + (3 * 200) = $1000
Invoice 3: price = (1 * 200) = $200
Invoice 4: price = (10 * 100) = $1000

The highest price is $1000, and the invoices with the highest prices are 2 and 4. We return the details of the one with the smallest ID, which is invoice 2.

道题分两步:先找出"总价最高的那张发票",再列出这张发票的明细。

算每张发票的总价: Purchases 只有数量,单价在 Products,所以先 JOIN,再按 invoice_id 汇总 SUM(quantity * price),放进 CTE invoice_total。
选出目标发票: 按 total DESC, invoice_id ASC 排序,取第一行 (LIMIT 1)。总价最高排最前,总价并列时 invoice_id 小的排前面,正好对应题目的并列规则。
输出明细: 再次连接 Purchases 和 Products,只保留目标发票的行。输出的 price 是这一行的小计(quantity * price),不是商品单价。
  1. # Write your MySQL query statement below
  2. WITH invoice_total AS (
  3.     SELECT p.invoice_id,
  4.            SUM(p.quantity * pr.price) AS total
  5.     FROM Purchases p
  6.     JOIN Products pr
  7.       ON p.product_id = pr.product_id
  8.     GROUP BY p.invoice_id
  9. )
  10. SELECT p.product_id,
  11.        p.quantity,
  12.        p.quantity * pr.price AS price
  13. FROM Purchases p
  14. JOIN Products pr
  15.   ON p.product_id = pr.product_id
  16. WHERE p.invoice_id = (
  17.     SELECT invoice_id
  18.     FROM invoice_total
  19.     ORDER BY total DESC, invoice_id ASC
  20.     LIMIT 1
  21. );
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-22 11:36:19 | 只看该作者
全局:
2377. Sort the Olympic Table
Easy
Topics
SQL Schema
Pandas Schema
Table: Olympic

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| country       | varchar |
| gold_medals   | int     |
| silver_medals | int     |
| bronze_medals | int     |
+---------------+---------+
In SQL, country is the primary key for this table.
Each row in this table shows a country name and the number of gold, silver, and bronze medals it won in the Olympic games.


The Olympic table is sorted according to the following rules:

The country with more gold medals comes first.
If there is a tie in the gold medals, the country with more silver medals comes first.
If there is a tie in the silver medals, the country with more bronze medals comes first.
If there is a tie in the bronze medals, the countries with the tie are sorted in ascending order lexicographically.
Write a solution to sort the Olympic table.

The result format is shown in the following example.



Example 1:

Input:
Olympic table:
+-------------+-------------+---------------+---------------+
| country     | gold_medals | silver_medals | bronze_medals |
+-------------+-------------+---------------+---------------+
| China       | 10          | 10            | 20            |
| South Sudan | 0           | 0             | 1             |
| USA         | 10          | 10            | 20            |
| Israel      | 2           | 2             | 3             |
| Egypt       | 2           | 2             | 2             |
+-------------+-------------+---------------+---------------+
Output:
+-------------+-------------+---------------+---------------+
| country     | gold_medals | silver_medals | bronze_medals |
+-------------+-------------+---------------+---------------+
| China       | 10          | 10            | 20            |
| USA         | 10          | 10            | 20            |
| Israel      | 2           | 2             | 3             |
| Egypt       | 2           | 2             | 2             |
| South Sudan | 0           | 0             | 1             |
+-------------+-------------+---------------+---------------+
Explanation:
The tie between China and USA is broken by their lexicographical names. Since "China" is lexicographically smaller than "USA", it comes first.
Israel comes before Egypt because it has more bronze medals.
  1. # Write your MySQL query statement below
  2. SELECT country,
  3.        gold_medals,
  4.        silver_medals,
  5.        bronze_medals
  6. FROM Olympic
  7. ORDER BY gold_medals DESC,
  8.          silver_medals DESC,
  9.          bronze_medals DESC,
  10.          country ASC;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-22 11:37:41 | 只看该作者
全局:
2388. Change Null Values in a Table to the Previous Value
Medium
Topics
SQL Schema
Pandas Schema
Table: CoffeeShop

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| drink       | varchar |
+-------------+---------+
id is the primary key (column with unique values) for this table.
Each row in this table shows the order id and the name of the drink ordered. Some drink rows are nulls.


Write a solution to replace the null values of the drink with the name of the drink of the previous row that is not null. It is guaranteed that the drink on the first row of the table is not null.

Return the result table in the same order as the input.

The result format is shown in the following example.



Example 1:

Input:
CoffeeShop table:
+----+-------------------+
| id | drink             |
+----+-------------------+
| 9  | Rum and Coke      |
| 6  | null              |
| 7  | null              |
| 3  | St Germain Spritz |
| 1  | Orange Margarita  |
| 2  | null              |
+----+-------------------+
Output:
+----+-------------------+
| id | drink             |
+----+-------------------+
| 9  | Rum and Coke      |
| 6  | Rum and Coke      |
| 7  | Rum and Coke      |
| 3  | St Germain Spritz |
| 1  | Orange Margarita  |
| 2  | Orange Margarita  |
+----+-------------------+
Explanation:
For ID 6, the previous value that is not null is from ID 9. We replace the null with "Rum and Coke".
For ID 7, the previous value that is not null is from ID 9. We replace the null with "Rum and Coke;.
For ID 2, the previous value that is not null is from ID 1. We replace the null with "Orange Margarita".
Note that the rows in the output are the same as in the input.
  1. WITH numbered AS (
  2.     SELECT id,
  3.            drink,
  4.            ROW_NUMBER() OVER () AS rn
  5.     FROM CoffeeShop
  6. ),
  7. grouped AS (
  8.     SELECT id,
  9.            drink,
  10.            rn,
  11.            COUNT(drink) OVER (ORDER BY rn) AS grp
  12.     FROM numbered
  13. )
  14. SELECT id,
  15.        FIRST_VALUE(drink) OVER (PARTITION BY grp ORDER BY rn) AS drink
  16. FROM grouped
  17. ORDER BY rn;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-22 11:38:21 | 只看该作者
全局:
2388. Change Null Values in a Table to the Previous Value
Medium
Topics
SQL Schema
Pandas Schema
Table: CoffeeShop

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| drink       | varchar |
+-------------+---------+
id is the primary key (column with unique values) for this table.
Each row in this table shows the order id and the name of the drink ordered. Some drink rows are nulls.


Write a solution to replace the null values of the drink with the name of the drink of the previous row that is not null. It is guaranteed that the drink on the first row of the table is not null.

Return the result table in the same order as the input.

The result format is shown in the following example.



Example 1:

Input:
CoffeeShop table:
+----+-------------------+
| id | drink             |
+----+-------------------+
| 9  | Rum and Coke      |
| 6  | null              |
| 7  | null              |
| 3  | St Germain Spritz |
| 1  | Orange Margarita  |
| 2  | null              |
+----+-------------------+
Output:
+----+-------------------+
| id | drink             |
+----+-------------------+
| 9  | Rum and Coke      |
| 6  | Rum and Coke      |
| 7  | Rum and Coke      |
| 3  | St Germain Spritz |
| 1  | Orange Margarita  |
| 2  | Orange Margarita  |
+----+-------------------+
Explanation:
For ID 6, the previous value that is not null is from ID 9. We replace the null with "Rum and Coke".
For ID 7, the previous value that is not null is from ID 9. We replace the null with "Rum and Coke;.
For ID 2, the previous value that is not null is from ID 1. We replace the null with "Orange Margarita".
Note that the rows in the output are the same as in the input.
  1. # Write your MySQL query statement below
  2. WITH numbered AS (
  3.     SELECT id,
  4.            drink,
  5.            ROW_NUMBER() OVER () AS rn
  6.     FROM CoffeeShop
  7. ),
  8. grouped AS (
  9.     SELECT id,
  10.            drink,
  11.            rn,
  12.            COUNT(drink) OVER (ORDER BY rn) AS grp
  13.     FROM numbered
  14. )
  15. SELECT id,
  16.        FIRST_VALUE(drink) OVER (PARTITION BY grp ORDER BY rn) AS drink
  17. FROM grouped
  18. ORDER BY rn;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-22 11:39:11 | 只看该作者
全局:
2372. Calculate the Influence of Each Salesperson
Medium
Topics
SQL Schema
Pandas Schema
Table: Salesperson

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| salesperson_id | int     |
| name           | varchar |
+----------------+---------+
salesperson_id contains unique values.
Each row in this table shows the ID of a salesperson.


Table: Customer

+----------------+------+
| Column Name    | Type |
+----------------+------+
| customer_id    | int  |
| salesperson_id | int  |
+----------------+------+
customer_id contains unique values.
salesperson_id is a foreign key (reference column) from the Salesperson table.
Each row in this table shows the ID of a customer and the ID of the salesperson.


Table: Sales

+-------------+------+
| Column Name | Type |
+-------------+------+
| sale_id     | int  |
| customer_id | int  |
| price       | int  |
+-------------+------+
sale_id contains unique values.
customer_id is a foreign key (reference column) from the Customer table.
Each row in this table shows ID of a customer and the price they paid for the sale with sale_id.


Write a solution to report the sum of prices paid by the customers of each salesperson. If a salesperson does not have any customers, the total value should be 0.

Return the result table in any order.

The result format is shown in the following example.



Example 1:

Input:
Salesperson table:
+----------------+-------+
| salesperson_id | name  |
+----------------+-------+
| 1              | Alice |
| 2              | Bob   |
| 3              | Jerry |
+----------------+-------+
Customer table:
+-------------+----------------+
| customer_id | salesperson_id |
+-------------+----------------+
| 1           | 1              |
| 2           | 1              |
| 3           | 2              |
+-------------+----------------+
Sales table:
+---------+-------------+-------+
| sale_id | customer_id | price |
+---------+-------------+-------+
| 1       | 2           | 892   |
| 2       | 1           | 354   |
| 3       | 3           | 988   |
| 4       | 3           | 856   |
+---------+-------------+-------+
Output:
+----------------+-------+-------+
| salesperson_id | name  | total |
+----------------+-------+-------+
| 1              | Alice | 1246  |
| 2              | Bob   | 1844  |
| 3              | Jerry | 0     |
+----------------+-------+-------+
Explanation:
Alice is the salesperson for customers 1 and 2.
  - Customer 1 made one purchase with 354.
  - Customer 2 made one purchase with 892.
The total for Alice is 354 + 892 = 1246.

Bob is the salesperson for customers 3.
  - Customer 1 made one purchase with 988 and 856.
The total for Bob is 988 + 856 = 1844.

Jerry is not the salesperson of any customer.
The total for Jerry is 0.
  1. # Write your MySQL query statement below
  2. SELECT sp.salesperson_id,
  3.        sp.name,
  4.        COALESCE(SUM(s.price), 0) AS total
  5. FROM Salesperson sp
  6. LEFT JOIN Customer c
  7.   ON sp.salesperson_id = c.salesperson_id
  8. LEFT JOIN Sales s
  9.   ON c.customer_id = s.customer_id
  10. GROUP BY sp.salesperson_id, sp.name;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-22 11:40:05 | 只看该作者
全局:
2686. Immediate Food Delivery III
Medium
Topics
SQL Schema
Pandas Schema
Table: Delivery

+-----------------------------+---------+
| Column Name                 | Type    |
+-----------------------------+---------+
| delivery_id                 | int     |
| customer_id                 | int     |
| order_date                  | date    |
| customer_pref_delivery_date | date    |
+-----------------------------+---------+
delivery_id is the column with unique values of this table.
Each row contains information about food delivery to a customer that makes an order at some date and specifies a preferred delivery date (on the order date or after it).
If the customer's preferred delivery date is the same as the order date, then the order is called immediate, otherwise, it is scheduled.

Write a solution to find the percentage of immediate orders on each unique order_date, rounded to 2 decimal places.

Return the result table ordered by order_date in ascending order.

The result format is in the following example.



Example 1:

Input:
Delivery table:
+-------------+-------------+------------+-----------------------------+
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
+-------------+-------------+------------+-----------------------------+
| 1           | 1           | 2019-08-01 | 2019-08-02                  |
| 2           | 2           | 2019-08-01 | 2019-08-01                  |
| 3           | 1           | 2019-08-01 | 2019-08-01                  |
| 4           | 3           | 2019-08-02 | 2019-08-13                  |
| 5           | 3           | 2019-08-02 | 2019-08-02                  |
| 6           | 2           | 2019-08-02 | 2019-08-02                  |
| 7           | 4           | 2019-08-03 | 2019-08-03                  |
| 8           | 1           | 2019-08-03 | 2019-08-03                  |
| 9           | 5           | 2019-08-04 | 2019-08-08                  |
| 10          | 2           | 2019-08-04 | 2019-08-18                  |
+-------------+-------------+------------+-----------------------------+
Output:
+------------+----------------------+
| order_date | immediate_percentage |
+------------+----------------------+
| 2019-08-01 | 66.67                |
| 2019-08-02 | 66.67                |
| 2019-08-03 | 100.00               |
| 2019-08-04 | 0.00                 |
+------------+----------------------+
Explanation:
- On 2019-08-01 there were three orders, out of those, two were immediate and one was scheduled. So, immediate percentage for that date was 66.67.
- On 2019-08-02 there were three orders, out of those, two were immediate and one was scheduled. So, immediate percentage for that date was 66.67.
- On 2019-08-03 there were two orders, both were immediate. So, the immediate percentage for that date was 100.00.
- On 2019-08-04 there were two orders, both were scheduled. So, the immediate percentage for that date was 0.00.
order_date is sorted in ascending order.
  1. SELECT order_date,
  2.        ROUND(SUM(order_date = customer_pref_delivery_date) * 100 / COUNT(*), 2) AS immediate_percentage
  3. FROM Delivery
  4. GROUP BY order_date
  5. ORDER BY order_date;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-22 11:41:02 | 只看该作者
全局:
2504. Concatenate the Name and the Profession
Easy
Topics
SQL Schema
Pandas Schema
Table: Person

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| person_id   | int     |
| name        | varchar |
| profession  | ENUM    |
+-------------+---------+
person_id is the primary key (column with a unique value) for this table.
Each row in this table contains a person's ID, name, and profession.
The profession column in an enum of the type ('Doctor', 'Singer', 'Actor', 'Player', 'Engineer', or 'Lawyer')


Write a solution to report each person's name followed by the first letter of their profession enclosed in parentheses.

Return the result table ordered by person_id in descending order.

The result format is shown in the following example.



Example 1:

Input:
Person table:
+-----------+-------+------------+
| person_id | name  | profession |
+-----------+-------+------------+
| 1         | Alex  | Singer     |
| 3         | Alice | Actor      |
| 2         | Bob   | Player     |
| 4         | Messi | Doctor     |
| 6         | Tyson | Engineer   |
| 5         | Meir  | Lawyer     |
+-----------+-------+------------+
Output:
+-----------+----------+
| person_id | name     |
+-----------+----------+
| 6         | Tyson(E) |
| 5         | Meir(L)  |
| 4         | Messi(D) |
| 3         | Alice(A) |
| 2         | Bob(P)   |
| 1         | Alex(S)  |
+-----------+----------+
Explanation: Note that there should not be any white space between the name and the first letter of the profession.
  1. # Write your MySQL query statement below
  2. SELECT person_id,
  3.        CONCAT(name, '(', LEFT(profession, 1), ')') AS name
  4. FROM Person
  5. ORDER BY person_id DESC;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-22 11:42:13 | 只看该作者
全局:
2494. Merge Overlapping Events in the Same Hall
Hard
Topics
SQL Schema
Pandas Schema
Table: HallEvents

+-------------+------+
| Column Name | Type |
+-------------+------+
| hall_id     | int  |
| start_day   | date |
| end_day     | date |
+-------------+------+
This table may contain duplicates rows.
Each row of this table indicates the start day and end day of an event and the hall in which the event is held.


Write a solution to merge all the overlapping events that are held in the same hall. Two events overlap if they have at least one day in common.

Return the result table in any order.

The result format is in the following example.



Example 1:

Input:
HallEvents table:
+---------+------------+------------+
| hall_id | start_day  | end_day    |
+---------+------------+------------+
| 1       | 2023-01-13 | 2023-01-14 |
| 1       | 2023-01-14 | 2023-01-17 |
| 1       | 2023-01-18 | 2023-01-25 |
| 2       | 2022-12-09 | 2022-12-23 |
| 2       | 2022-12-13 | 2022-12-17 |
| 3       | 2022-12-01 | 2023-01-30 |
+---------+------------+------------+
Output:
+---------+------------+------------+
| hall_id | start_day  | end_day    |
+---------+------------+------------+
| 1       | 2023-01-13 | 2023-01-17 |
| 1       | 2023-01-18 | 2023-01-25 |
| 2       | 2022-12-09 | 2022-12-23 |
| 3       | 2022-12-01 | 2023-01-30 |
+---------+------------+------------+
Explanation: There are three halls.
Hall 1:
- The two events ["2023-01-13", "2023-01-14"] and ["2023-01-14", "2023-01-17"] overlap. We merge them in one event ["2023-01-13", "2023-01-17"].
- The event ["2023-01-18", "2023-01-25"] does not overlap with any other event, so we leave it as it is.
Hall 2:
- The two events ["2022-12-09", "2022-12-23"] and ["2022-12-13", "2022-12-17"] overlap. We merge them in one event ["2022-12-09", "2022-12-23"].
Hall 3:
- The hall has only one event, so we return it. Note that we only consider the events of each hall separately.
  1. # Write your MySQL query statement below
  2. WITH flagged AS (
  3.     SELECT hall_id,
  4.            start_day,
  5.            end_day,
  6.            MAX(end_day) OVER (
  7.                PARTITION BY hall_id
  8.                ORDER BY start_day, end_day
  9.                ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
  10.            ) AS prev_max_end
  11.     FROM HallEvents
  12. ),
  13. grouped AS (
  14.     SELECT hall_id,
  15.            start_day,
  16.            end_day,
  17.            SUM(CASE WHEN prev_max_end IS NULL OR start_day > prev_max_end
  18.                     THEN 1 ELSE 0 END)
  19.                OVER (PARTITION BY hall_id ORDER BY start_day, end_day) AS grp
  20.     FROM flagged
  21. )
  22. SELECT hall_id,
  23.        MIN(start_day) AS start_day,
  24.        MAX(end_day)   AS end_day
  25. FROM grouped
  26. GROUP BY hall_id, grp;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-22 11:42:57 | 只看该作者
全局:
2474. Customers With Strictly Increasing Purchases
Hard
Topics
SQL Schema
Pandas Schema
Table: Orders

+--------------+------+
| Column Name  | Type |
+--------------+------+
| order_id     | int  |
| customer_id  | int  |
| order_date   | date |
| price        | int  |
+--------------+------+
order_id is the column with unique values for this table.
Each row contains the id of an order, the id of customer that ordered it, the date of the order, and its price.


Write a solution to report the IDs of the customers with the total purchases strictly increasing yearly.

The total purchases of a customer in one year is the sum of the prices of their orders in that year. If for some year the customer did not make any order, we consider the total purchases 0.
The first year to consider for each customer is the year of their first order.
The last year to consider for each customer is the year of their last order.
Return the result table in any order.

The result format is in the following example.



Example 1:

Input:
Orders table:
+----------+-------------+------------+-------+
| order_id | customer_id | order_date | price |
+----------+-------------+------------+-------+
| 1        | 1           | 2019-07-01 | 1100  |
| 2        | 1           | 2019-11-01 | 1200  |
| 3        | 1           | 2020-05-26 | 3000  |
| 4        | 1           | 2021-08-31 | 3100  |
| 5        | 1           | 2022-12-07 | 4700  |
| 6        | 2           | 2015-01-01 | 700   |
| 7        | 2           | 2017-11-07 | 1000  |
| 8        | 3           | 2017-01-01 | 900   |
| 9        | 3           | 2018-11-07 | 900   |
+----------+-------------+------------+-------+
Output:
+-------------+
| customer_id |
+-------------+
| 1           |
+-------------+
Explanation:
Customer 1: The first year is 2019 and the last year is 2022
  - 2019: 1100 + 1200 = 2300
  - 2020: 3000
  - 2021: 3100
  - 2022: 4700
  We can see that the total purchases are strictly increasing yearly, so we include customer 1 in the answer.

Customer 2: The first year is 2015 and the last year is 2017
  - 2015: 700
  - 2016: 0
  - 2017: 1000
  We do not include customer 2 in the answer because the total purchases are not strictly increasing. Note that customer 2 did not make any purchases in 2016.

Customer 3: The first year is 2017, and the last year is 2018
  - 2017: 900
  - 2018: 900
We do not include customer 3 in the answer because the total purchases are not strictly increasing.
  1. # Write your MySQL query statement below
  2. WITH yearly AS (
  3.     SELECT customer_id,
  4.            YEAR(order_date) AS yr,
  5.            SUM(price)       AS total
  6.     FROM Orders
  7.     GROUP BY customer_id, YEAR(order_date)
  8. ),
  9. compared AS (
  10.     SELECT customer_id,
  11.            yr,
  12.            total,
  13.            LAG(yr)    OVER (PARTITION BY customer_id ORDER BY yr) AS prev_yr,
  14.            LAG(total) OVER (PARTITION BY customer_id ORDER BY yr) AS prev_total
  15.     FROM yearly
  16. )
  17. SELECT customer_id
  18. FROM compared
  19. GROUP BY customer_id
  20. HAVING SUM(prev_yr IS NOT NULL
  21.            AND (yr <> prev_yr + 1 OR total <= prev_total)) = 0;
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2026-11-22 11:43:42 | 只看该作者
全局:
2394. Employees With Deductions
Medium
Topics
SQL Schema
Pandas Schema
Table: Employees

+--------------+------+
| Column Name  | Type |
+--------------+------+
| employee_id  | int  |
| needed_hours | int  |
+--------------+------+
employee_id is column with unique values for this table.
Each row contains the id of an employee and the minimum number of hours needed for them to work to get their salary.


Table: Logs

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| employee_id | int      |
| in_time     | datetime |
| out_time    | datetime |
+-------------+----------+
(employee_id, in_time, out_time) is the primary key (combination of columns with unique values) for this table.
Each row of this table shows the time stamps for an employee. in_time is the time the employee started to work, and out_time is the time the employee ended work.
All the times are in October 2022. out_time can be one day after in_time which means the employee worked after the midnight.


In a company, each employee must work a certain number of hours every month. Employees work in sessions. The number of hours an employee worked can be calculated from the sum of the number of minutes the employee worked in all of their sessions. The number of minutes in each session is rounded up.

For example, if the employee worked for 51 minutes and 2 seconds in a session, we consider it 52 minutes.
Write a solution to report the IDs of the employees that will be deducted. In other words, report the IDs of the employees that did not work the needed hours.

Return the result table in any order.

The result format is in the following example.



Example 1:

Input:
Employees table:
+-------------+--------------+
| employee_id | needed_hours |
+-------------+--------------+
| 1           | 20           |
| 2           | 12           |
| 3           | 2            |
+-------------+--------------+
Logs table:
+-------------+---------------------+---------------------+
| employee_id | in_time             | out_time            |
+-------------+---------------------+---------------------+
| 1           | 2022-10-01 09:00:00 | 2022-10-01 17:00:00 |
| 1           | 2022-10-06 09:05:04 | 2022-10-06 17:09:03 |
| 1           | 2022-10-12 23:00:00 | 2022-10-13 03:00:01 |
| 2           | 2022-10-29 12:00:00 | 2022-10-29 23:58:58 |
+-------------+---------------------+---------------------+
Output:
+-------------+
| employee_id |
+-------------+
| 2           |
| 3           |
+-------------+
Explanation:
Employee 1:
- Worked for three sessions:
    - On 2022-10-01, they worked for 8 hours.
    - On 2022-10-06, they worked for 8 hours and 4 minutes.
    - On 2022-10-12, they worked for 4 hours and 1 minute. Note that they worked through midnight.
- Employee 1 worked a total of 20 hours and 5 minutes across sessions and will not be deducted.
Employee 2:
- Worked for one session:
    - On 2022-10-29, they worked for 11 hours and 59 minutes.
- Employee 2 did not work their hours and will be deducted.
Employee 3:
- Did not work any session.
- Employee 3 did not work their hours and will be deducted.
  1. # Write your MySQL query statement below
  2. SELECT e.employee_id
  3. FROM Employees e
  4. LEFT JOIN Logs l
  5.   ON e.employee_id = l.employee_id
  6. GROUP BY e.employee_id, e.needed_hours
  7. HAVING COALESCE(
  8.            SUM(CEIL(TIMESTAMPDIFF(SECOND, l.in_time, l.out_time) / 60)),
  9.            0
  10.        ) < e.needed_hours * 60;
复制代码
回复

使用道具 举报

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

本版积分规则

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