Skip to content

Commit cf72878

Browse files
hamza added basic aggregate functions
1 parent 654f649 commit cf72878

File tree

4 files changed

+260
-0
lines changed

4 files changed

+260
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [Not Boring Movies](https://leetcode.com/problems/not-boring-movies/description/?envType=study-plan-v2&envId=top-sql-50)
2+
3+
### Problem Requirements:
4+
5+
Report the movies with an odd-numbered ID and a description that is not <code>"boring"</code>.
6+
7+
Return the result table ordered by <code>rating</code> in <strong> descending 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 a <code>MOD(num1, num2)</code> function that returns the value of <code>num1 % num2</code> (with <code>%</code> beging the modulo operator) </p>
15+
</details>
16+
17+
<details>
18+
<summary>Hint#2</summary>
19+
<p>SQL has a <code>NOT</code> operator </p>
20+
</details>
21+
22+
</details>
23+
24+
<details>
25+
<summary style="font-size:1.3rem;"> <strong>Explanation</strong> </summary>
26+
27+
In order to solve this problem we need to use the <code>mod</code> function to get the odd numbered IDs and the <code>not</code> operator to get the movies with description not equal to <code>"boring"</code>.
28+
29+
Then, we can combine the two conditions using the <code>and</code> operator.
30+
31+
Finally, we can order the result by <code>rating</code> in descending order using <code>desc</code> keyword.
32+
33+
</details>
34+
35+
<details>
36+
<summary style="font-size:1.3rem"><strong> SQL Solution</strong> </summary>
37+
38+
```sql
39+
select id, movie, description, rating from Cinema
40+
where mod(id, 2)=1 and not description = "boring"
41+
order by rating desc;
42+
```
43+
44+
</details>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# [Project Employees I](https://leetcode.com/problems/project-employees-i/description/?envType=study-plan-v2&envId=top-sql-50)
2+
3+
### Problem Requirements:
4+
5+
Report the <code>average</code> experience years of all the employees for each project, <strong> rounded to 2 digits</strong>.
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>When we have more than one tables we must think about joins first</p>
15+
</details>
16+
17+
<details>
18+
<summary>Hint#2</summary>
19+
<p>SQL has a function <code>AVG(column_name)</code> returns the average value of that column </p>
20+
</details>
21+
22+
<details>
23+
<summary>Hint#3</summary>
24+
<p>Here we are asked to find the average for each project, we must think about <strong>Grouping</strong> </p>
25+
</details>
26+
27+
</details>
28+
29+
<details>
30+
<summary style="font-size:1.3rem;"> <strong>Explanation</strong> </summary>
31+
32+
We want to find the average experience years for each project, so we must join the two tables on the <code>employee_id</code> column, then we must group the result by the <code>project_id</code> column, and finally we must find the average of the <code>experience_years</code> column.
33+
34+
When selecting the average of a column we must use the <code>AVG(column_name)</code> function, and to round the result to 2 digits we must use the <code>ROUND()</code> function, then we use <code>AS</code> to give the column an alias (<code>average_years</code>)
35+
36+
</details>
37+
38+
<details>
39+
<summary style="font-size:1.3rem"><strong> SQL Solution</strong> </summary>
40+
41+
```sql
42+
select p.project_id, round(avg(e.experience_years),2) as average_years
43+
from Project p
44+
inner join Employee e on e.employee_id = p.employee_id
45+
group by p.project_id;
46+
```
47+
48+
</details>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# [Queries Quality and Percentage](https://leetcode.com/problems/queries-quality-and-percentage/description/?envType=study-plan-v2&envId=top-sql-50)
2+
3+
### Problem Requirements:
4+
5+
We define query <code>quality</code> as:
6+
7+
The average of the ratio between query rating and its position.
8+
9+
We also define <code>poor_query_percentage</code> as:
10+
11+
The percentage of all queries with rating less than 3.
12+
13+
Find each <code>query_name</code>, the <code>quality</code> and <code>poor_query_percentage</code>.
14+
15+
Both <code>quality</code> and <code>poor_query_percentage</code> should be rounded to <code>2</code> decimal places.
16+
17+
Return the result table in any order.
18+
19+
<!-- <details open>
20+
<summary style="font-size:1.5rem;"> <strong>Solution#1</strong> </summary> -->
21+
22+
<details>
23+
<summary style="font-size:1.3rem;"> <strong>Hints</strong> </summary>
24+
25+
<details>
26+
<summary>Hint#1</summary>
27+
<p>How SQL <code>AVG</code> function works ? <br>
28+
<code>AVG(column_name) = SUM(column_name) / COUNT(column_name)</code>
29+
</p>
30+
</details>
31+
32+
<details>
33+
<summary>Hint#2</summary>
34+
<p>SQL has something called <code>CASE</code> Expression <br>
35+
<code>CASE WHEN condition THEN value1 ELSE value2 END</code>
36+
</p>
37+
</details>
38+
39+
<details>
40+
<summary>Hint#3</summary>
41+
<p>Try combining <code>AVG</code> and <code>CASE</code> together</p>
42+
</details>
43+
<details>
44+
<summary>Hint#5</summary>
45+
<p> Use Grouping </p>
46+
</details>
47+
<details>
48+
<summary>Hint#4</summary>
49+
<p> Note that <code>query_name</code> may be <code>NULL</code> </p>
50+
</details>
51+
52+
</details>
53+
54+
<details>
55+
<summary style="font-size:1.3rem;"> <strong>Explanation</strong> </summary>
56+
57+
This problem is a bit tricky, we need to calculate the <code>quality</code> and <code>poor_query_percentage</code> for each <code>query_name</code>.
58+
59+
First we must think about Grouping the data by <code>query_name</code>, then we can calculate the <code>quality</code> and <code>poor_query_percentage</code> for each group using <code>AVG</code> function.
60+
61+
The <code>quality</code> is the average of the ratio between query rating and its position <code>q1.rating / q1.position</code>, so we can use <code>AVG</code> as following: <code>AVG(q1.rating / q1.position)</code>
62+
63+
The <code>poor_query_percentage</code> is the percentage of all queries with rating less than <code>3</code>, so how do we get the avarge of this condition ? we can use <code>CASE</code> expression to check if the rating is less than <code>3</code>, then we can calculate the average of this condition, do not forget to multiply the avarage by <code>100</code> to get the percentage.
64+
65+
Do not forget to round the results to <code>2</code> decimal places.
66+
67+
Also do not forget to filter out the <code>NULL</code> values in <code>query_name</code> column.
68+
69+
</details>
70+
71+
<details>
72+
<summary style="font-size:1.3rem"><strong> SQL Solution</strong> </summary>
73+
74+
```sql
75+
select
76+
q1.query_name as query_name,
77+
round(avg(q1.rating / q1.position), 2) as quality,
78+
round(100 * avg(case when q1.rating < 3 then 1 else 0 end), 2) as poor_query_percentage
79+
from Queries q1
80+
where q1.query_name is not null
81+
group by query_name;
82+
```
83+
84+
</details>
85+
86+
<!-- </details> -->
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [Immediate Food Delivery II](https://leetcode.com/problems/immediate-food-delivery-ii/description/?envType=study-plan-v2&envId=top-sql-50)
2+
3+
### Problem Requirements:
4+
5+
If the customer's <strong> preferred delivery date </strong> is the same as the <strong>order date</strong>, then the order is called <strong>immediate</strong>; otherwise, it is called scheduled.
6+
7+
The first order of a customer is the order with the <strong>earliest order date</strong> that the customer made. It is <strong>guaranteed</strong> that a customer has precisely one first order.
8+
9+
Find the <strong>percentage of immediate orders in the first orders of all customers</strong>, rounded to <strong>2</strong> decimal places.
10+
11+
<details>
12+
<summary style="font-size:1.3rem;"> <strong>Hints</strong> </summary>
13+
14+
<details>
15+
<summary>Hint#1</summary>
16+
<p>Immediate order occurres when <code>order_date = customer_pref_delivery_date</code></p>
17+
</details>
18+
19+
<details>
20+
<summary>Hint#2</summary>
21+
<p>Use <code>CASE</code> Expression and the <code>SUM</code> function to find the number of Immediate orders.
22+
What will the whole sample sapce of orders be ?</p>
23+
</details>
24+
25+
<details>
26+
<summary>Hint#3</summary>
27+
<p>The sample space is the number of all orders in the table we can get it using <code>COUNT(*)</code> </p>
28+
</details>
29+
30+
<details>
31+
<summary>Hint#4</summary>
32+
<p>How would you find the <code>MIN</code> <strong>order_date</strong> for each customer ? </p>
33+
</details>
34+
35+
<details>
36+
<summary>Hint#5</summary>
37+
<p>Use <code>WHERE IN</code> clause (subquering) </p>
38+
</details>
39+
40+
</details>
41+
42+
<details>
43+
<summary style="font-size:1.3rem;"> <strong>Explanation</strong> </summary>
44+
45+
This problem is somewhat tough, we need to calculate the percentage of immediate orders in the first orders of all customers.
46+
47+
First we need to find a way to find the immediate orders. But, before that we must also find a way to find the first order of each customer.
48+
49+
To find the first order of each customer we will be using <code>WHERE IN</code> clause.
50+
51+
The <code>WHERE IN</code> clause is used to filter the result of a subquery. It extracts the rows from the outer query and then compares it with the result of the subquery. It looks like this:
52+
53+
```sql
54+
WHERE (col1,col2) IN (
55+
SELECT SOME_FUNCTION(col1), SOME_FUNCTION(col2)
56+
FROM tableX
57+
WHERE condition
58+
);
59+
```
60+
61+
Now we need to find the number of immediate orders, we can do that using <code>CASE</code> expression and <code>SUM</code> function.
62+
63+
Since we did filter the Delivery table to have only the <code>customer_id</code> with the <strong>first order made </strong> we can now check the use <code>SUM</code> funtion on the <code>CASE</code> condition to find the number of <code>order_date = customer_pref_delivery_date</code> inside the <code>CASE</code> expression to find the count of immediate orders.
64+
65+
We can divide the Summetion by the <code>COUNT(\*)</code> then multiply by <code>100</code> to get the percentage. Of course do not forget to <strong>round</strong> to <strong>2</strong> decimal places.
66+
67+
</details>
68+
69+
<details>
70+
<summary style="font-size:1.3rem"><strong> SQL Solution</strong> </summary>
71+
72+
```sql
73+
SELECT ROUND(100 * SUM(CASE WHEN order_date = customer_pref_delivery_date THEN 1 ELSE 0 END) / COUNT(*), 2) AS immediate_percentage
74+
FROM Delivery
75+
WHERE (customer_id,order_date) IN (
76+
SELECT customer_id, MIN(order_date) AS od
77+
FROM Delivery
78+
GROUP BY customer_id
79+
);
80+
```
81+
82+
</details>

0 commit comments

Comments
 (0)