Skip to content

Commit 79f4540

Browse files
committed
Add SQL solutions
* 1211 * 1633
1 parent 56b50db commit 79f4540

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
-- 1211. Queries Quality and Percentage
2+
-- Easy
3+
-- https://leetcode.com/problems/queries-quality-and-percentage
4+
5+
/*
6+
Table: Queries
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| query_name | varchar |
11+
| result | varchar |
12+
| position | int |
13+
| rating | int |
14+
+-------------+---------+
15+
There is no primary key for this table, it may have duplicate rows.
16+
This table contains information collected from some queries on a database.
17+
The position column has a value from 1 to 500.
18+
The rating column has a value from 1 to 5. Query with rating less than 3 is a poor query.
19+
We define query quality as:
20+
The average of the ratio between query rating and its position.
21+
We also define poor query percentage as:
22+
The percentage of all queries with rating less than 3.
23+
Write an SQL query to find each query_name, the quality and poor_query_percentage.
24+
Both quality and poor_query_percentage should be rounded to 2 decimal places.
25+
Return the result table in any order.
26+
The query result format is in the following example.
27+
28+
Example 1:
29+
Input:
30+
Queries table:
31+
+------------+-------------------+----------+--------+
32+
| query_name | result | position | rating |
33+
+------------+-------------------+----------+--------+
34+
| Dog | Golden Retriever | 1 | 5 |
35+
| Dog | German Shepherd | 2 | 5 |
36+
| Dog | Mule | 200 | 1 |
37+
| Cat | Shirazi | 5 | 2 |
38+
| Cat | Siamese | 3 | 3 |
39+
| Cat | Sphynx | 7 | 4 |
40+
+------------+-------------------+----------+--------+
41+
Output:
42+
+------------+---------+-----------------------+
43+
| query_name | quality | poor_query_percentage |
44+
+------------+---------+-----------------------+
45+
| Dog | 2.50 | 33.33 |
46+
| Cat | 0.66 | 33.33 |
47+
+------------+---------+-----------------------+
48+
Explanation:
49+
Dog queries quality is ((5 / 1) + (5 / 2) + (1 / 200)) / 3 = 2.50
50+
Dog queries poor_ query_percentage is (1 / 3) * 100 = 33.33
51+
Cat queries quality equals ((2 / 5) + (3 / 3) + (4 / 7)) / 3 = 0.66
52+
Cat queries poor_ query_percentage is (1 / 3) * 100 = 33.33
53+
*/
54+
55+
SELECT
56+
query_name,
57+
ROUND(AVG(1.0 * rating / position), 2) AS quality,
58+
ROUND(100 * SUM(CASE WHEN rating < 3 THEN 1 ELSE 0 END)/ COUNT(*), 2) AS poor_query_percentage
59+
FROM
60+
Queries
61+
GROUP BY
62+
query_name;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
-- 1633. Percentage of Users Attended a Contest
2+
-- Easy
3+
-- https://leetcode.com/problems/percentage-of-users-attended-a-contest
4+
5+
/*
6+
Table: Users
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| user_id | int |
11+
| user_name | varchar |
12+
+-------------+---------+
13+
user_id is the primary key for this table.
14+
Each row of this table contains the name and the id of a user.
15+
16+
Table: Register
17+
+-------------+---------+
18+
| Column Name | Type |
19+
+-------------+---------+
20+
| contest_id | int |
21+
| user_id | int |
22+
+-------------+---------+
23+
(contest_id, user_id) is the primary key for this table.
24+
Each row of this table contains the id of a user and the contest they registered into.
25+
26+
Write an SQL query to find the percentage of the users registered in each contest rounded to two decimals.
27+
Return the result table ordered by percentage in descending order. In case of a tie, order it by contest_id in ascending order.
28+
The query result format is in the following example.
29+
30+
Example 1:
31+
Input:
32+
Users table:
33+
+---------+-----------+
34+
| user_id | user_name |
35+
+---------+-----------+
36+
| 6 | Alice |
37+
| 2 | Bob |
38+
| 7 | Alex |
39+
+---------+-----------+
40+
Register table:
41+
+------------+---------+
42+
| contest_id | user_id |
43+
+------------+---------+
44+
| 215 | 6 |
45+
| 209 | 2 |
46+
| 208 | 2 |
47+
| 210 | 6 |
48+
| 208 | 6 |
49+
| 209 | 7 |
50+
| 209 | 6 |
51+
| 215 | 7 |
52+
| 208 | 7 |
53+
| 210 | 2 |
54+
| 207 | 2 |
55+
| 210 | 7 |
56+
+------------+---------+
57+
Output:
58+
+------------+------------+
59+
| contest_id | percentage |
60+
+------------+------------+
61+
| 208 | 100.0 |
62+
| 209 | 100.0 |
63+
| 210 | 100.0 |
64+
| 215 | 66.67 |
65+
| 207 | 33.33 |
66+
+------------+------------+
67+
Explanation:
68+
All the users registered in contests 208, 209, and 210. The percentage is 100% and we sort them in the answer table by contest_id in ascending order.
69+
Alice and Alex registered in contest 215 and the percentage is ((2/3) * 100) = 66.67%
70+
Bob registered in contest 207 and the percentage is ((1/3) * 100) = 33.33%
71+
*/
72+
73+
SELECT
74+
contest_id,
75+
ROUND(COUNT(user_id)*100.00/(SELECT COUNT(*) FROM Users), 2) as percentage
76+
FROM
77+
Register
78+
GROUP BY
79+
contest_id
80+
ORDER BY
81+
percentage DESC,
82+
contest_id;

0 commit comments

Comments
 (0)