0% found this document useful (0 votes)
5 views

11 Processing Single Table 2-Ayu

Uploaded by

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

11 Processing Single Table 2-Ayu

Uploaded by

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

1

CII1J3 – DATABASE MODELING

PROCESSING
SINGLE TABLE
Part 2
Maxmanroe.com

Image Designed by stories / Freepik


2

Learning Outcomes
Program Learning Outcomes (PLO)
PLO-9 Students are able to make decisions appropriately in the context of problem
solving, based on the results of the analysis of information / data and the
implications of decisions.

Course Learning Outcomes (CLO)


CLO 1 Students are able to explain basic database concepts correctly
CLO 2 Students are able to make database modeling using the E-R model, relational
model and normalization correctly
CLO 3 Students are able to implement database modeling using SQL statements
correctly according to organizational needs
3

Using Range for Qualification

Using distinct values

Using IN and NOT IN in lists

ORDER BY Clauses

GROUP BY Clauses

HAVING clause

Example of Basic Queries


4

CII1J3 – DATABASE MODELING

PROCESSING
SINGLE TABLE
Part 2.1 Using Range for Qualification
Maxmanroe.com

Image Designed by stories / Freepik


5

Using Range for Qualification


• The comparison operators < and > are used to establish a
range of values.
• The keywords BETWEEN and NOT BETWEEN can also be
used.
6

Using Range for Qualification


• Which products in the Product table have a standard price
between $200 and $300?
SELECT ProductDescription, ProductStandardPrice
FROM Product_T
WHERE ProductStandardPrice > = 200 AND ProductStandardPrice < = 300;

SELECT ProductDescription, ProductStandardPrice


FROM Product_T
WHERE ProductStandardPrice BETWEEN 200 AND 300;
7

Using Range for Qualification


SELECT ProductDescription, ProductStandardPrice
FROM Product_T
WHERE ProductStandardPrice NOT BETWEEN 200 AND 300;

• Returns products in the Product table have a standard price


less than $200 or greater than $300
8

CII1J3 – DATABASE MODELING

PROCESSING
SINGLE TABLE
Part 2.2 Using Distinct Values
Maxmanroe.com

Image Designed by stories / Freepik


9

Using DISTINCT Values


• Sometimes when returning rows that don’t include the
primary key, duplicate rows will be returned.
• If we add the DISTINCT keyword, only 1 occurrence of the
same row will be returned
10

Using DISTINCT Values


• What order numbers are included in
the OrderLine table?

SELECT OrderID
FROM OrderLine_T;
11

Using DISTINCT Values


• What order numbers are included in
the OrderLine table?

SELECT DISTINCT OrderID


FROM OrderLine_T;
12

CII1J3 – DATABASE MODELING

PROCESSING
SINGLE TABLE
Part 2.3 Using IN and NOT IN in lists
Maxmanroe.com

Image Designed by stories / Freepik


13

Using IN and NOT IN in Lists


• To match a list of values, consider using IN.
• In contrast, using NOT IN searches for data that is not
included in the list
• Future explanation in the next chapter (Subqueries)
14

Using IN and NOT IN in Lists


• List all customers who live in warmer
states.

SELECT CustomerName, CustomerCity, CustomerState


FROM Customer_T
WHERE CustomerState IN (‘FL’, ‘TX’, ‘CA’, ‘HI’);
15

CII1J3 – DATABASE MODELING

PROCESSING
SINGLE TABLE
Part 2.4 ORDER BY Clauses
Maxmanroe.com

Image Designed by stories / Freepik


16

ORDER BY Clauses
• ‘ORDER BY’ Sorts the final results rows
SELECT ...
in ascending or descending order. FROM ...
• The default is ascending WHERE ...
ORDER BY A1 [ASC/DESC]
• If sorting from high to low, use DESC as
a keyword
17

ORDER BY Clauses
SELECT CustomerName, CustomerCity, CustomerState
FROM Customer_T
WHERE CustomerState IN (‘FL’, ‘TX’, ‘CA’, ‘HI’)
ORDER BY CustomerState, CustomerName;
18

ORDER BY Clauses
SELECT CustomerName, CustomerCity, CustomerState
FROM Customer_T
WHERE CustomerState IN (‘FL’, ‘TX’, ‘CA’, ‘HI’)
ORDER BY CustomerState, CustomerName;
19

ORDER BY Clauses
SELECT CustomerName, CustomerCity, CustomerState
FROM Customer_T
WHERE CustomerState IN (‘FL’, ‘TX’, ‘CA’, ‘HI’)
ORDER BY CustomerState, CustomerName;

? →

20

CII1J3 – DATABASE MODELING

PROCESSING
SINGLE TABLE
Part 2.5 GROUP BY Clauses
Maxmanroe.com

Image Designed by stories / Freepik


21

GROUP BY Clauses
• GROUP BY is particularly useful when paired with aggregate
functions
• GROUP BY divides a table into subsets (by groups)
• An aggregate function can be used to provide summary information for that group.
• Scalar Aggregate
• A single value returned from an SQL query that includes an aggregate function.

• Vector Aggregate
• Multiple values returned from an SQL query that includes an aggregate function.
22

GROUP BY Clauses
• Count the number of customers with
addresses in each state to which we
ship.

