fundamentals/database/notes/07-groupby-functions-worksheet.md

111 lines
1.7 KiB
Markdown
Raw Normal View History

2022-09-27 15:27:53 +01:00
# Queries
## 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
```
## Built-in Functions
1. Get the average IQ of all the students rounded to the nearest integer.
```sql
```
2. Find all batches whose name is longer than 10 characters.
```sql
```
3. Find all batches whose name's first 10 characters contains the string `sher`
```sql
```
4. Get all batches that have started on a Sunday
```sql
```
5. Get all batches that have been running for more than 10 years
```sql
```
6. Print the name and the instructor's id for each batch. If no instructor is assigned, print `NO INSTRUCTOR`.
```sql
```
7. Print the name and IQ of each student. If the IQ of the student is less than 100, print `LOW IQ` instead.
```sql
```
8. For each student print the name and their IQ category.
| IQ | Category |
| --------- | -------- |
| < 100 | LOW IQ |
| 100 - 150 | MEDIUM |
| > 150 | HIGH |
```sql
```