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_I D PRODUCT_ DESCRIPTI ON PRODUCT_ FINISH STANDARD _PRICE PRODUCT_ LINE_ID 1End TableCherry Coffee TableNatural Ash Computer Desk Natural Ash Entertainme nt Center Natural Oak "…retrieve all products with product line id 20001" PRODUCT_IDPRODUCT_DE SCRIPTION PRODUCT_FI NISH STANDARD_P RICE PRODUCT_LI NE_ID 2Coffee TableNatural Ash Computer Desk Natural Ash Drawer Dresser White Ash Dining TableNatural Ash
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 [WHEREcondition(s)];
Using the WHERE Clause SELECT product_description, product_finish, standard_price FROM product_t WHERE product_finish=‘Natural Ash'; PRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICE Coffee TableNatural Ash200 Computer DeskNatural Ash375 Dining TableNatural Ash850
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_t WHERE 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_price FROM product_t WHERE standard_price<=500 PRODUCT_DESCRIPTIONSTANDARD_PRICE End Table175 Coffee Table200 Computer Desk375 Writers Desk325 Computer Desk250
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. SELECTproduct_description, standard_price FROM product_t WHEREstandard_price BETWEEN 200 AND 700; Lower limit Higher limit PRODUCT_DESCRIP TION STANDARD_PRICE Coffee Table200 Computer Desk375 Entertainment Center650 Writers Desk325 Computer Desk250
Using the IN Operator Use the IN operator to test for values in a list. SELECTproduct_description, product_finish, standard_price FROM product_t WHEREproduct_finish IN (‘Cherry’, ‘White Ash’, ‘Oak’); PRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICE End TableCherry175 Writers DeskCherry325 8-Drawer DresserWhite Ash750
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 SELECTproduct_description FROM product_t WHEREproduct_description LIKE ‘E%';
Using the LIKE Operator You can combine pattern matching characters. You can use the ESCAPE identifier to search for "%" or "_". SELECTproduct_description FROMproduct_t WHEREproduct_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_t WHERE standard_price IS NULL; PRODUCT_IDPRODUCT_DESCRI PTION PRODUCT_FINISHSTANDARD_PRICEPRODUCT_LINE_ID 9Corner TableWalnut10001
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_t WHERE product_finish=‘Cherry’ AND product_line_id=‘10001'; PRODUCT_DESCRIPTIONPRODUCT_FINISHPRODUCT_LINE_ID End TableCherry10001 Writers DeskCherry10001
Using the OR Operator OR requires either condition to be TRUE. SELECT product_description, product_finish, product_line_id FROM product_t WHERE product_finish=‘Cherry’ OR product_line_id=‘30001'; PRODUCT_DESCRIPTIONPRODUCT_FINISHPRODUCT_LINE_ID End TableCherry10001 Writers DeskCherry10001 Entertainment CenterNatural Oak30001 Computer DeskWalnut30001
Using the NOT Operator SELECT product_description, product_finish FROM product_t WHERE product_finish NOT IN (‘Cherry',‘Oak’); PRODUCT_DESCRIPTIONPRODUCT_FINISH Coffee TableNatural Ash Computer DeskNatural Ash Entertainment CenterNatural Oak 8-Drawer DresserWhite Ash Dining TableNatural Ash Computer DeskWalnut
Rules of Precedence Order EvaluatedOperator 1All comparison operators 2NOT 3AND 4OR Override rules of precedence using parentheses
Rules of Precedence SELECT product_description, product_finish, standard_price FROM product_t WHERE product_finish=‘Cherry' OR product_finish=‘Oak' AND standard_price>500; PRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICE End TableCherry175 Writers DeskCherry325
Rules of Precedence Use parentheses to force priority SELECT product_description, product_finish, standard_price FROM product_t WHERE (product_finish=‘Cherry' OR product_finish=‘Natural Oak‘) AND standard_price>500; PRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICE Entertainment CenterNatural Oak650
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_IDPRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICEPRODUCT_LINE_ID 5Writers DeskCherry Entertainment CenterNatural Oak End TableCherry Dining TableNatural Ash Corner TableWalnut Computer DeskNatural Ash Computer DeskWalnut Coffee TableNatural Ash Drawer DresserWhite Ash
Sorting by Column Alias SELECT product_description pd, product_finish pf ORDER BY pd; PDPF 8-Drawer DresserWhite Ash Coffee TableNatural Ash Computer DeskNatural Ash Computer DeskWalnut Corner TableWalnut Dining TableNatural Ash End TableCherry Entertainment CenterNatural Oak Writers DeskCherry
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; IDPRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICEPRODUCT_LINE_ID 68-Drawer DresserWhite Ash Coffee TableNatural Ash Computer DeskNatural Ash Computer DeskWalnut Corner TableWalnut Dining TableNatural Ash End TableCherry Entertainment CenterNatural Oak Writers DeskCherry
Summary SELECT[DISTINCT] {*, column [alias],...} FROM table [WHEREcondition(s)] [ORDER BY{column, expr, alias} [ASC|DESC]];