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

Chapter4_session2-DML with SELECT, VIEW

Uploaded by

tindepzai91
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chapter4_session2-DML with SELECT, VIEW

Uploaded by

tindepzai91
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Database Systems

Chapter 4: Structured Query Language (SQL)


session 2
DML with SELECT, VIEW
Outline

1 DML with SELECT statement

2 Aggregate Functions

3 ORDER BY, GROUP, HAVING

4 VIEW

2
SELECT Statement
The SELECT Statement is used to retrieve data from a
database table.
The basic syntax of SELECT Statement:
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ]

SELECT identifies what columns to be projected into the table


that will be the results of the command
FROM identifies which table (s) needed to process the query
WHERE: restricts the query to rows that meet conditions, the
WHERE clause is optional.
3
SELECT Statement
 The contents of Product table:

4
SELECT Statement
Example: Showing the description, date, and price
of products with a vendor code of 21344
SELECT P_Descript, P_InDate, P_Price, V_Code
FROM Product
WHERE V_Code = 21344

Result:

5
SELECT Statement
Using asterisk ( * ) to select all columns in the
table
SELECT * FROM Product

The comparison operators can be used to restrict


output

6
SELECT Statement
Example: lists all of the rows of the product for
which the vendor code is not 21344.
SELECT P_Descript, P_InDate, P_Price, V_Code
FROM Product
WHERE V_Code <> 21344
Result:

7
SELECT Statement
Using Comparison Operators on Character Attributes
String (character) comparisons are made left-to-right ASCII
character comparison
SELECT P_Code, P_Descript, P_QOH, P_Min, P_Price
FROM Product
WHERE P_Code < '1558-QW1'
Result:

8
SELECT Statement
Using Comparison Operators on Dates
Using yyyy-mm-dd format for date

SELECT P_Descript, P_QOH, P_Min, P_Price, P_InDate


FROM Product
WHERE P_InDate >= '2018-01-20'

Result:

9
SELECT Statement
Using Computed Columns and Column Aliases
Example: determine the total value of each of the
products
SELECT P_Descript, P_QOH, P_Price, P_QOH * P_Price
FROM Product

Result

10
SELECT Statement
Using Alias
 Alias: alternate name given to a column or table in any SQL
statement to improve the readability
• Is useful with calculations
• There can also be the optional AS keyword between the column/table
name and alias
SELECT P_Descript, P_QOH AS Quantity, P_Price Price,
P_QOH * P_Price AS TotalValue
FROM Product

11
SELECT Statement
Logical Operators: AND, OR, NOT
SELECT P_Descript, P_InDate, P_Price, V_Code
FROM Product
WHERE V_Code = 21344 OR V_Code = 24288

12
SELECT Statement
Logical Operators: AND, OR, NOT
You can combine the logical OR with the logical AND to
place further restrictions on the output
SELECT P_Descript, P_InDate, P_Price, V_Code
FROM Product
WHERE (P_Price < 50 AND P_InDate > '2018-01-15')
OR V_Code = 24288

13
SELECT Statement
Special Operators
BETWEEN - Used to check whether an attribute value is
within a range.
IS NULL - Used to check whether an attribute value is
null
LIKE - Used to check whether an attribute value
matches a given string pattern
IN - Used to check whether an attribute value matches
any value within a value list
EXISTS - Used to check whether a subquery returns any
rows

14
SELECT Statement
Special operators
BETWEEN
SELECT * FROM Product
WHERE P_Price BETWEEN 50.00 AND 100.00

SELECT * FROM Product


WHERE P_Price>= 50.00 AND P_Price<= 100.00

15
SELECT Statement
Special operators
IS NULL
SELECT P_Code, P_Descript, V_Code
FROM Product
WHERE V_Code IS NULL

NULL is a special property of an attribute that represents


the absence of any value

16
SELECT Statement
Special operators
LIKE
• The LIKE special operator is used in conjunction with
wildcards to find patterns within string attributes.
• use the percent sign ( % ) and underscore ( _ ) wildcard
characters
% matches any substring.
_ matches one character

17
SELECT Statement
Special operators
LIKE
SELECT V_Name, V_Contact, V_AreaCode, V_Phone
FROM dbo.Vendor
WHERE V_Contact LIKE 'Smith%'

18
SELECT Statement
Special operators
IN
• All of the values in the list must be of the
same data type. SELECT *
FROM Product
WHERE V_Code IN (21344, 24288)
SELECT *
FROM Product
WHERE V_Code = 21344 OR V_Code = 24288

19
SELECT Statement
Special operators
EXISTS
• Is used to check if a subquery returns any rows, run the main
query; otherwise, do not.
• EX: list all vendors but only if there are products with the
quantity on hand, and less than double the minimum quantity
SELECT *
FROM Vendor
WHERE EXISTS(SELECT*FROM Product WHERE P_QOH < P_Min * 2)

