Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

1

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G SINGLE
TABLE Part 1
Maxmanroe.com

Image Designed by stories / Freepik


2

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

SELECT Statement

Arithmetic Expressions

Using functions

Using wildcards (*)

Using comparison operators

Using null values

Using Boolean Operator


4

CII1J3 – DATABASE MODELING

PROCESSIN
G SINGLE
TABLE
Part 1.1 SELECT Statement
Maxmanroe.com

Image Designed by stories / Freepik


5

SELECT Statement
• Query is a statement in SQL to retrieve data from a database
• The output of a query is a relation
• Basic query
6

SELECT Statement
• Query is a statement in SQL to retrieve data from a database
• The output of a query is a relation
• Basic query
7

SELECT Statement
• Query is a statement in SQL to retrieve data from a database
• The output of a query is a relation
• Basic query
8

SELECT Statement
• Query is a statement in SQL to retrieve data from a database
• The output of a query is a relation
• Basic query
9

SELECT Statement
• List of products and its standard price

SELECT ProductDescription, ProductStandardPrice


FROM Product_T;
10

SELECT Statement
• Which products have a standard price of less than $275?

SELECT ProductDescription, ProductStandardPrice


FROM Product_T
WHERE ProductStandardPrice < 275;
11

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G SINGLE
TABLE
Part 1.2 Arithmetic Expressions
Maxmanroe.com

Image Designed by stories / Freepik


12

Arithmetic Expressions
• Adding expressions to the basic + Addition

“SELECT . . . FROM . . . WHERE” - Subtraction


* Multiplication
clause
/ Division

• Can be used with any numeric columns


• Expressions are computed for each row of the
result table
13

Arithmetic Expressions
• What are the standard price and standard price if increased by 10
percent for every product?

SELECT ProductID, ProductStandardPrice, ProductStandardPrice*1.1 AS Plus10Percent


FROM Product_T;
14

Arithmetic Expressions
• Note! + Addition
• Multiplication and division take precedence over addition and - Subtraction
subtraction
* Multiplication
• Operators with the same priority will be executed from the
leftmost operator / Division
• The brackets '()' are used to indicate that the statement inside must
be executed first.
15

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G SINGLE
TABLE
Part 1.3 Using Functions
Maxmanroe.com

Image Designed by stories / Freepik


16

Using Functions
• Standard SQL identifies a wide variety of functions.
Mathematical MIN, MAX, COUNT, SUM, AVERAGE, ROUND, TRUNC, and MOD
String LOWER, UPPER, INITCAP, CONCAT, SUBSTR, and COALESCE
Date NEXT_DAY, ADD_ MONTHS, and MONTHS_BETWEEN
17

Using Functions
• What is the average standard price for all products in inventory?

SELECT AVG (ProductStandardPrice) AS AveragePrice


FROM Product_T;
18

Using Functions
• How many different items were ordered on order number 1004?

SELECT COUNT (*)


FROM OrderLine_T
WHERE OrderID = 1004;
19

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G SINGLE
TABLE
Part 1.4 Wildcards (*)
Maxmanroe.com

Image Designed by stories / Freepik


20

Wildcards
used as a wildcard to indicate all columns, displays all columns
* from all the items in the FROM clause
used to represent any collection of characters
%
used as a wildcard character to represent exactly one character
_ rather than any collection of characters
21

Wildcards
SELECT *
FROM OrderLine_T
WHERE OrderID = 1004;

Display all columns


from Table OrderLine_T
with order number 1004
22

Wildcards
SELECT *
FROM Product_T
WHERE ProductDescription like ‘%desk’;

display all different types of desks


23

Wildcards
SELECT *
FROM Product_T
WHERE ProductName like ‘_-drawer’;

any products with specified drawers,


such as 3-drawer, 5-drawer, or 8-drawer.
24

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G SINGLE
TABLE
Part 1.5 Using Comparison Operators
Maxmanroe.com

Image Designed by stories / Freepik


25

Using Comparison Operators


• Recalling

SELECT ProductDescription, ProductStandardPrice


FROM Product_T
WHERE ProductStandardPrice < 275;
26

Using Comparison Operators


Operator Meaning • Different SQL DBMSs
can use different
= Equal to comparison operators.
> Greater than • Comparison operators
can be used for numeric
>= Greater than or equal to data, character and
dates in SQL
< Less than
<= Less than or equal to
<> Not equal to
!= Not equal to
27

Using Comparison Operators


• Which products have a standard price of greater than or equal
$100?

SELECT ProductDescription, ProductStandardPrice


FROM Product_T
WHERE ProductStandardPrice >= 100;
28

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G SINGLE
TABLE
Part 1.6 Null Values
Maxmanroe.com

Image Designed by stories / Freepik


29

Null Values
• A null value means that a column is missing a value; the value is
not zero or blank or any special code—there simply is no value
30

Null Values
ProductDescription ProductStandardPrice AVERAGE(ProductStandardPrice) = 375
Computer Desk 375
Writer’s Desk Cherry 0
8-Drawer Desk 750

ProductDescription ProductStandardPrice AVERAGE(ProductStandardPrice) = 562.5


Computer Desk 375
Writer’s Desk Cherry
8-Drawer Desk 750
31

Null Values
• Display all customers for whom we do not know their postal code

SELECT *
FROM Customer_T
WHERE CustomerPostalCode IS NULL;
32

Null Values
• Display all customers data whose postal code is not NULL

SELECT *
FROM Customer_T
WHERE CustomerPostalCode IS NOT NULL;
33

CT12F3 DATABASE MODELLING AND


IMPLEMENTATION

PROCESSIN
G SINGLE
TABLE
Part 1.7 Boolean Operators
Maxmanroe.com

Image Designed by stories / Freepik


34

Boolean Operators
• AND Joins two or more conditions and returns results only when
all conditions are true.
• OR Joins two or more conditions and returns results when any
conditions are true.
• NOT Negates an expression.
35

Boolean Operators
• If multiple Boolean operators are used in an SQL statement, NOT
is evaluated first, then AND, then OR.
36

Boolean Operators
• List product name, finish, and standard price for all desks and all
tables that cost more than $300 in the Product table.
SELECT ProductDescription, ProductFinish, ProductStandardPrice
FROM Product_T
WHERE ProductDescription LIKE ‘%Desk’ OR ProductDescription LIKE ‘%Table’
AND ProductStandardPrice > 300;

<300 ?
37

Boolean Operators
...
WHERE
ProductDescription LIKE ‘%Desk’
OR
ProductDescription LIKE ‘%Table’
AND
ProductStandardPrice > 300;
38

Boolean Operators
• List product name, finish, and standard price for all desks and all
tables that cost more than $300 in the Product table.
SELECT ProductDescription, ProductFinish, ProductStandardPrice
FROM Product_T
WHERE (ProductDescription LIKE ‘%Desk’ OR ProductDescription LIKE ‘%Table’)
AND ProductStandardPrice > 300;


39

Boolean Operators
...
WHERE
(ProductDescription LIKE ‘%Desk’
OR
ProductDescription LIKE ‘%Table’ )
AND
ProductStandardPrice > 300;
40

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

Recommended Link:
https://www.w3schools.com/sql/
41

THANK
YOU

You might also like