Lecture 02 - Quiz 02 - Midterm - Advdbmgmt - Elms
Lecture 02 - Quiz 02 - Midterm - Advdbmgmt - Elms
Code
Result
Exercise:
1. Find the data for “Junior” students & limit the results to 2 candidates from table “MISSTUDENTS”
2. Find movies with a rating of ‘PG-13’ & Limit the result to 5 from the table “FILM” in Sakila Database
3. Find movies with a rating of ‘R’ & title that starts with ‘R’ from the table “FILM” in Sakila Database. Limit to 3 results.
4. Find data for payments that are $4.99 and limit the result to 5 from the table “Payment” in Sakila Database 2
MIN() & MAX() Functions in SQL
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
Code
Result
Result
Code Result
Result
Exercise:
1. Find the number of movies that has length less than 90 minutes
2. Find the number of movies that has a title starting with B
3. Find the number of the movies that has rental duration of 6 days or more. 6
ASC/DESC in SQL
Problem: Display 3 of the highest payments from the Payment Table from
the top to bottom.
• Exercise:
1. Display the 2 shortest movies from the Film_List Table from the bottom to
top.
2. Display the cheapest movie from the film_list Table from the bottom to top.
Note: Column names by which you should use asc/desc was not mentiond in any problem. You
have to guess what’s the right column to asc or desc. 7
Answer 01
8
Answer 02
9
GROUP BY in SQL
GROUP BY:
• The GROUP BY statement groups the rows that have repetitive
values & it is often used with aggregated/numeric functions.
• In other words, what would you make a group of if there is no repetitive
values?
Problem: Display the categories and number of released movies under
each categories top to bottom from the Film_List table and rename the
result column as “Number_of_Releases”.
• Exercise:
• Display the rating and number of movies based on rating bottom to top from the Film_List
table and rename the result column as “Number_of_Ratings”.
10
Answer (GROUP BY)
11
BETWEEN QUERIES in SQL
What does BETWEEN Statement return?
Between statement helps find data within a defined range.
Problem: Display the title, price and length of the movies from the
Film_List table where the length of the movies are between 90 & 91
minutes.
• Exercise:
• Display the Title, Category & Price from the table Film_List where
the price of the movie is between 0.99 & 1.00
12
Answer (BETWEEN
Statement)
13
Add/Drop a Column
Adding a Column
We will use the “ALTER” statement here.
Dropping a Column
15
Fill in data in new & Change data in existing column
Fill in data in a new column
We will use the “UPDATE” statement here to fill in new data in new
column.
16
Exercise (Add & UPDATE existing & new column)
3. Add a new column named DateJoined. Data Type should be DATE and format is YYYY-MM-DD.
Fill in dates between 2021 January & 2022 June, as you wish for each student.
Hint:
17
Answer (UPDATE existing
& new column)
1. 2.
3.
18
More with Dates
Find students who joined UIU between January 14 th, 2021 &June 30, 2022.
We can use both aggregated functions or between statement to get the result.
Or,
Exercise:
19
1. Find Students who Joined UIU between January 1 st, 2021 & January 05th, 2022.
Answer (Dates)
20
Float & Decimal Data
Add Column by position
Add two columns in Misstudents table named “GPA” &”BookStoreExpense”
Decimal: The decimal data type is used whenever we want to define the
exact range of numeric data before and after the dot in a particular column.
If the defined range for decimal is DECIMAL (6,2), it means:
6 is the value known as Precision, meaning the total length of
numeric data will be 6.
2 is the value known as Scale, meaning the total length of numeric
data after dot will be 2.
So, DECIMAL (6,2) = xxxx.xx
Float: The float data type is used when we have no defined range of numeric
data before and after dot, in fact may include various range, in a particular
column.
Column by Position: We will learn 3 positioning of new columns.
21
1. Last (By default), 2. First (The first column) 3. After (Anywhere right of the mentioned column)
The Table should look like
this with new columns
22
Exercise: Update the table so
that it looks like this with new
data
Hints: Type in each update quesries below once only and keep changing the data. You must run the
query each time before you go for the next.
23
Exercise
1. Display the Majors and the highest GPAs based on each majors from the
Misstudents Table and rename the result column as “HighestGPA”.
2. Display the Cities and the Average Sale based on each Cities from the
Misstudents Table and rename the result column as “AverageSale”.
Hint:
It is similar to the group by query we did before, but not entirely similar.
Problem was:
Display the categories and number of releases under each categories top to bottom from
the Film List table and rename the result column as “Number of Releases”.
Answer was:
24
Answer (GPA Problem)
25
“Calculating a Percentage
We will use simple math to calculate commission. In real life, similar measure are
useful to calculate discounts, commissions, increment etc..
Problem: Create a column named “Commission” & calculate to populate 12%
commissions of the sold amount from the Payment Table in Sakila Database.
Code
Result
Exercise:
1. Create a column named “StoreDiscount” & calculate to populate the column with 9% discount of the
“BookStoreExpense” amount for all students from table “MISSTUDENTS”.
26
“Calculating a Percentage
The aggregate function ROUND, rounds a decimal value to the scale you want to.
Problem: Display a rounded commission for the Commission and name the column as
“RoundedCommission” from table “Sakila.Payment” .
Code
Result
27
“Primary Key with Auto
Increment”
A PRIMARY KEY is a single column/field in a table that uniquely identifies each
record in a database table.
Let’s create a table where the primary key will auto populate.
Code
28
“Primary Key with Auto
Increment”
You can see how the Customer ID auto populated even though no data was input.
Result
29
“CONCAT Function”
The SQL CONCAT function joins two or more data together to form a single string.
Problem: Display owner names & a Combine(CONCAT) columns house numbers and
road names for owners bought(Sold Products) more than 3 products in this marketing
campaign. Rename the new combined column as “Address”.
Code
Result
30
Group by domain names from
email
Imagine in a marketing campaign, the firm want to know how many people from particular companies
attended a seminar based on the domain name from emails. But, email addresses has domain
information and also has other characters. To find the result, we have to subtract the other characters
from the domain to get the result.
31
Group by domain names from email
32
Mistake you might make
33