Structured Query Language (SQL) Pertemuan 09 Matakuliah : F0712 / Lab MS Access

advertisement
Matakuliah
Tahun
: F0712 / Lab MS Access
: 2007
Structured Query Language (SQL)
Pertemuan 09
1
SQL Framework
(1)
• SQL Statement
SELECT [Nama field]
FROM [Nama Tabel]
• SQL Statement dengan Kriteria
SELECT [Nama field]
FROM [Nama Tabel]
WHERE [Kriteria/Kondisi]
2
Reading Specified Columns from a
Single Table (1)
• Suppose we want to obtain just the values
of the Department and Buyer columns of
the SKU_Data table.
• SQL Statement :
SELECT SKU_Data.Department, SKU_Data.Buyer
FROM SKU_Data;
or
SELECT Department, Buyer
FROM SKU_Data
3
Reading Specified Columns from a
Single Table (2)
SKU_Data Record
SQL Result
Notice that some rows are
duplicated in these result.The
data in the first and second row,
for example is identical. We can
eliminate duplicates by using the
DISTINCT keyword as follows :
4
Reading Specified Columns from a
Single Table (3)
SELECT DISTINCT SKU_Data.Department, SKU_Data.Buyer
FROM SKU_Data;
SQL Result – Without “DISTINCT”
SQL Result – With “DISTINCT”
5
Reading Specified Rows from a Single Table
(1)
• Suppose we want all the columns of the
SKU_Data table, but we want only the
rows for the Water Sport Department.
• We can obtain that result by using the
WHERE clause as follows :
SELECT SKU_Data.SKU, SKU_Data.SKU_Description,
SKU_Data.Department, SKU_Data.Buyer
FROM SKU_Data
WHERE (((SKU_Data.Department)='Water Sport'));
or
6
Reading Specified Rows from a Single Table
(2)
SELECT *
FROM
SKU_Data
WHERE Department='Water Sport'
The result of the SQL Statement is
In a WHERE clause, if the coloumns contains text or date data, the
comparison values must be enclosed in single quotation marks (‘ ‘). If the
columns contains numeric data, however, the comparison values need not
be in quotes.
7
Reading Specified Rows from a Single Table
(3)
To find all of the SKU rows with a value greater
than 200.000, we could code :
SELECT *
FROM
SKU_Data
WHERE SKU > 200000
The Result is :
8
Reading Specified Columns and Specified Rows
from a Single Table (1)
• Example : to obtain the SKU_Description and
Department of all products in the Climbing
department, we specify :
SELECT SKU_Description, Department
FROM
SKU_Data
WHERE
Department = ‘Climbing’
Or
SELECT SKU_Data.SKU_Description, SKU_Data.Department
FROM
SKU_Data
WHERE (((SKU_Data.Department)='Climbing'));
9
Reading Specified Columns and Specified Rows
from a Single Table (2)
The result is
10
SORTING
• If you want the DBMS to display the rows in a
particular order, you can use the ORDER BY
phrase.
• For example, the SQL statement :
SELECT
FROM
ORDER_BY
*
ORDER_ITEM
OrderNumber
• Will result in the following :
11
SORTING
• We can sort by two columns by adding a second column
name.
• For example : to sort first by OrderNumber and then by
Price within Order Number.
• For example, the SQL statement :
SELECT
*
FROM
ORDER_ITEM
ORDER_BY OrderNumber, Price;
• Will result in the following :
12
WHERE Clause Options
(1)
• Compound WHERE Clauses  SQL
WHERE clauses can include multiple
conditions by using AND, OR, IN, and
NOT IN operators.
• For example : to find all of the rows of
SKU_Data that have a Department named
Water Sport and a Buyer named Nancy
Meyers.
13
WHERE Clause Options
(2)
• We can code :
SELECT
FROM
WHERE
AND
*
SKU_Data
Department = ‘Water Sport’
Buyer = ‘Nancy Meyers’;
• The result is :
14
WHERE Clause Options
(3)
• Using OR condition  to find all of the
rows in SKU_DATA for either that Camping
or Climbing.
• We could code :
SELECT
*
FROM
SKU_DATA
WHERE
Department = ‘Camping’
OR
Department = ‘Climbing’
15
WHERE Clause Options
(4)
• Using IN operator  We want to obtain all of the
rows in SKU_DATA for buyers Nancy Meyers,
Cindy Lo, and Jerry Martin.
• We could code :
SELECT *
FROM SKU_DATA
WHERE Buyer IN (‘Nancy Meyers, Cindy Lo,
Jerry Martin’)
• Using NOT IN operator  to find rows of
SKU_DATA for which the buyer is someone
other than Nancy Meyers,Cindy Lo, or Jerry
Martin.
16
WHERE Clause Options
(4)
• Using NOT IN operator  to find rows of
SKU_DATA for which the buyer is
someone other than Nancy Meyers,Cindy
Lo, or Jerry Martin.
• We could code :
SELECT *
FROM SKU_DATA
WHERE Buyer NOT IN (‘Nancy
Meyers, Cindy Lo, Jerry Martin’)
17
WHERE Clause Options
(4)
• Ranges in WHERE Clauses
• SQL Statement :
SELECT
FROM
WHERE
[Field]
[Table]
[Condition] BETWEEN
[Condition] AND [Condition]
• Wildcards in WHERE Clauses
• Using keyword “LIKE”
18
Performing Calculations in SQL Queries
(1)
• Using SQL Built-in Functions
–
–
–
–
–
SUM
AVG
MIN
MAX
COUNT
• Example : we want to know the sum of Order
Total for all of the orders in Retail_Order.
• We can code :
SELECT SUM (OrderTotal)
FROM
Retail_Order
19
Performing Calculations in SQL Queries
(2)
• Arithmetic in SELECT Statements.
• Example :
– Quantity*Price AS EP  to compute the
extended price.
– Quantity*Price AS EP, ExtendedPrice  to
compare this computed value to the stored
value of ExtendedPrice.
– Etc.
20
Grouping
• Using the GROUP BY keyword.
• Using the ORDER BY keyword.
• Using the HAVING keyword
21
Querying Two or More Tables with SQL
(1)
• Querying Multiple Tables with Subqueries.
• Querying Multiple Tables with Joins.
• Comparing Subqueries and Joins.
22
Download