Finishes aggregate questions.

This commit is contained in:
Tanmay 2022-07-20 14:47:29 +01:00
parent f2bf278d05
commit bd15293a27
3 changed files with 402 additions and 0 deletions

View File

@ -0,0 +1,171 @@
# Queries
## Joins
1. Get all batch names along with their instructor names.
```sql
SELECT
b.name, i.first_name, i.last_name
FROM
batches b
JOIN
instructors i ON b.instructor_id = i.id;
```
2. Get all students along with their batch names if present else `NULL`.
```sql
SELECT
s.first_name, s.last_name, b.name
FROM
students s
LEFT JOIN
batches b ON s.batch_id = b.id;
```
3. Get all students along with their batch names. Also, fetch all the batches which have no students.
```sql
SELECT
s.first_name, s.last_name, b.name
FROM
students s
RIGHT JOIN
batches b ON s.batch_id = b.id
```
4. Get all the combinations of batches and instructors.
```sql
SELECT
b.name, i.first_name, i.last_name
FROM
batches b, instructors i
```
5. Get all students with their instructors. If a student has no instructor, then show `NULL` for the instructor's name.
```sql
SELECT
s.first_name, s.last_name, i.first_name, i.last_name
FROM
students s
LEFT JOIN
batches b ON s.batch_id = b.id
LEFT JOIN
instructors i ON b.instructor_id = i.id;
```
## Aggregation
1. Get the maximum IQ in all students (Try without aggregation first).
```sql
SELECT
first_name, iq
FROM
students
ORDER BY iq DESC
LIMIT 1;
```
2. Get the maximum IQ in all students (With aggregation).
```sql
SELECT
MAX(IQ) AS 'IQ'
FROM
students;
```
3. Get the oldest batch from the batches table.
```sql
SELECT
MIN(start_date) AS 'start date'
FROM
batches;
```
4. Fetch the number of batches that start with the word `Jedi`.
```sql
SELECT
COUNT(id)
FROM
batches
WHERE
name LIKE 'Jedi%';
```
5. Get the average IQ of all students (Without using `AVG`)
```sql
SELECT
SUM(iq) / COUNT(iq) as 'Average IQ'
FROM
students;
```
6. Get the average IQ of students in all batches.
```sql
SELECT
AVG(IQ) AS 'IQ'
FROM
students
WHERE
batch_id IS NOT NULL;
```
7. Find the average IQ of students in each batch.
```sql
SELECT
batch_id, AVG(iq)
FROM
students
GROUP BY batch_id;
```
8. Find the total number of students in each batch.
```sql
SELECT
b.name, COUNT(s.id)
FROM
batches b
LEFT JOIN
students s ON b.id = s.batch_id
GROUP BY b.id;
```
9. Get the total number of batches taught by each instructor.
```sql
SELECT
i.first_name, COUNT(b.id)
FROM
instructors i
LEFT JOIN
batches b ON i.id = b.instructor_id
GROUP BY i.id;
```
10. Find the average IQ of students in batches with batch ID `1` and `2`.
```sql
SELECT
batch_id, AVG(iq)
FROM
students
WHERE batch_id IN (1, 2)
GROUP BY batch_id;
```
11. Find count of students that are part of batches that have average IQ greater than `120`.
```sql
SELECT
batch_id, AVG(iq) as avg_iq, COUNT(iq)
FROM
students
GROUP BY batch_id
HAVING avg_iq > 130;
```

View File

@ -0,0 +1,80 @@
# Queries
## Joins
1. Get all batch names along with their instructor names.
```sql
```
2. Get all students along with their batch names if present else `NULL`.
```sql
```
3. Get all students along with their batch names. Also, fetch all the batches which have no students.
```sql
```
4. Get all the combinations of batches and instructors.
```sql
```
5. Get all students with their instructors. If a student has no instructor, then show `NULL` for the instructor's name.
```sql
```
## Aggregation
1. Get the maximum IQ in all students (Try without aggregation first).
```sql
```
2. Get the maximum IQ in all students (With aggregation).
```sql
```
3. Get the oldest batch from the batches table.
```sql
```
4. Fetch the number of batches that start with the word `Jedi`.
```sql
```
5. Get the average IQ of all students (Without using `AVG`)
```sql
```
6. Get the average IQ of students in all batches.
```sql
```
7. Find the average IQ of students in each batch.
```sql
```
8. Find the total number of students in each batch.
```sql
```
9. Get the total number of batches taught by each instructor.
```sql
```
10. Find the average IQ of students in batches with batch ID `1` and `2`.
```sql
```
11. Find count of students that are part of batches that have average IQ greater than `120`.
```sql
```

View File

