Download presentation
Presentation is loading. Please wait.
Published byJoshua Quinn Modified over 8 years ago
1
1 Structured Query Language (SQL) Pertemuan 09 Matakuliah: F0712 / Lab MS Access Tahun: 2007
2
2 SQL Framework (1) SQL Statement SELECT [Nama field] FROM [Nama Tabel] SQL Statement dengan Kriteria SELECT [Nama field] FROM [Nama Tabel] WHERE [Kriteria/Kondisi]
3
3 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
4
4 Reading Specified Columns from a Single Table (2) 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 : SKU_Data Record SQL Result
5
5 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”
6
6 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
7
7 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.
8
8 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 :
9
9 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 : SELECTSKU_Description, Department FROMSKU_Data WHEREDepartment = ‘Climbing’ Or SELECT SKU_Data.SKU_Description, SKU_Data.Department FROM SKU_Data WHERE (((SKU_Data.Department)='Climbing'));
10
10 Reading Specified Columns and Specified Rows from a Single Table (2) The result is
11
11 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* FROMORDER_ITEM ORDER_BYOrderNumber Will result in the following :
12
12 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 :
13
13 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.
14
14 WHERE Clause Options (2) We can code : SELECT* FROMSKU_Data WHEREDepartment = ‘Water Sport’ ANDBuyer = ‘Nancy Meyers’; The result is :
15
15 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* FROMSKU_DATA WHEREDepartment = ‘Camping’ ORDepartment = ‘Climbing’
16
16 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* FROMSKU_DATA WHEREBuyer 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.
17
17 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* FROMSKU_DATA WHEREBuyer NOT IN (‘Nancy Meyers, Cindy Lo, Jerry Martin’)
18
18 WHERE Clause Options (4) Ranges in WHERE Clauses SQL Statement : SELECT [Field] FROM[Table] WHERE[Condition] BETWEEN [Condition] AND [Condition] Wildcards in WHERE Clauses Using keyword “LIKE”
19
19 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 : SELECTSUM (OrderTotal) FROMRetail_Order
20
20 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.
21
21 Grouping Using the GROUP BY keyword. Using the ORDER BY keyword. Using the HAVING keyword
22
22 Querying Two or More Tables with SQL (1) Querying Multiple Tables with Subqueries. Querying Multiple Tables with Joins. Comparing Subqueries and Joins.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.