Chapter4 Session2
Chapter4 Session2
2 Aggregate Functions
4 VIEW
2
SELECT Statement
qThe SELECT Statement is used to retrieve data from a
database table.
qThe basic syntax of SELECT Statement:
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ]
4
SELECT Statement
qExample: 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
qUsing asterisk ( * ) to select all columns in the
table
SELECT * FROM Product
6
SELECT Statement
qExample: 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
qUsing 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
qUsing Comparison Operators on Dates
§ Using yyyy-mm-dd format for date
§ Result:
9
SELECT Statement
qUsing 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
qUsing 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
qLogical 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
qLogical 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
qSpecial 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
qSpecial operators
§ BETWEEN
SELECT * FROM Product
WHERE P_Price BETWEEN 50.00 AND 100.00
15
SELECT Statement
qSpecial operators
§ IS NULL
SELECT P_Code, P_Descript, V_Code
FROM Product
WHERE V_Code IS NULL
16
SELECT Statement
qSpecial 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
qSpecial operators
§ LIKE
SELECT V_Name, V_Contact, V_AreaCode, V_Phone
FROM dbo.Vendor
WHERE V_Contact LIKE 'Smith%'
18
SELECT Statement
qSpecial 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
qSpecial 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
q 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
qORDER BY clause is used to sort the result set in
ascending (ASC) or descending (DESC) order. the
default order is ascending.
§ Syntax:
23
Sorting Results: The ORDER
BY Clause
qResult
24
Sorting Results: The ORDER
BY Clause
qEMPLOYEE Table Contents
25
Sorting Results: The ORDER
BY Clause
qCascading 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
qDescending 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
qAn aggregate function allows you to perform a
calculation on a set of values to return a single
scalar value
qSyntax
28
The contents of Product table
29
Aggregate Functions
qCOUNT
§ The default is ALL
30
Aggregate Functions
qCOUNT
SELECT COUNT(V_Code) AS 'Số lượng V_Code'
FROM dbo.Product
SELECT COUNT(*)
FROM dbo.Product
32
Aggregate Functions
qSUM
SELECT SUM(P_QOH * P_Price) AS TOTVALUE
FROM Product
33
Aggregate Functions
qAVERAGE
34
GROUP BY Clause
qGROUP BY is particularly useful when paired with
aggregate functions.
qThe 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.
q Syntax:
35
GROUP BY Clause
qThe 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
36
GROUP BY Clause
qThe 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
qThe HAVING clause is often used with the GROUP
BY clause in the SELECT statement to filter group of
rows based on a specified condition.
qThe 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
qWith 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
qView: 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
41
Virtual Tables: View
qWITH 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.
42
Virtual Tables: View
qAdvantages/Disadvantages of Views
43