0% found this document useful (0 votes)
25 views36 pages

(It) 10 Processing Single Table 2

This document discusses basic SQL queries including using ranges, distinct values, IN and NOT IN operators, and ORDER BY clauses. It provides examples of queries to retrieve product details, customer address, and orders by customer state.

Uploaded by

angwynren
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)
25 views36 pages

(It) 10 Processing Single Table 2

This document discusses basic SQL queries including using ranges, distinct values, IN and NOT IN operators, and ORDER BY clauses. It provides examples of queries to retrieve product details, customer address, and orders by customer state.

Uploaded by

angwynren
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/ 36

11 quiz review.

txt 1

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G SINGLE
TABLE Part 2
Maxmanroe.com

Image Designed by stories / Freepik


2

Learning Outcomes
Students are able to use basic query to get information
3

Using Range for Qualification

Using distinct values

Using IN and NOT IN in lists

ORDER BY Clauses

Example of Basic Queries


4

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G 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

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G 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

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G 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

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G 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 in SELECT ...
ascending or descending order. FROM ...
WHERE ...
• The default is ascending
ORDER BY A1
• If sorting from high to low, use DESC as a [ASC/DESC]
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

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G SINGLE
TABLE
Part 2.4 Example of Basic Queries
Maxmanroe.com

Image Designed by stories / Freepik


21

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
22

Example of Basic Queries-1


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

Example of Basic Queries-2


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

Example of Basic Queries-2


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

Example of Basic Queries-2


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

Example of Basic Queries-2


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

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’;
28

Example of Basic Queries-3


• Which orders have been placed since 10/24/2015 Order_T
OrderID
OrderDate
CustomerID
29

Example of Basic Queries-3


• Which orders have been placed since 10/24/2015 Order_T
OrderID
OrderDate
CustomerID

SELECT OrderID, OrderDate


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

Example of Basic Queries-4


• What are the unique combinations of order number Orderline_T
OrderID
and order quantity included in the OrderLine table?
ProductID
OrderedQuantity
31

Example of Basic Queries-4


• What are the unique combinations of order number Orderline_T
OrderID
and order quantity included in the OrderLine table?
ProductID
OrderedQuantity

SELECT DISTINCT OrderID, OrderedQuantity


FROM OrderLine_T;
32

Example of Basic Queries-5


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

Example of Basic Queries-5


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

Example of Basic Queries-5


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

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

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

THANK
YOU

You might also like