@ -177,3 +177,154 @@ Note that different from the INNER JOIN, LEFT JOIN , and RIGHT JOIN clauses, the
If you add a WHERE clause, in case table t1 and t2 has a relationship, the CROSS JOIN works like the INNER JOIN.
---
## Aggregate Functions
> MySQL's aggregate function is used to perform calculations on multiple values and return the result in a single value like the average of all values, the sum of all values, and maximum & minimum value among certain groups of values.
> We mainly use the aggregate functions in databases, spreadsheets and many other data manipulation software packages. In the context of business, different organization levels need different information such as top levels managers interested in knowing whole figures and not the individual details. These functions produce the summarised data from our database. Thus, they are extensively used in economics and finance to represent the economic health or stock and sector performance.
The ISO SQL standard defines the following aggregate functions:
| Function | Description |
| -------- | --------------------------------------------- |
| MIN | Return the minimum value |
| MAX | Return the maximum value |
| SUM | Return the sum of all values |
| AVG | Return the average value of the argument |
| COUNT | Return a count of the number of rows returned |
### Get the minimum value
**Keyword**: `MIN`
**Syntax**: `SELECT MIN(column_name) FROM table_name`
Getting the minimum value without using the aggregate function could be done by using the `ORDER BY` clause.
```sql
SELECT
first_name, iq
FROM
students
ORDER BY iq
LIMIT 1;
```
This would give the correct answer but not in the case of NULL values. By default, MySQL will return the NULL value at the top of the sorted rows. Special handling will have to be done to get the correct result.
This is why we can use the aggregate function `MIN` to get the minimum value.
```sql
SELECT
MIN(iq)
FROM
students;
```
**The aggregate functions ignore the NULL values.**
### Get the maximum value
**Keyword**: `MAX`
**Syntax**: `SELECT MAX(column_name) FROM table_name`
To get the maximum IQ in students, we can use the aggregate function `MAX`.
```sql
SELECT
MAX(iq)
FROM
students;
```
### Get the sum of all values
**Keyword**: `SUM`
**Syntax**: `SELECT SUM(column_name) FROM table_name`
To get the sum of all the IQ in students, we can use the aggregate function `SUM`.
```sql
SELECT
SUM(iq)
FROM
students;
```
### Get the average value
**Keyword**: `AVG`
**Syntax**: `SELECT AVG(column_name) FROM table_name`
To get the average IQ in students, we can use the aggregate function `AVG`.
```sql
SELECT
AVG(iq)
FROM
students;
```
### Get the number of rows
**Keyword**: `COUNT`
**Syntax**: `SELECT COUNT(column_name) FROM table_name`
To get the number of rows in students, we can use the aggregate function `COUNT`.
```sql
SELECT
COUNT(*)
FROM
students;
```
Using `*` instead of a column name will return the number of rows in the table whereas using a column name will return the number of rows that have a non-null value in a column.
## GROUP BY clause
The GROUP BY clause groups a set of rows into a set of summary rows by values of columns or expressions. The GROUP BY clause returns one row for each group. In other words, it reduces the number of rows in the result set
**Keyword**: `GROUP BY`
**Syntax**: `SELECT column1, column2, ... FROM table_name GROUP BY column1, column2, ...`
In this syntax, you place the GROUP BY clause after the FROM and WHERE clauses. After the GROUP BY keywords, you place is a list of comma-separated columns or expressions to group rows.
> MySQL evaluates the GROUP BY clause after the FROM and WHERE clauses and before the HAVING, SELECT, DISTINCT, ORDER BY and LIMIT clauses
![Group By](https://www.mysqltutorial.org/wp-content/uploads/2021/07/MySQL-Group-By.svg)
Fields that are not encapsulated within an aggregate function and must be included in the GROUP BY Clause at the end of the SQL statement.
For example, the following query will return the number of students in each batch:
```sql
SELECT
batch_id, AVG(iq)
FROM
students
GROUP BY batch_id;
```
You can also use the `HAVING` clause to filter the result set.
For example, the following query will return the number of students in each batch where each batches average IQ is greater than or equal to 100:
```sql
SELECT
batch_id, AVG(iq)
FROM
students
GROUP BY batch_id
HAVING AVG(iq) >= 100;
```
## Problems
1. [Simple - I](https://leetcode.com/problems/big-countries/)
2. [Simple - II](https://leetcode.com/problems/find-customer-referee//)
3. [Joins - I](https://leetcode.com/problems/employees-earning-more-than-their-managers/)
4. [Joins - II](https://leetcode.com/problems/combine-two-tables/)
5. [Aggregation - I](https://leetcode.com/problems/duplicate-emails/)
6. [Join - III](https://leetcode.com/problems/delete-duplicate-emails/)
7. [Aggregation - II](https://leetcode.com/problems/customer-placing-the-largest-number-of-orders/)
8. [Aggregation - III](https://leetcode.com/problems/classes-more-than-5-students/)