Upload
annabelle-clark
View
217
Download
1
Embed Size (px)
Citation preview
Restricting and Sorting Data
Objectives
After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query
Limiting Rows Using a Selection
PRODUCT_ID
PRODUCT_DESCRIPTION
PRODUCT_FINISH
STANDARD_PRICE
PRODUCT_LINE_ID
1 End Table Cherry 175 10001
2 Coffee Table Natural Ash 200 20001
3 Computer Desk
Natural Ash 375 20001
4 Entertainment Center
Natural Oak 650 30001
"…retrieve all"…retrieve allproducts with products with product line id product line id
20001"20001"
PRODUCT_ID PRODUCT_DESCRIPTION
PRODUCT_FINISH
STANDARD_PRICE
PRODUCT_LINE_ID
2 Coffee Table Natural Ash 200 20001
3 Computer Desk
Natural Ash 375 20001
6 8-Drawer Dresser
White Ash 750 20001
7 Dining Table Natural Ash 850 20001
Limiting Rows Selected
Restrict the rows returned by using the WHERE clause.
The WHERE clause follows the FROM clause.
SELECT [DISTINCT] {*, column [alias], ...}FROM table[WHERE condition(s)];
Using the WHERE Clause
SELECT product_description, product_finish, standard_price
FROM product_tWHERE product_finish=‘Natural Ash';
PRODUCT_DESCRIPTION PRODUCT_FINISH STANDARD_PRICE
Coffee Table Natural Ash 200
Computer Desk Natural Ash 375
Dining Table Natural Ash 850
Character Strings and Dates
Character strings and date values are enclosed in single quotation marks
Character values are case-sensitive and date values are format-sensitive
Default date format is 'DD-MON-YY'
SELECT product_description, product_finish, standard_price
FROM product_tWHERE product_finish=‘Cherry';
Comparison Operators
Operator
=
>
>=
<
<=
<>
Meaning
Equal to
Greater than
Greater than or equal to
Less than
Less than or equal to
Not equal to
Using the Comparison Operators
SELECT product_description, standard_priceFROM product_tWHERE standard_price<=500
PRODUCT_DESCRIPTION STANDARD_PRICE
End Table 175
Coffee Table 200
Computer Desk 375
Writers Desk 325
Computer Desk 250
Other Comparison Operators
Operator
BETWEEN
...AND...
IN(list)
LIKE
IS NULL
Meaning
Between two values (inclusive)
Match any of a list of values
Match a character pattern
Is a null value
Using the BETWEEN Operator
Use the BETWEEN operator to display rows based on a range of values.
SELECT product_description, standard_priceFROM product_tWHERE standard_price BETWEEN 200 AND 700;
Lowerlimit
Higherlimit
PRODUCT_DESCRIPTION
STANDARD_PRICE
Coffee Table 200
Computer Desk 375
Entertainment Center 650
Writers Desk 325
Computer Desk 250
Using the IN Operator
Use the IN operator to test for values in a list.
SELECT product_description, product_finish,standard_price
FROM product_tWHERE product_finish IN (‘Cherry’, ‘White Ash’, ‘Oak’);
PRODUCT_DESCRIPTION PRODUCT_FINISH STANDARD_PRICE
End Table Cherry 175
Writers Desk Cherry 325
8-Drawer Dresser White Ash 750
Using the LIKE Operator
Use the LIKE operator to perform wildcard searches of valid search string values.
Search conditions can contain either literal characters or numbers. % denotes zero or many characters _ denotes one character
SELECT product_descriptionFROM product_tWHERE product_description LIKE ‘E%';
Using the LIKE Operator
You can combine pattern matching characters.
You can use the ESCAPE identifier to search for "%" or "_".
SELECT product_descriptionFROM product_tWHERE product_description LIKE '_n%';
PRODUCT_DESCRIPTION
End Table
Entertainment Center
Using the IS NULL Operator
Test for null values with the IS NULL operator
SELECT *FROM product_tWHERE standard_price IS NULL;
PRODUCT_ID PRODUCT_DESCRIPTION
PRODUCT_FINISH STANDARD_PRICE PRODUCT_LINE_ID
9 Corner Table Walnut 10001
Logical Operators
Operator
AND
OR
NOT
Meaning
Returns TRUE if both component
conditions are TRUE
Returns TRUE if either component
condition is TRUE
Returns TRUE if the following condition is FALSE
Using the AND Operator
AND requires both conditions to be TRUE.
SELECT product_description, product_finish, product_line_id
FROM product_tWHERE product_finish=‘Cherry’AND product_line_id=‘10001';
PRODUCT_DESCRIPTION PRODUCT_FINISH PRODUCT_LINE_ID
End Table Cherry 10001
Writers Desk Cherry 10001
Using the OR Operator
OR requires either condition to be TRUE.
SELECT product_description, product_finish, product_line_id
FROM product_tWHERE product_finish=‘Cherry’OR product_line_id=‘30001';
PRODUCT_DESCRIPTION PRODUCT_FINISH PRODUCT_LINE_ID
End Table Cherry 10001
Writers Desk Cherry 10001
Entertainment Center Natural Oak 30001
Computer Desk Walnut 30001
Using the NOT Operator
SELECT product_description, product_finishFROM product_tWHERE product_finish NOT IN (‘Cherry',‘Oak’);
PRODUCT_DESCRIPTION PRODUCT_FINISH
Coffee Table Natural Ash
Computer Desk Natural Ash
Entertainment Center Natural Oak
8-Drawer Dresser White Ash
Dining Table Natural Ash
Computer Desk Walnut
Rules of Precedence
Order Evaluated Operator
1 All comparison operators
2 NOT
3 AND
4 OR
Override rules of precedence using parentheses
Rules of Precedence
SELECT product_description, product_finish, standard_priceFROM product_tWHERE product_finish=‘Cherry'OR product_finish=‘Oak'AND standard_price>500;
PRODUCT_DESCRIPTION PRODUCT_FINISH STANDARD_PRICE
End Table Cherry 175
Writers Desk Cherry 325
Rules of Precedence
Use parentheses to force priority
SELECT product_description, product_finish, standard_priceFROM product_tWHERE (product_finish=‘Cherry'OR product_finish=‘Natural Oak‘)AND standard_price>500;
PRODUCT_DESCRIPTION PRODUCT_FINISH STANDARD_PRICE
Entertainment Center Natural Oak 650
ORDER BY Clause Sort rows with the ORDER BY clause
ASC: ascending order, default DESC: descending order
The ORDER BY clause comes last in the SELECT statement.
SQL> SELECT * 2 FROM product_t 3 ORDER BY product_description;
Sorting in Descending Order
SQL> SELECT * 2 FROM product_t 3 ORDER BY product_description DESC;
PRODUCT_ID PRODUCT_DESCRIPTION PRODUCT_FINISH STANDARD_PRICE PRODUCT_LINE_ID
5 Writers Desk Cherry 325 10001
4 Entertainment Center Natural Oak 650 30001
1 End Table Cherry 175 10001
7 Dining Table Natural Ash 850 20001
9 Corner Table Walnut 10001
3 Computer Desk Natural Ash 375 20001
8 Computer Desk Walnut 250 30001
2 Coffee Table Natural Ash 200 20001
6 8-Drawer Dresser White Ash 750 20001
Sorting by Column Alias
SELECT product_description pd, product_finish pfORDER BY pd;
PD PF
8-Drawer Dresser White Ash
Coffee Table Natural Ash
Computer Desk Natural Ash
Computer Desk Walnut
Corner Table Walnut
Dining Table Natural Ash
End Table Cherry
Entertainment Center Natural Oak
Writers Desk Cherry
Sorting by Multiple Columns The order of ORDER BY list is the order of sort.
You can sort by a column that is not in the SELECT list.
SQL> SELECT * 2 FROM product_t 3 ORDER BY product_description, product_finish;
ID PRODUCT_DESCRIPTION PRODUCT_FINISH STANDARD_PRICE PRODUCT_LINE_ID
6 8-Drawer Dresser White Ash 750 20001
2 Coffee Table Natural Ash 200 20001
3 Computer Desk Natural Ash 375 20001
8 Computer Desk Walnut 250 30001
9 Corner Table Walnut 10001
7 Dining Table Natural Ash 850 20001
1 End Table Cherry 175 10001
4 Entertainment Center Natural Oak 650 30001
5 Writers Desk Cherry 325 10001
Summary
SELECT [DISTINCT] {*, column [alias], ...}FROM table[WHERE condition(s)][ORDER BY {column, expr, alias} [ASC|DESC]];