Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Chen, Oracle Database System (Oracle) 1 HW Discussion Chapter 5 (#9) –Why need two “deletes”? SQL> --9. SQL> SQL> DELETE FROM orderitems 2 WHERE order#

Similar presentations


Presentation on theme: "Dr. Chen, Oracle Database System (Oracle) 1 HW Discussion Chapter 5 (#9) –Why need two “deletes”? SQL> --9. SQL> SQL> DELETE FROM orderitems 2 WHERE order#"— Presentation transcript:

1 Dr. Chen, Oracle Database System (Oracle) 1 HW Discussion Chapter 5 (#9) –Why need two “deletes”? SQL> --9. SQL> SQL> DELETE FROM orderitems 2 WHERE order# = 1005; 1 row deleted. SQL> SQL> DELETE FROM orders 2 WHERE order# = 1005; 1 row deleted. Order#Customer#OrderDateShipDateShipStreetShipCityShipStateShipZipShipCost NUMBER(4) DATE VARCHAR2(18)VARCHAR2(15)VARCHAR2(2)NUMBER(4)NUMBER(4,2) Order#Item#ISBNQuantityPaidEach NUMBER(4)NUMBER(2)VARCHAR2(10)NUMBER(3)NUMBER(5,2) ORDERS pk ORDERITEMS fk cpk

2 Dr. Chen, Oracle Database System (Oracle) 2 Database Development DDL DML DCL JL_D.B. ORACLE (SQL Components) (Retrieve Data and Produce Information)

3 Dr. Chen, Oracle Database System (Oracle) 3 Chapter 8 Restricting Rows and Sorting Data (from a Single Table) Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University Spokane, WA 99258 USA chen@jepson.gonzaga.edu

4 Dr. Chen, Oracle Database System (Oracle) 4 Objectives Use a WHERE clause to restrict the rows returned by a query Create a search condition using mathematical comparison operators Use the BETWEEN…AND comparison operator to identify records within a range of values Specify a list of values for a search condition using the IN comparison operator

5 Dr. Chen, Oracle Database System (Oracle) 5 Objectives (continued) Search for patterns using the LIKE comparison operator Identify the purpose of the % and _ wildcard characters Join multiple search conditions using the appropriate logical operator Perform searches for NULL values Specify the order for the presentation of query results using an ORDER BY clause Extra GROUP BY and HAVING examples are also introduced.

6 Dr. Chen, Oracle Database System (Oracle) 6 Refresh the Database Run the following script file –Start c:\oradata\chapter8\JLDB_Build_8.sql Note that we will first study “Logical Operator” follows by “Comparison Operator”.

7 Dr. Chen, Oracle Database System (Oracle) 7 WHERE Clause Syntax A WHERE clause is used to retrieve rows based on a stated condition Requires: –Column name –Comparison operator –Value or column for comparison Values are case sensitive (within ‘…’) Figure 8-1 Syntax of the SELECT statement SELECT [DISTINCT | UNIQUE] (*, field-1 [As alias], field-2, …) FROM tablename [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY columname];

8 Dr. Chen, Oracle Database System (Oracle) 8 MORE ON SELECT Figure 8-1 Syntax of the SELECT statement SELECT [DISTINCT | UNIQUE] (*, field-1 [As alias], field-2, …) FROM tablename-1, tablename-2, … [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY columname]; SELECT [DISTINCT | UNIQUE] (*, field-1 [As alias], field-2, …) FROM tablename [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY columname]; What is the difference between these two ‘SELECT” Single Table Multiple Tables

9 Dr. Chen, Oracle Database System (Oracle) 9 WHERE Clause Example List WHERE clause after FROM clause Enclose nonnumeric data in single quotes Figure 8-3 Results of a state-based search

10 Dr. Chen, Oracle Database System (Oracle) 10 Rules for Character Strings -- chapter 8, Figure 8-4; p.247 SELECT * FROM customers WHERE customer# = 1010; -- chapter 8, Figure 8-5; p.247 SELECT * FROM books WHERE isbn = 1915762492; --****************************** -- chapter 8, Figure 8-5(b); p.247 SELECT * FROM books WHERE isbn = '1915762492'; -- chapter 8, Figure 8-6; p.248 SELECT * FROM books WHERE pubdate = '21-JAN-05';

11 Dr. Chen, Oracle Database System (Oracle) 11 Logical Operators Used to combine conditions Evaluated in order of NOT, AND, OR –NOT – reverses meaning –AND – both conditions must be TRUE –OR – at least one condition must be TRUE

12 Dr. Chen, Oracle Database System (Oracle) 12 AND Logical Operator Example Figure 8-24 Searching with multiple conditions and the AND logical operator

13 Dr. Chen, Oracle Database System (Oracle) 13 OR Logical Operator Example Figure 8-25 Searching with multiple conditions and the OR logical operator

14 Dr. Chen, Oracle Database System (Oracle) 14 Multiple Logical Operators Resolved in order of NOT, AND, OR Figure 8-26 Searching with both AND and OR operators (NOT A GOOD WAY – why? See next example)

15 Dr. Chen, Oracle Database System (Oracle) 15 Multiple Logical Operators Use parentheses to override the order of evaluation Figure 8-27 Using parentheses to control the evaluation order for logical operators

16 Dr. Chen, Oracle Database System (Oracle) 16 Resolving Multiple Types of Operators 1.Arithmetic operators 2.Comparison operators 3.Logical operators

17 Dr. Chen, Oracle Database System (Oracle) 17 Comparison Operators Indicate how the data should relate to the given search value Figure 8-7 Searching for books with a retail price greater than $55 -- chapter 8, Figure 8-8; p.250 SELECT title FROM books WHERE title > 'HO';

18 Dr. Chen, Oracle Database System (Oracle) 18 Arithmetic Comparison Operators Table 8-2 Comparison Operators

19 Dr. Chen, Oracle Database System (Oracle) 19 Other Comparison Operators Table 8-2 Comparison Operators (cont.)

20 Dr. Chen, Oracle Database System (Oracle) 20 Practice … Figure 8-9 to Figure 8-13 (pp.250-255) -- chapter 8, Figure 8-9; p.251 SELECT title, retail-cost profit FROM books WHERE retail-cost < cost*.2; -- chapter 8, Figure 8-10; p.252 SELECT firstname, lastname, state FROM customers WHERE state <= 'GA'; -- chapter 8, Figure 8-11; p.253 SELECT firstname, lastname, state FROM customers WHERE state >= 'GA'; -- chapter 8, Figure 8-12; p.254 SELECT firstname, lastname, state FROM customers WHERE state <> 'GA'; -- chapter 8, Figure 8-13; p.255 SELECT order#, orderdate FROM orders WHERE orderdate < '01-APR-09'; What is the interpretation on this SQL command?

21 Dr. Chen, Oracle Database System (Oracle) 21 BETWEEN…AND Operator Finds values in a specified range Figure 8-14 Searching Pubid with the BETWEEN … AND operator

22 Dr. Chen, Oracle Database System (Oracle) 22 -- chapter 8, Figure 8-15(c); p.256 SELECT title FROM books WHERE title BETWEEN ‘B' AND ‘H'; TITLE ------------------------------ BODYBUILD IN 10 MINUTES A DAY BUILDING A CAR WITH TOOTHPICKS DATABASE IMPLEMENTATION COOKING WITH MUSHROOMS E-BUSINESS THE EASY WAY BIG BEAR AND LITTLE DOVE 6 rows selected. exclusive TITLE ------------------------------ BODYBUILD IN 10 MINUTES A DAY REVENGE OF MICKEY BUILDING A CAR WITH TOOTHPICKS DATABASE IMPLEMENTATION COOKING WITH MUSHROOMS HOLY GRAIL OF ORACLE HANDCRANKED COMPUTERS E-BUSINESS THE EASY WAY PAINLESS CHILD-REARING THE WOK WAY TO COOK BIG BEAR AND LITTLE DOVE HOW TO GET FASTER PIZZA HOW TO MANAGE THE MANAGER SHORTEST POEMS What is the output? How many records? inclusive -- chapter 8, Figure 8-15; p.256 SELECT title FROM books SELECT title FROM books WHERE title >= ‘B' AND title <=‘H';

23 Dr. Chen, Oracle Database System (Oracle) 23 -- chapter 8, Figure 8-15(d); p.256 SELECT title FROM books WHERE title BETWEEN 'B' AND 'HO'; TITLE ------------------------------ BODYBUILD IN 10 MINUTES A DAY BUILDING A CAR WITH TOOTHPICKS DATABASE IMPLEMENTATION COOKING WITH MUSHROOMS HANDCRANKED COMPUTERS E-BUSINESS THE EASY WAY BIG BEAR AND LITTLE DOVE 7 rows selected. TITLE ------------------------------ BODYBUILD IN 10 MINUTES A DAY REVENGE OF MICKEY BUILDING A CAR WITH TOOTHPICKS DATABASE IMPLEMENTATION COOKING WITH MUSHROOMS HOLY GRAIL OF ORACLE HANDCRANKED COMPUTERS E-BUSINESS THE EASY WAY PAINLESS CHILD-REARING THE WOK WAY TO COOK BIG BEAR AND LITTLE DOVE HOW TO GET FASTER PIZZA HOW TO MANAGE THE MANAGER SHORTEST POEMS -- chapter 8, Figure 8-15; p.256 SELECT title FROM books SELECT title FROM books WHERE title >= 'B' AND title <='HO'; What is the output? How many records?

24 Dr. Chen, Oracle Database System (Oracle) 24 SELECT orderdate FROM orders WHERE orderdate BETWEEN '31-Mar-09' AND '02-Apr-09’; ORDERDATE --------- 31-MAR-09 01-APR-09 02-APR-09 inclusive SELECT * FROM EmployeeAddressTable WHERE LastName BETWEEN 'Ackerman' AND 'Scott‘; Retrieve employees alphabetically between (and including) "Ackerman" and exclusive "Scott".

25 Dr. Chen, Oracle Database System (Oracle) 25 IN Operator and Example Returns records that match a value in a specified list List must be in parentheses Values are separated by commas What logical operator can be used to replace IN ? More on the next slide Figure 8-16 Searching Pubid with the IN operator

26 Dr. Chen, Oracle Database System (Oracle) 26 Other Examples (IN, NOT IN) -- chapter 8, Figure 8-17; p.257 SELECT firstname, lastname, state FROM customers WHERE state IN ('CA', 'TX'); -- chapter 8, Figure 8-18; p.258 SELECT firstname, lastname, state FROM customers WHERE state NOT IN ('CA', 'TX'); -- chapter 8, Figure 8-17(b) an alternate way SELECT firstname, lastname, state FROM customers WHERE state = 'CA‘ state = 'TX'; OR

27 Dr. Chen, Oracle Database System (Oracle) 27 LIKE Operator Performs pattern searches Used with wildcard characters –Underscore (_) for exactly one character in the indicated position –Percent sign (%) represents any number of characters Figure 8-19 Searching with the LIKE operator and the % wildcard character

28 Dr. Chen, Oracle Database System (Oracle) 28 More Examples -- chapter 8, Figure 8-20; p.259 SELECT customer#, lastname, firstname FROM customers WHERE customer# LIKE '10_9'; -- chapter 8, Figure 8-21; p.260 SELECT isbn, title FROM books WHERE isbn LIKE '_4%0'; -- chapter 8, Figure 8-22; p.261 SELECT * FROM testing;

29 Dr. Chen, Oracle Database System (Oracle) 29 Treatment of NULL Values Absence of data Requires use of IS NULL operator Figure 8-28 Searching for NULL values with the IS NULL operator -- chapter 8, Figure 8-29; p.266 SELECT order#, shipdate FROM orders WHERE shipdate IS NOT NUL

30 Dr. Chen, Oracle Database System (Oracle) 30 Treatment of NULL Values (continued) A common error is using = NULL, which does not raise an Oracle error but also does not return any rows Figure 8-30 Using the = NULL operator by mistake

31 Dr. Chen, Oracle Database System (Oracle) 31 An extra table for GROUP BY and HAVING loc_idbldg_coderoomcapacity NUMBER(6),VARCHAR2(10)VARCHAR2(6)NUMBER(5) location CREATE TABLE location (loc_id NUMBER(6), bldg_code VARCHAR2(10), room VARCHAR2(6), capacity NUMBER(5), CONSTRAINT location_loc_id_pk PRIMARY KEY (loc_id)); You need to download (create a new folder of \NW_CW\) and run the following command to make the example work: @ c:\oradata\NW_CW\northwoods.sql

32 Dr. Chen, Oracle Database System (Oracle) 32 GROUP BY Clauses – Extra Examples The group by clause is used to form groups of rows of a resulting table based on column clauses. When the group by clause is used, all aggregate operations are computed on the individual groups, not on the entire table. --Figure G-0 (wrong version) -- NOT a group by expression: -- ROOM SELECT bldg_code, room, SUM(capacity), AVG(capacity), MAX(capacity) FROM location WHERE capacity >= 5 GROUP BY bldg_code; --Figure G-1 -- right version of group by SELECT bldg_code, SUM(capacity), AVG(capacity), MAX(capacity) FROM location WHERE capacity >= 5 GROUP BY bldg_code; Output from Figure G-1 BLDG_CODE SUM(CAPACITY) AVG(CAPACITY) MAX(CAPACITY) ---------- ------------- ------------- ------------- BUS 167 41.75 55 CR 260 65 150

33 Dr. Chen, Oracle Database System (Oracle) 33 --Figure G-1 -- the right version of group by SELECT bldg_code, SUM(capacity), AVG(capacity), MAX(capacity) FROM location WHERE capacity >= 5 GROUP BY bldg_code; --Figure G-2 -- the right version of group by w/ having SELECT bldg_code, SUM(capacity) TOTAL_CAPACITY, AVG(capacity), MAX(capacity) FROM location WHERE capacity >= 5 GROUP BY bldg_code HAVING AVG(capacity) >=42; Output from Figure G-1 BLDG_CODE SUM(CAPACITY) AVG(CAPACITY) MAX(CAPACITY) ---------- ------------- ------------- ------------- BUS 167 41.75 55 CR 260 65 150 Output from Figure G-2 BLDG_CODE TOTAL_CAPACITY AVG(CAPACITY) MAX(CAPACITY) ---------- ------------- ------------- ------------- CR 260 65 150 The Group by and Having Clauses The having clause is used to eliminate certain groups from further consideration. The following query will produce the same results but with the AVG capacity of at least 42.

34 Dr. Chen, Oracle Database System (Oracle) 34 ORDER BY Clause Syntax The ORDER BY clause presents data in sorted order Ascending order is default Use DESC keyword to override column default 255 columns maximum Figure 8-31 Syntax of the SELECT statement

35 Dr. Chen, Oracle Database System (Oracle) 35 ORDER BY Clause Syntax Sort Sequence In ascending order, values will be listed in the following sequence: –Numeric values –Character values –NULL values In descending order, sequence is reversed

36 Dr. Chen, Oracle Database System (Oracle) 36 Examples on ORDER BY Figure 8-32, 34 Sorting results by publisher name in ascending and descending orders

37 Dr. Chen, Oracle Database System (Oracle) 37 Figure 8-36 The default sort order for NULL valuesFigure 8-37 Using the NULL FIRST option in the ORDER BY clause

38 Dr. Chen, Oracle Database System (Oracle) 38 Secondary Sort Figure 8-38 Using primary and secondary sort columns In the previous examples, only one column was specified in the ORDER BY clause, which is called a primary sort. In some cases, you might want to include a secondary sort, which specifies a second filed to sort b if an exact match occurs between two or more rows in the primary sort.

39 Dr. Chen, Oracle Database System (Oracle) 39 ORDER BY Example Figure 8-39 Sorting on the State and City columns

40 Dr. Chen, Oracle Database System (Oracle) 40 ORDER BY Can Reference Column Position Figure 8-40 Referencing positions of sort columns in the ORDER BY clause

41 Dr. Chen, Oracle Database System (Oracle) 41 Practice all the examples in the text. A Script file is available on the Bb (file name: Ch8Queries.sql) After completing all examples, do the HW.

42 Dr. Chen, Oracle Database System (Oracle) 42 Homework - Hands-On Assignments Read and Practice all examples on Chapters 8 1. Run the script files (in the folder \oradata\chapter8\): JLDB_Build_8.sql 2. Read Oracle assignment and create a script file Oracle_ch8_Lname_Fname.sql for questions (all EVEN numbers; p.281) on “Hands-on Assignments”. Use appropriate COLUMN statements to produce readable outputs if needed. 3. Execute and test one problem at a time and make sure they are all running successfully. 4. When you done, spool the script files (see next slide for spooling instructions) and UPLOAD the file (Oracle_ch8_Spool_Lname_Fname.txt) to Bb by the midnight before the next class. Upload the spooled file (*.txt) to the Bb (under “Assignments & Projects”) by the deadline.

43 Dr. Chen, Oracle Database System (Oracle) 43 How to Spool your Script and Output Files After you tested the script file of Oracle_ch6_Lname_Fname.sql successfully, follow the instructions below to spool both script and output files: Step 0. Run the following script file from SQL*Plus (since you have created JLDB tables) –Start c:\oradata\chapter6\JLDB_Build_6.sql 1. type the following on SQL> –Spool c:\oradata\Oracle_ch6_Spool_Lname_Fname.txt (make sure your name is entered) 2. open Oracle_ch6_Lname_Fname.sql that you already tested 3. copy and paste all the SQL commands (including all comments) to the SQL*PLUS 4. type Spool Off on the SQL> The output should contain your personal information, all SQL commands and their solution on the.txt file and saved in C: drive (oradata\ folder) Upload the spooled file (*.txt) to the Bb (under “Assignments & Projects”) by the deadline.

44 Dr. Chen, Oracle Database System (Oracle) 44 Summary The WHERE clause can be included in a SELECT statement to restrict the rows returned by a query to only those meeting a specified condition When searching a nonnumeric field, the search values must be enclosed in single quotation marks Comparison operators are used to indicate how the record should relate to the search value The BETWEEN...AND comparison operator is used to search for records that fall within a certain range of values

45 Dr. Chen, Oracle Database System (Oracle) 45 Summary (continued) The LIKE comparison operator is used with the percent and underscore symbols (% and _) to establish search patterns Logical operators such as AND and OR can be used to combine several search conditions When using the AND operator, all conditions must be TRUE for a record to be returned in the results –However, with the OR operator, only one condition must be TRUE A NULL value is the absence of data, not a field with a blank space entered

46 Dr. Chen, Oracle Database System (Oracle) 46 Summary (continued) Use the IS NULL comparison operator to match NULL values; the IS NOT NULL comparison operator finds records that do not contain NULL values in the indicated column You can sort the results of queries by using an ORDER BY clause; when used, the ORDER BY clause should be listed last in the SELECT statement By default, records are sorted in ascending order; entering DESC directly after the column name sorts the records in descending order A column does not have to be listed in the SELECT clause to serve as a basis for sorting


Download ppt "Dr. Chen, Oracle Database System (Oracle) 1 HW Discussion Chapter 5 (#9) –Why need two “deletes”? SQL> --9. SQL> SQL> DELETE FROM orderitems 2 WHERE order#"

Similar presentations


Ads by Google