0% found this document useful (0 votes)
11 views33 pages

Lecture 02 - Quiz 02 - Midterm - Advdbmgmt - Elms

DATA MANAGEMENT LECTURE 2

Uploaded by

abir shad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views33 pages

Lecture 02 - Quiz 02 - Midterm - Advdbmgmt - Elms

DATA MANAGEMENT LECTURE 2

Uploaded by

abir shad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

ADVBMGMT

AAFD || QUIZ 02 MATERIALS|| NUMERIC QUERIES


“LIMIT” Clause in
SQL
 The LIMIT clause is used to specify the number of records to return.

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

Exercise: From the table “Film” under Sakila database,


1. Find the maximum rental rate and the name the column as “Price_of_Expensive_Movies”.
2. Find the maximum rental duration from the same table.
3. Find the minimum length of a movie and name the column as “Shortest_movie_we_have”. 3
AVG() & SUM() Functions in
SQL
 The AVG() function returns the average value of the selected column.
 The SUM() function returns the total value of the selected column.
 Let’s find the average length of PG13 movies and change the
name of the column as ‘Average_Length_of_PG13_Movies’
Code

Result

Exercise: From the Table “Film” in Sakila Database


1. Find the average rental rate and the name the column as
“Average_Price_of_our_rentals”.
2. Find the average rental duration of movies that are Rated R.
4
3. Find the total replacement cost for all the movies.
Comparison operators in SQL
Operator Description
 > Greater than
 < Less than
 >= Greater than or equal to

 <= Less than or equal to


 Let’s find movies whose rental rate is less than a dollar.

Code Result

Exercise: from the Table “Film” in Sakila Database


1. Find the movies with length that are greater than 60 minutes..
2. Find the movies with rental rate greater than or equal to 4 dollars.
3. Find the movies with rental duration of 3 or less days.
5
COUNT() function in SQL
 The COUNT() function returns the number of rows that matches a specified
criteria.
Code

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.

 This will create a new column with null values.


 Note: “Null” is a special marker used to indicate that a data
value does not exist in the database..

 Dropping a Column

 This query will completely delete the “Semester” column.


 Note: although non numeric, while using ALTER statement, you
do not need to put the data inside quotations. 14
Update & Delete queries
To enter or delete data/row/columns, we need to use Update & Delete Statements.
However, your computer may or may not refuse to run statements such as Delete & Update etc. The
reason is, these are statement that directly change data in a database which is a security concern.
That is why, you need to relax your security settings first.
Go to Edit, Choose Preferences,
Choose SQL Editor, Then at the
bottom, Deselect “ Safe Updates”

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.

 This changes the data for the student “Imamul”.


 You can change any data in any table using the update
statement just like this.

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.

Similar to the above


code, entered as you
wished.

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,

 Both queries will give you the same answers.

 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy