Skip to content

Commit 7fdd585

Browse files
authored
Merge pull request #5 from gymgle/master
add database sql 50 solutions
2 parents f5d1780 + 8c377bf commit 7fdd585

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--https://leetcode.com/problems/percentage-of-users-attended-a-contest/description/
2+
3+
# Write your MySQL query statement below
4+
SELECT
5+
r.contest_id,
6+
ROUND(COUNT(r.user_id) * 100.0 / (SELECT COUNT(*) FROM Users), 2) AS percentage
7+
FROM
8+
Register r
9+
GROUP BY
10+
r.contest_id
11+
ORDER BY
12+
percentage DESC,
13+
r.contest_id ASC;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--https://leetcode.com/problems/game-play-analysis-iv/description/
2+
3+
# Write your MySQL query statement below
4+
SELECT
5+
ROUND(COUNT(DISTINCT player_id) / (SELECT COUNT(DISTINCT player_id) FROM Activity), 2) AS fraction
6+
FROM
7+
Activity
8+
WHERE
9+
(player_id, DATE_SUB(event_date, INTERVAL 1 DAY))
10+
IN (
11+
SELECT player_id, MIN(event_date) AS first_login FROM Activity GROUP BY player_id
12+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--https://leetcode.com/problems/immediate-food-delivery-ii/description
2+
3+
# Write your MySQL query statement below
4+
SELECT ROUND(100 * SUM(
5+
CASE
6+
WHEN b.min_order_date = b.min_delivery_date THEN 1
7+
ELSE 0
8+
END
9+
) / COUNT(*), 2) AS immediate_percentage
10+
FROM (
11+
SELECT
12+
MIN(order_date) AS min_order_date,
13+
MIN(customer_pref_delivery_date) AS min_delivery_date
14+
FROM delivery
15+
GROUP BY customer_id
16+
) b;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--https://leetcode.com/problems/monthly-transactions-i/description/
2+
3+
# Write your MySQL query statement below
4+
SELECT
5+
SUBSTR(trans_date,1,7) AS month,
6+
country,
7+
COUNT(*) AS trans_count,
8+
COUNT(CASE WHEN state = 'approved' THEN 1 END) AS approved_count,
9+
SUM(amount) AS trans_total_amount,
10+
SUM(CASE WHEN state = 'approved' THEN amount else 0 END) AS approved_total_amount
11+
FROM
12+
Transactions
13+
GROUP BY
14+
month,
15+
country;

0 commit comments

Comments
 (0)