Skip to content

Commit b77c62f

Browse files
committed
Update readme.md
1 parent c3b0106 commit b77c62f

File tree

1 file changed

+192
-0
lines changed
  • LeetCode SQL 50 Solution/1211. Queries Quality and Percentage

1 file changed

+192
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
Here's a structured `README.md` file for **LeetCode 1211 - Queries Quality and Percentage**, formatted for a GitHub repository:
2+
3+
```md
4+
# 📊 Queries Quality and Percentage - LeetCode 1211
5+
6+
## 📌 Problem Statement
7+
You are given the **Queries** table, which contains information collected from various queries on a database.
8+
9+
### Queries Table
10+
| Column Name | Type |
11+
| ----------- | ------- |
12+
| query_name | varchar |
13+
| result | varchar |
14+
| position | int |
15+
| rating | int |
16+
17+
- The **position** column has values from **1 to 500**.
18+
- The **rating** column has values from **1 to 5**.
19+
- **Queries with rating < 3 are considered "poor queries".**
20+
21+
### Definitions:
22+
1️⃣ **Query Quality:**
23+
The **average** of the **ratio** between query rating and its position:
24+
\[
25+
\text{quality} = \frac{\sum (\text{rating} / \text{position})}{\text{total queries for that name}}
26+
\]
27+
28+
2️⃣ **Poor Query Percentage:**
29+
The percentage of all queries where **rating < 3**:
30+
\[
31+
\text{poor\_query\_percentage} = \left(\frac{\text{count of poor queries}}{\text{total queries}}\right) \times 100
32+
\]
33+
34+
---
35+
36+
## 📊 Example 1:
37+
### Input:
38+
**Queries Table**
39+
| query_name | result | position | rating |
40+
| ---------- | ---------------- | -------- | ------ |
41+
| Dog | Golden Retriever | 1 | 5 |
42+
| Dog | German Shepherd | 2 | 5 |
43+
| Dog | Mule | 200 | 1 |
44+
| Cat | Shirazi | 5 | 2 |
45+
| Cat | Siamese | 3 | 3 |
46+
| Cat | Sphynx | 7 | 4 |
47+
48+
### Output:
49+
| query_name | quality | poor_query_percentage |
50+
| ---------- | ------- | --------------------- |
51+
| Dog | 2.50 | 33.33 |
52+
| Cat | 0.66 | 33.33 |
53+
54+
### Explanation:
55+
#### **Dog**
56+
- **Quality Calculation:**
57+
\[
58+
\left( \frac{5}{1} + \frac{5}{2} + \frac{1}{200} \right) \div 3 = 2.50
59+
\]
60+
- **Poor Query Percentage:**
61+
- Poor Queries: **1** (Mule, rating = 1)
62+
- Total Queries: **3**
63+
\[
64+
(1 / 3) \times 100 = 33.33\%
65+
\]
66+
67+
#### **Cat**
68+
- **Quality Calculation:**
69+
\[
70+
\left( \frac{2}{5} + \frac{3}{3} + \frac{4}{7} \right) \div 3 = 0.66
71+
\]
72+
- **Poor Query Percentage:**
73+
- Poor Queries: **1** (Shirazi, rating = 2)
74+
- Total Queries: **3**
75+
\[
76+
(1 / 3) \times 100 = 33.33\%
77+
\]
78+
79+
---
80+
81+
## 🖥 SQL Solution
82+
83+
### 1️⃣ Standard MySQL Query
84+
#### Explanation:
85+
- **Calculate quality** using `AVG(rating / position)`.
86+
- **Count poor queries** using `SUM(CASE WHEN rating < 3 THEN 1 ELSE 0 END)`.
87+
- **Calculate percentage** using `(COUNT of poor queries / total queries) * 100`.
88+
89+
```sql
90+
SELECT query_name,
91+
ROUND(AVG(rating * 1.0 / position), 2) AS quality,
92+
ROUND(SUM(CASE WHEN rating < 3 THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 2) AS poor_query_percentage
93+
FROM Queries
94+
GROUP BY query_name;
95+
```
96+
97+
---
98+
99+
### 📝 Step-by-Step Breakdown:
100+
101+
1️⃣ **Grouping Queries by `query_name`**
102+
```sql
103+
GROUP BY query_name;
104+
```
105+
- Ensures calculations are **per query type**.
106+
107+
2️⃣ **Calculating Query Quality**
108+
```sql
109+
ROUND(AVG(rating * 1.0 / position), 2) AS quality
110+
```
111+
- **`rating / position`** calculates the ratio.
112+
- **`AVG(...)`** finds the average across all entries for the query.
113+
- **Multiplying by `1.0` ensures floating-point division.**
114+
- **`ROUND(..., 2)` rounds to 2 decimal places**.
115+
116+
3️⃣ **Calculating Poor Query Percentage**
117+
```sql
118+
ROUND(SUM(CASE WHEN rating < 3 THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 2) AS poor_query_percentage
119+
```
120+
- **Counts queries with `rating < 3` using `SUM(CASE WHEN ... THEN 1 ELSE 0 END)`**.
121+
- **Divides by total queries (`COUNT(*)`) and multiplies by `100`**.
122+
- **Rounds to 2 decimal places**.
123+
124+
---
125+
126+
### 2️⃣ Alternative MySQL Query (Using `IF` Instead of `CASE`)
127+
128+
```sql
129+
SELECT query_name,
130+
ROUND(AVG(rating * 1.0 / position), 2) AS quality,
131+
ROUND(SUM(IF(rating < 3, 1, 0)) * 100.0 / COUNT(*), 2) AS poor_query_percentage
132+
FROM Queries
133+
GROUP BY query_name;
134+
```
135+
- **`IF(rating < 3, 1, 0)`** is equivalent to `CASE WHEN rating < 3 THEN 1 ELSE 0 END`.
136+
137+
---
138+
139+
## 🐍 Pandas Solution (Python)
140+
#### Explanation:
141+
- **Group by `query_name`**.
142+
- **Calculate query quality** as `rating / position`, then average.
143+
- **Filter poor queries (`rating < 3`) and compute percentage**.
144+
145+
```python
146+
import pandas as pd
147+
148+
def queries_quality(queries: pd.DataFrame) -> pd.DataFrame:
149+
# Group by query_name
150+
grouped = queries.groupby("query_name")
151+
152+
# Compute Quality
153+
quality = grouped.apply(lambda x: round((x["rating"] / x["position"]).mean(), 2))
154+
155+
# Compute Poor Query Percentage
156+
poor_query_percentage = grouped.apply(lambda x: round((x["rating"] < 3).mean() * 100, 2))
157+
158+
# Return result
159+
result = pd.DataFrame({"query_name": quality.index,
160+
"quality": quality.values,
161+
"poor_query_percentage": poor_query_percentage.values})
162+
return result
163+
```
164+
165+
---
166+
167+
## 📁 File Structure
168+
```
169+
📂 Queries-Quality
170+
│── 📜 README.md
171+
│── 📜 solution.sql
172+
│── 📜 solution_pandas.py
173+
│── 📜 test_cases.sql
174+
```
175+
176+
---
177+
178+
## 🔗 Useful Links
179+
- 📖 [LeetCode Problem](https://leetcode.com/problems/queries-quality-and-percentage/)
180+
- 📚 [SQL `GROUP BY` Documentation](https://www.w3schools.com/sql/sql_groupby.asp)
181+
- 🐍 [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/groupby.html)
182+
```
183+
184+
### Features of this `README.md`:
185+
✅ **Clear problem statement with table structure**
186+
✅ **Examples with detailed calculations**
187+
✅ **SQL and Pandas solutions with explanations**
188+
✅ **Alternative SQL query for flexibility**
189+
✅ **File structure for GitHub organization**
190+
✅ **Useful reference links**
191+
192+
Let me know if you'd like any modifications! 🚀

0 commit comments

Comments
 (0)