Documente Academic
Documente Profesional
Documente Cultură
SQL Framework
SQL Statement SELECT [Nama field] FROM [Nama Tabel]
(1)
SQL Statement dengan Kriteria SELECT [Nama field] FROM [Nama Tabel] WHERE [Kriteria/Kondisi]
2
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
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 :
(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
(2)
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
(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 :
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
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;
12
(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
(2)
We can code :
SELECT FROM WHERE AND * SKU_Data Department = Water Sport Buyer = Nancy Meyers;
The result is :
14
(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
(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
(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
(4)
18
(1)
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
(2)
20
Grouping
Using the GROUP BY keyword. Using the ORDER BY keyword. Using the HAVING keyword
21
(1)
Querying Multiple Tables with Subqueries. Querying Multiple Tables with Joins. Comparing Subqueries and Joins.
22