20
SELECT Statement
 Use DISTINCT keyword to eliminates all duplicate
rows in the table resulting from the query
SELECT V_Code SELECT DISTINCT V_Code
FROM Product FROM Product

21
Sorting Results: The ORDER BY Clause
ORDER BY clause is used to sort the result set in
ascending (ASC) or descending (DESC) order. the
default order is ascending.
Syntax:

If the ordering column has nulls, they are listed either
first or last, depending on the RDBMS.
The ORDER BY clause must always be listed last in the
SELECT command sequence.
22
Sorting Results: The ORDER BY Clause
Example:
SELECT P_Code, P_Descript, P_QOH, P_Price
FROM dbo.Product
ORDER BY P_Price

23
Sorting Results: The ORDER BY Clause
Result

24
Sorting Results: The ORDER BY Clause
EMPLOYEE Table Contents

25
Sorting Results: The ORDER BY Clause
Cascading order sequence
SELECT EMP_LName, EMP_FName, EMP_Initial, EMP_AreaCode, EMP_Phone
FROM Employee
ORDER BY EMP_LName, EMP_FName, EMP_Initial

26
Sorting Results: The ORDER BY Clause
Descending order
SELECT P_Descript, V_Code, P_InDate, P_Price
FROM Product
WHERE P_InDate < '2018-01-21' AND P_Price <= 50.00
ORDER BY V_Code, P_Price DESC

27
Aggregate Functions
An aggregate function allows you to perform a
calculation on a set of values to return a single
scalar value

Syntax

28
The contents of Product table

29
Aggregate Functions
COUNT
The default is ALL

30
Aggregate Functions
COUNT
SELECT COUNT(V_Code) AS 'Số lượng V_Code'
FROM dbo.Product

SELECT COUNT( DISTINCT V_Code) AS 'Số lượng V_Code'


FROM dbo.Product

SELECT COUNT(*)
FROM dbo.Product

COUNT(*) returns the number of total rows from the query,


including the rows that contain nulls. 31
Aggregate Functions
MAX/MIN
SELECT MAX(P_Price) AS 'Max Price'
FROM dbo.Product

SELECT P_Code, P_Descript, P_Price


FROM Product
WHERE P_Price = (SELECT MAX(P_Price) FROM Product)

32
Aggregate Functions
SUM
SELECT SUM(P_QOH * P_Price) AS TOTVALUE
FROM Product

33
Aggregate Functions
AVERAGE

SELECT AVG(P_Price) AS AveragePrice


FROM Product

SELECT P_Descript,P_QOH, P_Price, V_Code


FROM Product WHERE P_Price > (SELECT AVG(P_Price)FROM Product)
ORDER BY P_Price DESC

34
GROUP BY Clause
GROUP BY is particularly useful when paired with
aggregate functions.
The GROUP BY clause allows you to arrange the
rows returned by SELECT statement in groups.
The groups are determined by the columns that
you specify in the GROUP BY clause.
 Syntax:

35
GROUP BY Clause
The GROUP BY clause is valid only when used in
conjunction with one of the SQL aggregate
functions: COUNT, MIN, MAX, AVG, SUM
SELECT V_Code, P_Code
FROM Product
GROUP BY V_Code

The above command will result an error:

36
GROUP BY Clause
The command will should be written
SELECT V_Code, COUNT(P_Code) AS Quantity
FROM Product
GROUP BY V_Code
ORDER BY Quantity

37
HAVING Clause
The HAVING clause is often used with the GROUP
BY clause in the SELECT statement to filter group of
rows based on a specified condition.
The WHERE clause is used to restrict the rows that
you select. But the HAVING clause is used to
restrict groups.

38
SQL statement processing
order

39
HAVING Clause
With HAVING clause
SELECT V_Code,COUNT(P_Code),AVG(P_Price)
FROM Product
GROUP BY V_Code

SELECT V_Code,COUNT(P_Code),AVG(P_Price)
FROM Product
GROUP BY V_Code
HAVING AVG(P_Price)<10
40
Virtual Tables: View
View: provides users controlled access to tables
It is a virtual table based on a SELECT query
Logical table exists only in memory
Can be treated as though it were a real table
CREATE VIEW Price50 AS
SELECT P_Code, P_Descript, P_QOH, P_Price
FROM dbo.Product
WHERE P_PRICE > 50.00
WITH CHECK OPTION

SELECT * FROM Price50

41
Virtual Tables: View
WITH CHECK OPTION
will cause UPDATE or INSERT statements on that view to
be rejected when those statements would cause
updated or inserted rows to be removed from the view.
This option can be used only with updateable views.

UPDATE Price50 Will result an error


SET P_Price = 20.5 message due to CHECK
OPTION constraint.
WHERE P_Code = '11QER/31'

42
Virtual Tables: View
Advantages/Disadvantages of Views

43

You might also like