SELECT CustomerState, COUNT (CustomerState)


FROM Customer_T
GROUP BY CustomerState;
23

CII1J3 – DATABASE MODELING

PROCESSING
SINGLE TABLE
Part 2.6 HAVING clause
Maxmanroe.com

Image Designed by stories / Freepik


24

HAVING clause
• The HAVING clause acts like a WHERE clause, but it identifies
groups, rather than rows, that meet a criterion.
• Therefore, you will usually see a HAVING clause following a
GROUP BY clause.
25

HAVING Clauses
• Find only states with more than one
customer.

SELECT CustomerState, COUNT (CustomerState)


FROM Customer_T
GROUP BY CustomerState
HAVING COUNT (CustomerState) > 1;
26

CII1J3 – DATABASE MODELING

PROCESSING
SINGLE TABLE
Part 2.7 Example of Basic Queries
Maxmanroe.com

Image Designed by stories / Freepik


27

Example of Basic Queries-1


• List the unit price, product name, and product ID for all
products in the Product table. Product_T
ProductID
ProductDescription
ProductFinish
ProductStandardPrice
ProductCost
ProductPriorYearGoal
ProductCurrentYearGoal
ProductLineID
28

Example of Basic Queries-1


• List the unit price, product name, and product Product_T

ID for all products in the Product table. ProductID


ProductDescription
ProductFinish
ProductStandardPrice
ProductCost
SELECT ProductStandardPrice, ProductDescription, ProductPriorYearGoal
ProductID ProductCurrentYearGoal
ProductLineID
FROM Product_T;
29

Example of Basic Queries-2


• What is the address of the customer Customer_T
named Home Furnishings? CustomerID
CustomerName
CustomerAddress
CustomerCity
CustomerState
CustomerPostalCode
30

Example of Basic Queries-2


• What is the address of the customer Customer_T
named Home Furnishings? CustomerID
CustomerName
CustomerAddress
CustomerCity
CustomerState
SELECT CustomerName, CustomerAddress
CustomerPostalCode
31

Example of Basic Queries-2


• What is the address of the customer Customer_T
named Home Furnishings? CustomerID
CustomerName
CustomerAddress
CustomerCity
CustomerState
SELECT CustomerName, CustomerAddress
CustomerPostalCode
FROM Customer_T
32

Example of Basic Queries-2


• What is the address of the customer Customer_T
named Home Furnishings? CustomerID
CustomerName
CustomerAddress
CustomerCity
CustomerState
SELECT CustomerName, CustomerAddress
CustomerPostalCode
FROM Customer_T
WHERE CustomerName = ‘Home Furnishings’;
33

Example of Basic Queries-2


• Using alias
SELECT CustomerName, CustomerAddress
FROM Customer_T
WHERE CustomerName = ‘Home Furnishings’;

SELECT CUST.CustomerName AS Name, CUST.CustomerAddress


FROM Customer_T AS Cust
WHERE Name = ‘Home Furnishings’;
34

Example of Basic Queries-3


• Which orders have been placed since Order_T

10/24/2015 OrderID
OrderDate
CustomerID
35

Example of Basic Queries-3


• Which orders have been placed since Order_T

10/24/2015 OrderID
OrderDate
CustomerID

SELECT OrderID, OrderDate


FROM Order_T
WHERE OrderDate > ‘24-OCT-2015’;
36

Example of Basic Queries-4


• What are the unique combinations of order Orderline_T

number and order quantity included in the OrderID

OrderLine table? ProductID


OrderedQuantity
37

Example of Basic Queries-4


• What are the unique combinations of order Orderline_T

number and order quantity included in the OrderID

OrderLine table? ProductID


OrderedQuantity

SELECT DISTINCT OrderID, OrderedQuantity


FROM OrderLine_T;
38

Example of Basic Queries-5


• List, in alphabetical order, the product finish and the Product_T
average standard price for each finish having an ProductID
average standard price less than $750. ProductDescription
ProductFinish
ProductStandardPrice
ProductCost
ProductPriorYearGoal
ProductCurrentYearGoal
ProductLineID
39

Example of Basic Queries-5


• List, in alphabetical order, the product finish and the Product_T
average standard price for each finish having an ProductID
average standard price less than $750. ProductDescription
ProductFinish
• order by ProductFinish
ProductStandardPrice
• group by ProductFinish → avg (ProductStandardPrice) ProductCost
• having avg (ProductStandardPrice) < 750 ProductPriorYearGoal
ProductCurrentYearGoal
ProductLineID
40

Example of Basic Queries-5


• List, in alphabetical order, the product finish and the Product_T
average standard price for each finish having an ProductID
average standard price less than 750. ProductDescription
ProductFinish
ProductStandardPrice
SELECT ProductFinish, AVG (ProductStandardPrice) ProductCost
FROM Product_T ProductPriorYearGoal
GROUP BY ProductFinish ProductCurrentYearGoal
HAVING AVG (ProductStandardPrice) < 750 ProductLineID
ORDER BY ProductFinish;
41

References
Hoffer, Jeffrey A., et.al., "Modern Database Management", Twelfth
Edition, Pearson, 2016. Chapter 6.

Recommended Link:
https://www.sqltutorial.org/
42

THANK
YOU

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