Skip to content

Commit 28ff254

Browse files
fixed merge
2 parents 4562280 + 9c622aa commit 28ff254

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hints.txt
2+
script.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [Average Selling Price](https://leetcode.com/problems/average-selling-price/description/?envType=study-plan-v2&envId=top-sql-50)
2+
3+
### Problem Requirements:
4+
5+
Find the average selling price for each product. <code>average_price</code> should be rounded to 2 decimal places.
6+
7+
Return the result table in any order
8+
9+
<details>
10+
<summary style="font-size:1.3rem;"> <strong>Hints</strong> </summary>
11+
12+
<details>
13+
<summary>Hint#1</summary>
14+
<p>Use <code>OUTER JOIN</code></p>
15+
</details>
16+
<details>
17+
<summary>Hint#2</summary>
18+
<p>JOIN ON <code>product_id </code>AND <code>purchase_date</code></p>
19+
</details>
20+
<details>
21+
<summary>Hint#3</summary>
22+
<p>SQL has an aggregation function called <code>SUM(expression)</code> which calculate the sum of values in a set</p>
23+
</details>
24+
<details>
25+
<summary>Hint#4</summary>
26+
<p>SQL has a <code>ROUND(number, decimals)</code> function which rounds a number to a specified number of decimal places. </p>
27+
</details>
28+
<details>
29+
<summary>Hint#5</summary>
30+
<p>SQL has a <code>COALESCE(val1 , val2 , ....)</code> function which Return the first non-null value in a list </p>
31+
</details>
32+
<details>
33+
<summary>Hint#6</summary>
34+
<p>Use <code>GROUP BY</code> clause to group a set of rows into a set of summary rows</p>
35+
</details>
36+
37+
</details>
38+
39+
<details>
40+
<summary style="font-size:1.3rem;"> <strong>Explanation</strong> </summary>
41+
42+
To find the average_price for each product we want should calculate the total money paid and the total units bought then divide them as follows
43+
44+
45+
$$
46+
round(\frac{SUM(price \times units)}{SUM(units)} , 2)
47+
$$
48+
49+
we will <code> JOIN</code> these tables <code> ON</code> <code> product_id </code> and <code> purchase_date</code>. The <code>purchase_date</code> should be between <code>start_date</code> and <code>end_date</code>.
50+
<ul>
51+
<li>To calculate the total money we can use <code>SUM()</code> function to sum the money of each record. The money of each record equals the <code>price</code>multiplied by <code>units</code>. </li>
52+
<li>To calculate the total units we also can use <code>SUM()</code> function </li>.
53+
<li>To calculate the <code>average_price</code> we should divide total money by total units then use <code>ROUND(number , decimals)</code> to round the result. </li>
54+
</ul>
55+
But what if the total units is zero here we will have <code>null</code>. To solve this problem we can use <code>COALESCE()</code> function. This function will return the first non-null value in a list.
56+
</details>
57+
58+
<details>
59+
<summary style="font-size:1.3rem"><strong> SQL Solution</strong> </summary>
60+
61+
62+
63+
```sql
64+
SELECT
65+
Prices.product_id,
66+
COALESCE(
67+
ROUND(
68+
SUM(Prices.price * UnitsSold.units) / SUM(UnitsSold.units),
69+
2
70+
),
71+
0
72+
) AS average_price
73+
FROM
74+
Prices
75+
LEFT OUTER JOIN UnitsSold ON Prices.product_id = UnitsSold.product_id
76+
AND UnitsSold.purchase_date >= Prices.start_date
77+
AND UnitsSold.purchase_date <= end_date
78+
GROUP BY
79+
product_id
80+
```
81+
</details>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [Percentage of Users Attended a Contest](https://leetcode.com/problems/percentage-of-users-attended-a-contest/description/?envType=study-plan-v2&envId=top-sql-50)
2+
3+
### Problem Requirements:
4+
5+
Find the percentage of the users registered in each contest rounded to <strong>two decimals</strong>.
6+
7+
Return the result table ordered by <code>percentage</code> in <strong>descending order</strong>. In case of a tie, order it by <code>contest_id</code> in <strong>ascending order</strong>.
8+
9+
<details>
10+
<summary style="font-size:1.3rem;"> <strong>Hints</strong> </summary>
11+
12+
<details>
13+
<summary>Hint#1</summary>
14+
<p>SQL has an aggregation function called <code>COUNT(expression)</code> which count all the rows that satisfy a specified condition</p>
15+
</details>
16+
<details>
17+
<summary>Hint#2</summary>
18+
<p>Use <code>subquery</code></p>
19+
</details>
20+
21+
</details>
22+
23+
<details>
24+
<summary style="font-size:1.3rem;"> <strong>Explanation</strong> </summary>
25+
26+
To find the percentage of the users registered in each contest we should count the number of user that have registered into a contest and the total number of users then divide.
27+
<br>
28+
<br>
29+
<ul>
30+
<li>
31+
calculate the number of users that registered in each contest using <code>COUNT()</code> function.
32+
</li>
33+
<li>
34+
calculate the total number of user using <code>COUNT()</code> function as a <code><strong>sub query</strong></code>. By using <code>*</code> <i>(means all)</i> we can count the number of records in <code>Users</code> table.
35+
</li>
36+
<li>
37+
divide both number and multiply them by <code>100</code> Then use <code>ROUND(number , decimals)</code> to round the result.
38+
</li>
39+
</ul>
40+
</details>
41+
42+
<details>
43+
<summary style="font-size:1.3rem"><strong> SQL Solution</strong> </summary>
44+
45+
46+
```sql
47+
SELECT
48+
contest_id,
49+
ROUND(
50+
COUNT(user_id)/(
51+
SELECT
52+
COUNT(user_id)
53+
FROM
54+
Users
55+
)* 100
56+
, 2) AS percentage
57+
FROM
58+
Register
59+
GROUP BY
60+
contest_id
61+
ORDER BY
62+
percentage DESC,
63+
contest_id
64+
```
65+
66+
</details>

0 commit comments

Comments
 (0)