Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Restricting and Sorting Data

2 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

3 Limiting Rows Using a Selection PRODUCT_I D PRODUCT_ DESCRIPTI ON PRODUCT_ FINISH STANDARD _PRICE PRODUCT_ LINE_ID 1End TableCherry17510001 2Coffee TableNatural Ash20020001 3Computer Desk Natural Ash37520001 4Entertainme nt Center Natural Oak65030001 "…retrieve all products with product line id 20001" PRODUCT_IDPRODUCT_DE SCRIPTION PRODUCT_FI NISH STANDARD_P RICE PRODUCT_LI NE_ID 2Coffee TableNatural Ash20020001 3Computer Desk Natural Ash37520001 68-Drawer Dresser White Ash75020001 7Dining TableNatural Ash85020001

4 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)];

5 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

6 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';

7 Comparison Operators Operator = > >= < <= <> Meaning Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to

8 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

9 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

10 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

11 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

12 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%';

13 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

14 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

15 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

16 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

17 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

18 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

19 Rules of Precedence Order EvaluatedOperator 1All comparison operators 2NOT 3AND 4OR Override rules of precedence using parentheses

20 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

21 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

22 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;

23 Sorting in Descending Order SQL> SELECT * 2 FROM product_t 3 ORDER BY product_description DESC; PRODUCT_IDPRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICEPRODUCT_LINE_ID 5Writers DeskCherry32510001 4Entertainment CenterNatural Oak65030001 1End TableCherry17510001 7Dining TableNatural Ash85020001 9Corner TableWalnut 10001 3Computer DeskNatural Ash37520001 8Computer DeskWalnut25030001 2Coffee TableNatural Ash20020001 68-Drawer DresserWhite Ash75020001

24 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

25 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 Ash75020001 2Coffee TableNatural Ash20020001 3Computer DeskNatural Ash37520001 8Computer DeskWalnut25030001 9Corner TableWalnut 10001 7Dining TableNatural Ash85020001 1End TableCherry17510001 4Entertainment CenterNatural Oak65030001 5Writers DeskCherry32510001

26 Summary SELECT[DISTINCT] {*, column [alias],...} FROM table [WHEREcondition(s)] [ORDER BY{column, expr, alias} [ASC|DESC]];


Download ppt "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."

Similar presentations


Ads by Google