+----------------+---------+
| Column Name | Type |
+----------------+---------+
| item_id | int |
| item_type | varchar |
| item_category | varchar |
| square_footage | decimal |
+----------------+---------+
item_id is the column of unique values for this table.
Each row includes item id, item type, item category and sqaure footage.
Leetcode warehouse wants to maximize the number of items it can stock in a 500,000 square feet warehouse. It wants to stock as many prime items as possible, and afterwards use the remaining square footage to stock the most number of non-prime items.
Write a solution to find the number of prime and non-prime items that can be stored in the 500,000 square feet warehouse. Output the item type with prime_eligible followed by not_prime and the maximum number of items that can be stocked.
Note:
Item count must be a whole number (integer).
If the count for the not_prime category is 0, you should output 0 for that particular category.
Return the result table ordered by item count in descending order.
The result format is in the following example.
Example 1:
Input:
Inventory table:
+---------+----------------+---------------+----------------+
| item_id | item_type | item_category | square_footage |
+---------+----------------+---------------+----------------+
| 1374 | prime_eligible | Watches | 68.00 |
| 4245 | not_prime | Art | 26.40 |
| 5743 | prime_eligible | Software | 325.00 |
| 8543 | not_prime | Clothing | 64.50 |
| 2556 | not_prime | Shoes | 15.00 |
| 2452 | prime_eligible | Scientific | 85.00 |
| 3255 | not_prime | Furniture | 22.60 |
| 1672 | prime_eligible | Beauty | 8.50 |
| 4256 | prime_eligible | Furniture | 55.50 |
| 6325 | prime_eligible | Food | 13.20 |
+---------+----------------+---------------+----------------+
Output:
+----------------+-------------+
| item_type | item_count |
+----------------+-------------+
| prime_eligible | 5400 |
| not_prime | 8 |
+----------------+-------------+
Explanation:
- The prime-eligible category comprises a total of 6 items, amounting to a combined square footage of 555.20 (68 + 325 + 85 + 8.50 + 55.50 + 13.20). It is possible to store 900 combinations of these 6 items, totaling 5400 items and occupying 499,680 square footage.
- In the not_prime category, there are a total of 4 items with a combined square footage of 128.50. After deducting the storage used by prime-eligible items (500,000 - 499,680 = 320), there is room for 2 combinations of non-prime items, accommodating a total of 8 non-prime items within the available 320 square footage.
Output table is ordered by item count in descending order.
+-------------+------+
| Column Name | Type |
+-------------+------+
| A | int |
| B | int |
| C | int |
+-------------+------+
(A, B, C) is the primary key for this table.
Each row include the lengths of each of a triangle's three sides.
Write a query to find the type of triangle. Output one of the following for each row:
Equilateral: It's a triangle with 3 sides of equal length.
Isosceles: It's a triangle with 2 sides of equal length.
Scalene: It's a triangle with 3 sides of differing lengths.
Not A Triangle: The given values of A, B, and C don't form a triangle.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
Triangles table:
+----+----+----+
| A | B | C |
+----+----+----+
| 20 | 20 | 23 |
| 20 | 20 | 20 |
| 20 | 21 | 22 |
| 13 | 14 | 30 |
+----+----+----+
Output:
+----------------+
| triangle_type |
+----------------+
| Isosceles |
| Equilateral |
| Scalene |
| Not A Triangle |
+----------------+
Explanation:
- Values in the first row from an Isosceles triangle, because A = B.
- Values in the second row from an Equilateral triangle, because A = B = C.
- Values in the third row from an Scalene triangle, because A != B != C.
- Values in the fourth row cannot form a triangle, because the combined value of sides A and B is not larger than that of side C.
# Write your MySQL query statement below
SELECT
CASE
WHEN A + B <= C OR A + C <= B OR B + C <= A THEN 'Not A Triangle'
3054. Binary Tree Nodes
Medium
Topics
SQL Schema
Pandas Schema
Table: Tree
+-------------+------+
| Column Name | Type |
+-------------+------+
| N | int |
| P | int |
+-------------+------+
N is the column of unique values for this table.
Each row includes N and P, where N represents the value of a node in Binary Tree, and P is the parent of N.
Write a solution to find the node type of the Binary Tree. Output one of the following for each node:
Root: if the node is the root node.
Leaf: if the node is the leaf node.
Inner: if the node is neither root nor leaf node.
Return the result table ordered by node value in ascending order.
The result format is in the following example.
Example 1:
Input:
Tree table:
+---+------+
| N | P |
+---+------+
| 1 | 2 |
| 3 | 2 |
| 6 | 8 |
| 9 | 8 |
| 2 | 5 |
| 8 | 5 |
| 5 | null |
+---+------+
Output:
+---+-------+
| N | Type |
+---+-------+
| 1 | Leaf |
| 2 | Inner |
| 3 | Leaf |
| 5 | Root |
| 6 | Leaf |
| 8 | Inner |
| 9 | Leaf |
+---+-------+
Explanation:
- Node 5 is the root node since it has no parent node.
- Nodes 1, 3, 6, and 9 are leaf nodes because they don't have any child nodes.
- Nodes 2, and 8 are inner nodes as they serve as parents to some of the nodes in the structure.
# Write your MySQL query statement below
SELECT
N,
CASE
WHEN P IS NULL THEN 'Root'
WHEN N IN (SELECT P FROM Tree WHERE P IS NOT NULL) THEN 'Inner'
3055. Top Percentile Fraud
Medium
Topics
SQL Schema
Pandas Schema
Table: Fraud
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| policy_id | int |
| state | varchar |
| fraud_score | int |
+-------------+---------+
policy_id is column of unique values for this table.
This table contains policy id, state, and fraud score.
The Leetcode Insurance Corp has developed an ML-driven predictive model to detect the likelihood of fraudulent claims. Consequently, they allocate their most seasoned claim adjusters to address the top 5% of claims flagged by this model.
Write a solution to find the top 5 percentile of claims from each state.
Return the result table ordered by state in ascending order, fraud_score in descending order, and policy_id in ascending order.
The result format is in the following example.
Example 1:
Input:
Fraud table:
+-----------+------------+-------------+
| policy_id | state | fraud_score |
+-----------+------------+-------------+
| 1 | California | 0.92 |
| 2 | California | 0.68 |
| 3 | California | 0.17 |
| 4 | New York | 0.94 |
| 5 | New York | 0.81 |
| 6 | New York | 0.77 |
| 7 | Texas | 0.98 |
| 8 | Texas | 0.97 |
| 9 | Texas | 0.96 |
| 10 | Florida | 0.97 |
| 11 | Florida | 0.98 |
| 12 | Florida | 0.78 |
| 13 | Florida | 0.88 |
| 14 | Florida | 0.66 |
+-----------+------------+-------------+
Output:
+-----------+------------+-------------+
| policy_id | state | fraud_score |
+-----------+------------+-------------+
| 1 | California | 0.92 |
| 11 | Florida | 0.98 |
| 4 | New York | 0.94 |
| 7 | Texas | 0.98 |
+-----------+------------+-------------+
Explanation
- For the state of California, only policy ID 1, with a fraud score of 0.92, falls within the top 5 percentile for this state.
- For the state of Florida, only policy ID 11, with a fraud score of 0.98, falls within the top 5 percentile for this state.
- For the state of New York, only policy ID 4, with a fraud score of 0.94, falls within the top 5 percentile for this state.
- For the state of Texas, only policy ID 7, with a fraud score of 0.98, falls within the top 5 percentile for this state.
Output table is ordered by state in ascending order, fraud score in descending order, and policy ID in ascending order.
# Write your MySQL query statement below
WITH t AS (
SELECT
policy_id,
state,
fraud_score,
PERCENT_RANK() OVER (
PARTITION BY state
ORDER BY fraud_score DESC
) AS pr
FROM Fraud
)
SELECT policy_id, state, fraud_score
FROM t
WHERE pr <= 0.05
ORDER BY state ASC, fraud_score DESC, policy_id ASC;
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| activity_id | int |
| user_id | int |
| activity_type | enum |
| time_spent | decimal |
+---------------+---------+
activity_id is column of unique values for this table.
activity_type is an ENUM (category) type of ('send', 'open').
This table contains activity id, user id, activity type and time spent.
Table: Age
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id | int |
| age_bucket | enum |
+-------------+------+
user_id is the column of unique values for this table.
age_bucket is an ENUM (category) type of ('21-25', '26-30', '31-35').
This table contains user id and age group.
Write a solution to calculate the percentage of the total time spent on sending and opening snaps for each age group. Precentage should be rounded to 2 decimal places.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
Activities table:
+-------------+---------+---------------+------------+
| activity_id | user_id | activity_type | time_spent |
+-------------+---------+---------------+------------+
| 7274 | 123 | open | 4.50 |
| 2425 | 123 | send | 3.50 |
| 1413 | 456 | send | 5.67 |
| 2536 | 456 | open | 3.00 |
| 8564 | 456 | send | 8.24 |
| 5235 | 789 | send | 6.24 |
| 4251 | 123 | open | 1.25 |
| 1435 | 789 | open | 5.25 |
+-------------+---------+---------------+------------+
Age table:
+---------+------------+
| user_id | age_bucket |
+---------+------------+
| 123 | 31-35 |
| 789 | 21-25 |
| 456 | 26-30 |
+---------+------------+
Output:
+------------+-----------+-----------+
| age_bucket | send_perc | open_perc |
+------------+-----------+-----------+
| 31-35 | 37.84 | 62.16 |
| 26-30 | 82.26 | 17.74 |
| 21-25 | 54.31 | 45.69 |
+------------+-----------+-----------+
Explanation:
For age group 31-35:
- There is only one user belonging to this group with the user ID 123.
- The total time spent on sending snaps by this user is 3.50, and the time spent on opening snaps is 4.50 + 1.25 = 5.75.
- The overall time spent by this user is 3.50 + 5.75 = 9.25.
- Therefore, the sending snap percentage will be (3.50 / 9.25) * 100 = 37.84, and the opening snap percentage will be (5.75 / 9.25) * 100 = 62.16.
For age group 26-30:
- There is only one user belonging to this group with the user ID 456.
- The total time spent on sending snaps by this user is 5.67 + 8.24 = 13.91, and the time spent on opening snaps is 3.00.
- The overall time spent by this user is 13.91 + 3.00 = 16.91.
- Therefore, the sending snap percentage will be (13.91 / 16.91) * 100 = 82.26, and the opening snap percentage will be (3.00 / 16.91) * 100 = 17.74.
For age group 21-25:
- There is only one user belonging to this group with the user ID 789.
- The total time spent on sending snaps by this user is 6.24, and the time spent on opening snaps is 5.25.
- The overall time spent by this user is 6.24 + 5.25 = 11.49.
- Therefore, the sending snap percentage will be (6.24 / 11.49) * 100 = 54.31, and the opening snap percentage will be (5.25 / 11.49) * 100 = 45.69.
All percentages in output table rounded to the two decimal places.
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| project_id | int |
| employee_id | int |
| workload | int |
+-------------+---------+
employee_id is the primary key (column with unique values) of this table.
employee_id is a foreign key (reference column) to Employee table.
Each row of this table indicates that the employee with employee_id is working on the project with project_id and the workload of the project.
Table: Employees
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| employee_id | int |
| name | varchar |
| team | varchar |
+------------------+---------+
employee_id is the primary key (column with unique values) of this table.
Each row of this table contains information about one employee.
Write a solution to find the employees who are allocated to projects with a workload that exceeds the average workload of all employees for their respective teams
Return the result table ordered by employee_id, project_id in ascending order.
The result format is in the following example.
Example 1:
Input:
Project table:
+-------------+-------------+----------+
| project_id | employee_id | workload |
+-------------+-------------+----------+
| 1 | 1 | 45 |
| 1 | 2 | 90 |
| 2 | 3 | 12 |
| 2 | 4 | 68 |
+-------------+-------------+----------+
Employees table:
+-------------+--------+------+
| employee_id | name | team |
+-------------+--------+------+
| 1 | Khaled | A |
| 2 | Ali | B |
| 3 | John | B |
| 4 | Doe | A |
+-------------+--------+------+
Output:
+-------------+------------+---------------+------------------+
| employee_id | project_id | employee_name | project_workload |
+-------------+------------+---------------+------------------+
| 2 | 1 | Ali | 90 |
| 4 | 2 | Doe | 68 |
+-------------+------------+---------------+------------------+
Explanation:
- Employee with ID 1 has a project workload of 45 and belongs to Team A, where the average workload is 56.50. Since his project workload does not exceed the team's average workload, he will be excluded.
- Employee with ID 2 has a project workload of 90 and belongs to Team B, where the average workload is 51.00. Since his project workload does exceed the team's average workload, he will be included.
- Employee with ID 3 has a project workload of 12 and belongs to Team B, where the average workload is 51.00. Since his project workload does not exceed the team's average workload, he will be excluded.
- Employee with ID 4 has a project workload of 68 and belongs to Team A, where the average workload is 56.50. Since his project workload does exceed the team's average workload, he will be included.
Result table orderd by employee_id, project_id in ascending order.
# Write your MySQL query statement below
WITH t AS (
SELECT
p.employee_id,
p.project_id,
e.name AS employee_name,
p.workload AS project_workload,
AVG(p.workload) OVER (PARTITION BY e.team) AS team_avg
3058. Friends With No Mutual Friends
Medium
Topics
SQL Schema
Pandas Schema
Table: Friends
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id1 | int |
| user_id2 | int |
+-------------+------+
(user_id1, user_id2) is the primary key (combination of columns with unique values) for this table.
Each row contains user id1, user id2, both of whom are friends with each other.
Write a solution to find all pairs of users who are friends with each other and have no mutual friends.
Return the result table ordered by user_id1, user_id2 in ascending order.
The result format is in the following example.
Example 1:
Input:
Friends table:
+----------+----------+
| user_id1 | user_id2 |
+----------+----------+
| 1 | 2 |
| 2 | 3 |
| 2 | 4 |
| 1 | 5 |
| 6 | 7 |
| 3 | 4 |
| 2 | 5 |
| 8 | 9 |
+----------+----------+
Output:
+----------+----------+
| user_id1 | user_id2 |
+----------+----------+
| 6 | 7 |
| 8 | 9 |
+----------+----------+
Explanation:
- Users 1 and 2 are friends with each other, but they share a mutual friend with user ID 5, so this pair is not included.
- Users 2 and 3 are friends, they both share a mutual friend with user ID 4, resulting in exclusion, similarly for users 2 and 4 who share a mutual friend with user ID 3, hence not included.
- Users 1 and 5 are friends with each other, but they share a mutual friend with user ID 2, so this pair is not included.
- Users 6 and 7, as well as users 8 and 9, are friends with each other, and they don't have any mutual friends, hence included.
- Users 3 and 4 are friends with each other, but their mutual connection with user ID 2 means they are not included, similarly for users 2 and 5 are friends but are excluded due to their mutual connection with user ID 1.
Output table is ordered by user_id1 in ascending order.
3060. User Activities within Time Bounds
Hard
Topics
SQL Schema
Pandas Schema
Table: Sessions
+---------------+----------+
| Column Name | Type |
+---------------+----------+
| user_id | int |
| session_start | datetime |
| session_end | datetime |
| session_id | int |
| session_type | enum |
+---------------+----------+
session_id is column of unique values for this table.
session_type is an ENUM (category) type of (Viewer, Streamer).
This table contains user id, session start, session end, session id and session type.
Write a solution to find the the users who have had at least two session of the same type (either 'Viewer' or 'Streamer') with a maximum gap of 12 hours between sessions.
Return the result table ordered by user_id in ascending order.
The result format is in the following example.
Example:
Input:
Sessions table:
+---------+---------------------+---------------------+------------+--------------+
| user_id | session_start | session_end | session_id | session_type |
+---------+---------------------+---------------------+------------+--------------+
| 101 | 2023-11-01 08:00:00 | 2023-11-01 09:00:00 | 1 | Viewer |
| 101 | 2023-11-01 10:00:00 | 2023-11-01 11:00:00 | 2 | Streamer |
| 102 | 2023-11-01 13:00:00 | 2023-11-01 14:00:00 | 3 | Viewer |
| 102 | 2023-11-01 15:00:00 | 2023-11-01 16:00:00 | 4 | Viewer |
| 101 | 2023-11-02 09:00:00 | 2023-11-02 10:00:00 | 5 | Viewer |
| 102 | 2023-11-02 12:00:00 | 2023-11-02 13:00:00 | 6 | Streamer |
| 101 | 2023-11-02 13:00:00 | 2023-11-02 14:00:00 | 7 | Streamer |
| 102 | 2023-11-02 16:00:00 | 2023-11-02 17:00:00 | 8 | Viewer |
| 103 | 2023-11-01 08:00:00 | 2023-11-01 09:00:00 | 9 | Viewer |
| 103 | 2023-11-02 20:00:00 | 2023-11-02 23:00:00 | 10 | Viewer |
| 103 | 2023-11-03 09:00:00 | 2023-11-03 10:00:00 | 11 | Viewer |
+---------+---------------------+---------------------+------------+--------------+
Output:
+---------+
| user_id |
+---------+
| 102 |
| 103 |
+---------+
Explanation:
- User ID 101 will not be included in the final output as they do not have any two sessions of the same session type.
- User ID 102 will be included in the final output as they had two viewer sessions with session IDs 3 and 4, respectively, and the time gap between them was less than 12 hours.
- User ID 103 participated in two viewer sessions with a gap of less than 12 hours between them, identified by session IDs 10 and 11. Therefore, user 103 will be included in the final output.
Output table is ordered by user_id in increasing order.
3061. Calculate Trapping Rain Water
Hard
Topics
SQL Schema
Pandas Schema
Table: Heights
+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| height | int |
+-------------+------+
id is the primary key (column with unique values) for this table, and it is guaranteed to be in sequential order.
Each row of this table contains an id and height.
Write a solution to calculate the amount of rainwater can be trapped between the bars in the landscape, considering that each bar has a width of 1 unit.
The elevation map depicted above (in the black section) is graphically represented with the x-axis denoting the id and the y-axis representing the heights [0,1,0,2,1,0,1,3,2,1,2,1]. In this scenario, 6 units of rainwater are trapped within the blue section.
# Write your MySQL query statement below
SELECT SUM(LEAST(left_max, right_max) - height) AS total_trapped_water