bordoloi and bockcopyright 2004 prentice hall, inc.3-1 cos 346 day 14

54
Bordoloi and Bordoloi and Bock Bock Copyright 2004 Prentice Hall, Inc. 3-1 COS 346 COS 346 Day 14 Day 14

Post on 21-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-1

COS 346COS 346

Day 14Day 14

Page 2: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-2

AgendaAgenda

• Assignment 5 Corrected Assignment 5 Corrected – 1 A, 1 B, 3 C’s and 1 D1 A, 1 B, 3 C’s and 1 D– Lots of missing data Lots of missing data

• Assignment 6 Posted Assignment 6 Posted – Due March 22 Due March 22

• Capstone Progress Reports Over DueCapstone Progress Reports Over Due– Missing 3 Missing 3

• Some hands on work from chapter 2Some hands on work from chapter 2– SQL Coding Exercises and Question on page 44SQL Coding Exercises and Question on page 44

• Today we will discus Today we will discus – Single Table Query Basics (Chap 3)Single Table Query Basics (Chap 3)– Adding Power to Queries (Chap 4)Adding Power to Queries (Chap 4)– Lots of Hands-on stuffLots of Hands-on stuff

• We will be in the Oracle SQL text for the next 2+ weeksWe will be in the Oracle SQL text for the next 2+ weeks

Page 3: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-3

Alternate Web-Based AdminAlternate Web-Based Admin(fixed)(fixed)

http://littleblack.umfk.maine.edu:NNNN/emhttp://littleblack.umfk.maine.edu:NNNN/emWhere NNNN is Where NNNN is

Buddy = 5501Buddy = 5501Craig = 5502Craig = 5502Fred = 5503Fred = 5503G2 = 5504G2 = 5504Dawn = 5505 Dawn = 5505 Rob = 5506Rob = 5506Ray = 5507Ray = 5507

http://littleblack.umfk.maine.edu:1158/em/http://littleblack.umfk.maine.edu:1158/em/

Page 4: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-4

CHAPTER 3:CHAPTER 3:SINGLE TABLE QUERY BASICSSINGLE TABLE QUERY BASICS

Page 5: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-5

SIMPLE SELECT STATEMENTSSIMPLE SELECT STATEMENTS

• The main element in an SQL query is the The main element in an SQL query is the SELECT statement.SELECT statement.• A properly written SELECT statement will always A properly written SELECT statement will always produce a result in the form of one or more rows of produce a result in the form of one or more rows of output.output.• The SELECT statement chooses (selects) rows The SELECT statement chooses (selects) rows from one or more tables according to specific from one or more tables according to specific criteria. criteria.

Page 6: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-6

ExampleExample

SELECT *SELECT *FROM employee;FROM employee;EMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_MIDDLE_NAMEEMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_MIDDLE_NAME------------ ------------------------ ------------------------- --------------------------------------- ------------------------ ------------------------- ---------------------------999666666 Bordoloi Bijoy999666666 Bordoloi Bijoy999555555 Joyner Suzanne 999555555 Joyner Suzanne AA999444444 Zhu Waiman 999444444 Zhu Waiman ZZmore rows and columns will be displayedmore rows and columns will be displayed

• This query selects rows from the “employee” This query selects rows from the “employee” table.table.• The asterisk (*) tells Oracle to select (display) all The asterisk (*) tells Oracle to select (display) all columns contained in the table “employee.” columns contained in the table “employee.”

Page 7: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-7

ExampleExample

•The following SELECT statement produces an The following SELECT statement produces an identical output.identical output.

SELECT emp_ssn, emp_last_name, emp_first_name, emp_middle_name,SELECT emp_ssn, emp_last_name, emp_first_name, emp_middle_name, emp_address, emp_city, emp_state, emp_zip, emp_date_of_birth,emp_address, emp_city, emp_state, emp_zip, emp_date_of_birth, emp_salary, emp_parking_space, emp_gender,emp_salary, emp_parking_space, emp_gender, emp_dpt_number, emp_superssnemp_dpt_number, emp_superssnFROM employee;FROM employee;

Page 8: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-8

• Oracle will process the query regardless of Oracle will process the query regardless of whether you type the entire query on one line, or whether you type the entire query on one line, or indent.indent.

• There are no rules about how many words can There are no rules about how many words can be put on a line or where to break a line. be put on a line or where to break a line.

• Although Oracle does not require indenting, Although Oracle does not require indenting, indenting enhances readability. indenting enhances readability.

Indenting SQL CodeIndenting SQL Code

Page 9: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-9

• The following keywords are your signal to start The following keywords are your signal to start a new line.a new line.

» SELECTSELECT

» FROMFROM

» WHEREWHERE

» GROUP BYGROUP BY

» HAVINGHAVING

» ORDER BY ORDER BY

Indenting SQL CodeIndenting SQL Code

Page 10: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-10

• Specify the column names to be displayed in the Specify the column names to be displayed in the result set by typing the exact, complete column result set by typing the exact, complete column names. names.

• Separate each column name with a comma (,).Separate each column name with a comma (,).• Specify the name of the table after the FROM Specify the name of the table after the FROM

clause.clause.• Terminate the query with a semicolon (;).Terminate the query with a semicolon (;).

SELECT emp_ssn, emp_last_name, emp_first_nameSELECT emp_ssn, emp_last_name, emp_first_name

FROM employee;FROM employee;

Selecting Specific ColumnsSelecting Specific Columns

Page 11: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-11

Using Column Commands Using Column Commands

• IF the result out put is “too long” to viewIF the result out put is “too long” to view– SET WRAP OFF;SET WRAP OFF;

• Prevent text wrappingPrevent text wrapping

– COLUMN <col_name> FORMAT A15 COLUMN <col_name> FORMAT A15 • 15 characters wide 15 characters wide

– COLUMN <col_name> FORMAT 99999.99COLUMN <col_name> FORMAT 99999.99– COLUMN <col_name> FORMAT $99,999.9COLUMN <col_name> FORMAT $99,999.9

Page 12: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-12

COLUMN emp_salary FORMAT 99999.99;

select emp_salaryfrom employee;

COLUMN emp_salary FORMAT $99,999.99;

select emp_salaryfrom employee;

EMP_SALARY---------- 55000.00 43000.00 43000.00 25000.00 25000.00 30000.00 38000.00 25000.00

8 rows selected.

EMP_SALARY----------- $55,000.00 $43,000.00 $43,000.00 $25,000.00 $25,000.00 $30,000.00 $38,000.00 $25,000.00

8 rows selected.

Page 13: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-13

• There are syntactical rules that must be followed There are syntactical rules that must be followed or Oracle gives an error message instead of the or Oracle gives an error message instead of the desired result table. desired result table.

• The Oracle relational database management The Oracle relational database management system communicates errors in SELECT system communicates errors in SELECT statements by providing unique error numbers statements by providing unique error numbers and accompanying error descriptions. and accompanying error descriptions.

Common ErrorsCommon Errors

Page 14: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-14

Invalid Column NameInvalid Column Name

• In this SELECT statement, the employee social In this SELECT statement, the employee social security number column name is spelled security number column name is spelled incorrectly.incorrectly.

SELECT emp_socsecnoSELECT emp_socsecno

FROM employee;FROM employee;

ERROR at line 1:ERROR at line 1:

ORA-00904: invalid column nameORA-00904: invalid column name

Page 15: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-15

FROM Keyword MissingFROM Keyword Missing

• The next SELECT statement is missing the FROM The next SELECT statement is missing the FROM clause so that no table name has been specified.clause so that no table name has been specified.

• Without a table name, the database management Without a table name, the database management system does not know which table to query.system does not know which table to query.

SELECT emp_ssn;SELECT emp_ssn;

ERROR at line 1:ERROR at line 1:

ORA-00923: FROM keyword not found where expectedORA-00923: FROM keyword not found where expected

Page 16: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-16

THE DISTINCT CLAUSETHE DISTINCT CLAUSE• Oracle provides a means for eliminating duplicate rows Oracle provides a means for eliminating duplicate rows

in a result table through use of the DISTINCT keyword .in a result table through use of the DISTINCT keyword .SELECT emp_salarySELECT emp_salaryFROM employee;FROM employee;EMP_SALARY EMP_SALARY ----------------- ----------------- $55,000.00 $55,000.00 $43,000.00 $43,000.00 $43,000.00 $43,000.00 $25,000.00 $25,000.00 $25,000.00 $25,000.00 $30,000.00 $30,000.00 $38,000.00 $38,000.00 $25,000.00 $25,000.00 8 rows selected.8 rows selected.

Page 17: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-17

DISTINCTDISTINCT

• The query is rewritten using the DISTINCT The query is rewritten using the DISTINCT keyword to eliminate duplicate rows.keyword to eliminate duplicate rows.

SELECT DISTINCT emp_salarySELECT DISTINCT emp_salaryFROM employee;FROM employee; EMP_SALARYEMP_SALARY-------------------------------------- $25,000.00$25,000.00 $30,000.00$30,000.00 $38,000.00$38,000.00 $43,000.00$43,000.00 $55,000.00$55,000.00

Page 18: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-18

THE WHERE CLAUSETHE WHERE CLAUSE

• Specific rows can be selected by adding a WHERE Specific rows can be selected by adding a WHERE clause to the SELECT query. clause to the SELECT query.

SELECT emp_ssn, emp_last_name, emp_first_name, emp_salarySELECT emp_ssn, emp_last_name, emp_first_name, emp_salary

FROM employeeFROM employee

WHERE emp_salary >= 35000; WHERE emp_salary >= 35000;

EMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_SALARYEMP_SSN EMP_LAST_NAME EMP_FIRST_NAME EMP_SALARY

-------------- -------------------------- --------------------------- --------------------------------- -------------------------- --------------------------- -------------------

999666666 Bordoloi 999666666 Bordoloi Bijoy $55,000.00 Bijoy $55,000.00

999555555 Joyner Suzanne $43,000.00999555555 Joyner Suzanne $43,000.00

999444444 Zhu Waiman $43,000.00999444444 Zhu Waiman $43,000.00

more rows will be displayed…more rows will be displayed…

Page 19: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-19

Comparison OperatorsComparison Operators

Operator MeaningOperator Meaning == equal toequal to << less thanless than >> greater thangreater than >=>= greater than or equal togreater than or equal to <=<= less than or equal toless than or equal to !=!= not equal tonot equal to <><> not equal to not equal to !>!> not greater thannot greater than !<!< not less thannot less than

Page 20: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-20

Comparing Character DataComparing Character Data

• Comparison operators are not limited to numeric data.Comparison operators are not limited to numeric data.• They can also be used with columns containing They can also be used with columns containing

character data. character data. • If the value is a character string or date, you must If the value is a character string or date, you must

surround the value (string of characters) with which a surround the value (string of characters) with which a column is being compared with column is being compared with single quotation single quotation (' ')(' ') marks.marks.

SELECT emp_ssn, emp_last_name, emp_first_nameSELECT emp_ssn, emp_last_name, emp_first_name

FROM employeeFROM employee

WHERE emp_gender = 'M'; WHERE emp_gender = 'M';

Page 21: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-21

Comparing Character DataComparing Character Data

SELECT emp_ssn, emp_last_name, emp_first_nameSELECT emp_ssn, emp_last_name, emp_first_nameFROM employeeFROM employeeWHERE emp_gender = M; WHERE emp_gender = M;

ERROR at line 3: ERROR at line 3: ORA-00904: invalid column name ORA-00904: invalid column name

• Since the literal string value was not enclosed by single Since the literal string value was not enclosed by single quote marks, Oracle assumed the letter M to be a quote marks, Oracle assumed the letter M to be a column name.column name.

• There is no column named M in the table so an error There is no column named M in the table so an error was returned. was returned.

Page 22: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-22

THE ORDER BY CLAUSETHE ORDER BY CLAUSE

• Output from a SELECT statement can be sorted by Output from a SELECT statement can be sorted by using the optional ORDER BY clause. using the optional ORDER BY clause.

SELECT emp_last_name, emp_first_nameSELECT emp_last_name, emp_first_nameFROM employeeFROM employeeWHERE emp_last_name >= 'J'WHERE emp_last_name >= 'J'ORDER BY emp_last_name;ORDER BY emp_last_name;EMP_LAST_NAME EMP_FIRST_NAMEEMP_LAST_NAME EMP_FIRST_NAME------------------------- --------------------------------------------------- --------------------------Joshi Joshi Dinesh DineshJoyner Joyner Suzanne SuzanneMarkis Markis Marcia Marciamore rows will be displayedmore rows will be displayed……

Page 23: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-23

ORDER BY With ASC and DESCORDER BY With ASC and DESC

• To sort columns from high to low, or descending, an To sort columns from high to low, or descending, an optional keyword DESC must be specified.optional keyword DESC must be specified.

ASC - Ascending, low to high.ASC - Ascending, low to high. DESC - Descending, high to low. DESC - Descending, high to low.

• When ASC or DESC is used, it must be followed by the When ASC or DESC is used, it must be followed by the column name.column name.

Page 24: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-24

ORDER BY With More Than One ColumnORDER BY With More Than One Column

• Sorting by multiple columns improves the look and Sorting by multiple columns improves the look and usability of the information.usability of the information.

SELECT emp_dpt_number, emp_last_name, emp_first_nameSELECT emp_dpt_number, emp_last_name, emp_first_nameFROM employeeFROM employeeORDER BY emp_dpt_number, emp_last_name;ORDER BY emp_dpt_number, emp_last_name;EMP_DPT_NUMBER EMP_LAST_NAME EMP_FIRST_NAMEEMP_DPT_NUMBER EMP_LAST_NAME EMP_FIRST_NAME---------------------------- -------------------------- ------------------------------------------- -------------------------- --------------- 1 1 Bordoloi Bordoloi Bijoy Bijoy

33 Amin Amin Hyder Hyder 3 3 Joyner Suzanne Joyner Suzanne 3 3 Markis Markis Marcia Marcia 7 7 Bock Bock Douglas Douglas 7 7 Joshi Joshi Dinesh Dinesh 7 7 Prescott Prescott Sherri Sherri 7 7 Zhu Zhu Waiman Waiman

Page 25: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-25

CHAPTER 4:CHAPTER 4:ADDING POWER TO QUERIESADDING POWER TO QUERIES

Page 26: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-26

LOGICAL OPERATORSLOGICAL OPERATORS (AND, OR, AND NOT) (AND, OR, AND NOT)

• AND: Joins two or more conditions, and returns AND: Joins two or more conditions, and returns results only when results only when allall of the conditions are true. of the conditions are true.

• OR: Joins two or more conditions, and returns OR: Joins two or more conditions, and returns results when results when any any of the conditions are true.of the conditions are true.

• NOT: Negates the expression that follows it.NOT: Negates the expression that follows it.

Page 27: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-27

AND OPERATORAND OPERATOR

Last Name First Name GenderLast Name First Name Gender

---------- ------------- ---------------- ------------- ------

Joyner Suzanne FJoyner Suzanne F

Markis Marcia FMarkis Marcia F

Prescott Sherri FPrescott Sherri F

Page 28: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-28

OR OPERATOROR OPERATOR

SELECT emp_last_name "Last Name",SELECT emp_last_name "Last Name",emp_first_name "First Name",emp_first_name "First Name",emp_gender "Gender"emp_gender "Gender"FROM employeeFROM employeeWHERE emp_gender = 'F' OR emp_last_name > 'M'WHERE emp_gender = 'F' OR emp_last_name > 'M'ORDER BY emp_last_name;ORDER BY emp_last_name;

Last Name First Name GenderLast Name First Name Gender--------------- --------------- --------------------- --------------- ------Joyner Suzanne FJoyner Suzanne FMarkis Marcia FMarkis Marcia FPrescott Sherri FPrescott Sherri FZhu Waiman MZhu Waiman M

Page 29: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-29

NOT OPERATORNOT OPERATOR

SELECT emp_last_name "Last Name", SELECT emp_last_name "Last Name", emp_first_name "First Name",emp_first_name "First Name",emp_dpt_number "Dept"emp_dpt_number "Dept"FROM employeeFROM employeeWHERE NOT emp_dpt_number = 7WHERE NOT emp_dpt_number = 7ORDER BY emp_last_name;ORDER BY emp_last_name;

Last Name First Name DeptLast Name First Name Dept--------------- --------------- -------------------- --------------- -----Amin Hyder 3Amin Hyder 3Bordoloi Bijoy 1Bordoloi Bijoy 1Joyner Suzanne 3Joyner Suzanne 3Markis Marcia 3Markis Marcia 3  

Page 30: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-30

Combining OR and AND OperatorsCombining OR and AND Operators

• When the AND operator is combined with OR, When the AND operator is combined with OR, the Oracle server will evaluate the condition the Oracle server will evaluate the condition connected by the AND connected by the AND firstfirst before any before any conditions connected with OR.conditions connected with OR.

• Parenthesis must be used to force an order of Parenthesis must be used to force an order of operation. operation.

Page 31: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-31

Combining OR and AND OperatorsCombining OR and AND Operators

SELECT emp_last_name "Last Name",SELECT emp_last_name "Last Name",emp_first_name "First Name",emp_first_name "First Name",emp_dpt_number "Dept", emp_gender "Gender" emp_dpt_number "Dept", emp_gender "Gender" FROM employeeFROM employeeWHERE emp_last_name > 'E' AND emp_gender = 'F‘WHERE emp_last_name > 'E' AND emp_gender = 'F‘OR emp_dpt_number = 1OR emp_dpt_number = 1ORDER BY emp_last_name;ORDER BY emp_last_name;  Last Name First Name Dept GenderLast Name First Name Dept Gender--------------- -------------- ----- --------------------- -------------- ----- ------Bordoloi Bijoy 1 MBordoloi Bijoy 1 MJoyner Suzanne 3 FJoyner Suzanne 3 FMarkis Marcia 3 FMarkis Marcia 3 FPrescott Sherri 7 FPrescott Sherri 7 F

Page 32: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-32

Combining OR and AND Operators Combining OR and AND Operators

SELECT emp_last_name "Last Name", SELECT emp_last_name "Last Name", emp_first_name "First Name",emp_first_name "First Name",emp_dpt_number "Dept", emp_gender "Gender" emp_dpt_number "Dept", emp_gender "Gender" FROM employee FROM employee WHERE emp_last_name > 'E' AND WHERE emp_last_name > 'E' AND (emp_gender = 'F' OR emp_dpt_number = 1)(emp_gender = 'F' OR emp_dpt_number = 1)ORDER BY emp_last_name; ORDER BY emp_last_name;   Last Name First Name Dept GenderLast Name First Name Dept Gender--------------- --------------- ----- --------------------- --------------- ----- ------Joyner Suzanne 3 FJoyner Suzanne 3 FMarkis Marcia 3 FMarkis Marcia 3 FPrescott Sherri 7 FPrescott Sherri 7 F

Page 33: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-33

LISTS (IN and NOT IN)LISTS (IN and NOT IN)

• There are two operators that are designed for testing to There are two operators that are designed for testing to determine if data stored in a table column are either in or determine if data stored in a table column are either in or not in a list or set of values.not in a list or set of values.

• These are the IN and NOT IN operators. These are the IN and NOT IN operators.

• These operators greatly simplify the task of writing queries These operators greatly simplify the task of writing queries that might otherwise require a large number of either OR that might otherwise require a large number of either OR logical operators or an unwieldy use of the NOT logical logical operators or an unwieldy use of the NOT logical operator. operator.

Page 34: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-34

Using IN OperatorUsing IN Operator

SELECT emp_last_name "Last Name", emp_first_name "First SELECT emp_last_name "Last Name", emp_first_name "First Name",Name", emp_salary "Salary"emp_salary "Salary"FROM employeeFROM employeeWHERE emp_salary = 43000 OR emp_salary = 30000 OR WHERE emp_salary = 43000 OR emp_salary = 30000 OR emp_salary = 25000emp_salary = 25000ORDER BY emp_salary;ORDER BY emp_salary;  Last Name First Name SalaryLast Name First Name Salary--------------- --------------- -------------------------- --------------- -----------Markis Marcia $25,000.00Markis Marcia $25,000.00Amin Hyder $25,000.00Amin Hyder $25,000.00Prescott Sherri $25,000.00Prescott Sherri $25,000.00Bock Douglas $30,000.00Bock Douglas $30,000.00Joyner Suzanne $43,000.00Joyner Suzanne $43,000.00Zhu Waiman $43,000.00Zhu Waiman $43,000.006 rows selected.6 rows selected.

Page 35: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-35

Using IN OperatorUsing IN Operator

SELECT emp_last_name "Last Name", emp_first_name "First Name",SELECT emp_last_name "Last Name", emp_first_name "First Name",emp_salary "Salary"emp_salary "Salary"FROM employee FROM employee WHERE emp_salary IN (43000, 30000, 25000)WHERE emp_salary IN (43000, 30000, 25000)ORDER BY emp_salary;ORDER BY emp_salary;  Last Name First Name SalaryLast Name First Name Salary--------------- --------------- -------------------------- --------------- -----------Markis Marcia $25,000.00Markis Marcia $25,000.00Amin Hyder $25,000.00Amin Hyder $25,000.00Prescott Sherri $25,000.00Prescott Sherri $25,000.00Bock Douglas $30,000.00Bock Douglas $30,000.00Joyner Suzanne $43,000.00Joyner Suzanne $43,000.00Zhu Waiman $43,000.00Zhu Waiman $43,000.006 rows selected.6 rows selected.

Page 36: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-36

Using IN OperatorUsing IN Operator

SELECT emp_last_name "Last Name", emp_first_name "First SELECT emp_last_name "Last Name", emp_first_name "First Name",Name", emp_city "City"emp_city "City"FROM employeeFROM employeeWHERE emp_city IN ('Marina', 'Edwardsville', 'St. Louis')WHERE emp_city IN ('Marina', 'Edwardsville', 'St. Louis')ORDER BY emp_city;ORDER BY emp_city;  Last Name First Name CityLast Name First Name City--------------- --------------- ---------------------------- --------------- -------------Bordoloi Bijoy EdwardsvilleBordoloi Bijoy EdwardsvillePrescott Sherri EdwardsvillePrescott Sherri EdwardsvilleJoyner Suzanne MarinaJoyner Suzanne MarinaAmin Hyder MarinaAmin Hyder MarinaZhu Waiman St. LouisZhu Waiman St. LouisBock Douglas St. LouisBock Douglas St. Louis

  

Page 37: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-37

Using the NOT IN OperatorUsing the NOT IN Operator

• NOT can precede IN to form negative.NOT can precede IN to form negative.• To get the list of employees who did not earn the To get the list of employees who did not earn the

three salary figures listed above:three salary figures listed above:

SELECT emp_last_name "Last Name", emp_first_name "First Name",SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary "Salary"emp_salary "Salary"FROM employeeFROM employeeWHERE emp_salary NOT IN (43000, 30000, 25000)WHERE emp_salary NOT IN (43000, 30000, 25000)ORDER BY emp_salary;ORDER BY emp_salary;  Last Name First Name SalaryLast Name First Name Salary--------------- --------------- -------------------------- --------------- -----------Joshi Dinesh $38,000.00Joshi Dinesh $38,000.00Bordoloi Bijoy $55,000.00Bordoloi Bijoy $55,000.00  

Page 38: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-38

RANGES (BETWEEN and NOT BETWEEN)RANGES (BETWEEN and NOT BETWEEN)

• Oracle provides two operators, BETWEEN and Oracle provides two operators, BETWEEN and NOT BETWEEN that can simply the range of NOT BETWEEN that can simply the range of values in a query.values in a query.

• This eliminates the need to use a more complex This eliminates the need to use a more complex WHERE clause involving the use of the AND WHERE clause involving the use of the AND logical operator.logical operator.

Page 39: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-39

Using the BETWEEN OperatorUsing the BETWEEN Operator

• The following query uses the AND logical operator.The following query uses the AND logical operator.

SELECT emp_last_name "Last Name", emp_first_name "First Name",SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary "Salary"emp_salary "Salary"FROM employeeFROM employeeWHERE emp_salary >= 25000 AND emp_salary <= 40000WHERE emp_salary >= 25000 AND emp_salary <= 40000ORDER BY emp_salary;ORDER BY emp_salary;

Last Name First Name SalaryLast Name First Name Salary-------------- --------------- ------------------------- --------------- -----------Markis Marcia $25,000.00Markis Marcia $25,000.00Amin Hyder $25,000.00Amin Hyder $25,000.00Prescott Sherri $25,000.00Prescott Sherri $25,000.00Bock Douglas $30,000.00Bock Douglas $30,000.00Joshi Dinesh $38,000.00Joshi Dinesh $38,000.00

Page 40: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-40

Using the BETWEEN OperatorUsing the BETWEEN Operator

• The query can be rewritten using the BETWEEN The query can be rewritten using the BETWEEN operator. operator.

SELECT emp_last_name "Last Name", emp_first_name "First Name",SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary "Salary"emp_salary "Salary"FROM employee FROM employee WHERE emp_salary BETWEEN 25000 AND 40000WHERE emp_salary BETWEEN 25000 AND 40000ORDER BY emp_salary;ORDER BY emp_salary;Last Name First Name SalaryLast Name First Name Salary--------------- --------------- -------------------------- --------------- -----------Markis Marcia $25,000.00Markis Marcia $25,000.00Amin Hyder $25,000.00Amin Hyder $25,000.00Prescott Sherri $25,000.00Prescott Sherri $25,000.00Bock Douglas $30,000.00Bock Douglas $30,000.00Joshi Dinesh $38,000.00Joshi Dinesh $38,000.00

Page 41: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-41

Using the NOT BETWEEN OperatorUsing the NOT BETWEEN Operator

SELECT emp_last_name "Last Name", emp_salary "Salary"SELECT emp_last_name "Last Name", emp_salary "Salary"

FROM employee FROM employee

WHERE emp_salary NOT BETWEEN 28000 AND 50000WHERE emp_salary NOT BETWEEN 28000 AND 50000

ORDER BY emp_salary;ORDER BY emp_salary;

Last Name SalaryLast Name Salary

--------------- -------------------------- -----------

Markis $25,000.00Markis $25,000.00

Amin $25,000.00Amin $25,000.00

Prescott $25,000.00Prescott $25,000.00

Bordoloi $55,000.00Bordoloi $55,000.00

Page 42: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-42

LIKE and NOT LIKELIKE and NOT LIKE

• The LIKE and NOT LIKE operators can be used to The LIKE and NOT LIKE operators can be used to search for data rows containing incomplete or partial search for data rows containing incomplete or partial character strings within a data column. character strings within a data column.

• The next query searches the employee table for The next query searches the employee table for employee names that begin with the characters 'Bo'.employee names that begin with the characters 'Bo'.

• The search is case-sensitive meaning that 'Bo' is not The search is case-sensitive meaning that 'Bo' is not equivalent to 'BO'.equivalent to 'BO'.

Page 43: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-43

LIKE and NOT LIKELIKE and NOT LIKE

SELECT emp_last_name "Last Name", emp_first_name "First SELECT emp_last_name "Last Name", emp_first_name "First Name"Name"

FROM employeeFROM employee

WHERE emp_last_name LIKE 'Bo%';WHERE emp_last_name LIKE 'Bo%';

Last Name First NameLast Name First Name

------------- -------------------------- -------------

Bordoloi BijoyBordoloi Bijoy

Bock DouglasBock Douglas

Page 44: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-44

LIKE and NOT LIKELIKE and NOT LIKE

Wild cardWild card MeaningMeaning

% % (percent) (percent) any string of zero or more charactersany string of zero or more characters

_ (underscore)_ (underscore) any single characterany single character

[ ] (brackets)[ ] (brackets) any single character within a specified range such as 'any single character within a specified range such as 'aa' ' to 'to 'dd', inclusive [a-d] or a set of characters such as ', inclusive [a-d] or a set of characters such as [aeiouy][aeiouy]

[^] (not brackets)[^] (not brackets) any single character any single character not not in the specified range or set. in the specified range or set. (e.g., [^a-f] )(e.g., [^a-f] )

Page 45: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-45

MORE EXAMPLESMORE EXAMPLES

• LIKE '%inger' will search for every name that ends LIKE '%inger' will search for every name that ends with 'inger' (Rwith 'inger' (Ringeringer, Str, Stringeringer).).

• LIKE '%en%' will search for every name that has LIKE '%en%' will search for every name that has the letters 'en' in the name (Bthe letters 'en' in the name (Benennet, Grenet, Greenen, , McBaddMcBaddenen).).

• LIKE '_heryl' will search for every six-letter name LIKE '_heryl' will search for every six-letter name ending with 'heryl' (Cending with 'heryl' (Cherylheryl). Notice how this is ). Notice how this is different than '%heryl' which would return names different than '%heryl' which would return names that are six characters or more.that are six characters or more.

Page 46: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-46

MORE EXAMPLESMORE EXAMPLES

• LIKE '[CK]ars[eo]n' will search for every six-letter LIKE '[CK]ars[eo]n' will search for every six-letter name that begins with a 'C' or 'K' and has the letter name that begins with a 'C' or 'K' and has the letter 'e' or 'o' between 'ars' and 'n' (e.g., ''e' or 'o' between 'ars' and 'n' (e.g., 'CCarsarseen,' n,' ''KKarsarseen,' 'n,' 'CCarsarsoon,' and 'n,' and 'KKarsarsoon'.n'.

• LIKE '[M-Z]inger' will search for all the names LIKE '[M-Z]inger' will search for all the names ending with 'inger' that begin with any single letter ending with 'inger' that begin with any single letter 'M' thru 'Z' (R'M' thru 'Z' (Ringeringer).).

• LIKE 'M[^c]%' will search for all the names that LIKE 'M[^c]%' will search for all the names that begin with 'M' not having 'c' as the second letter.begin with 'M' not having 'c' as the second letter.

Page 47: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-47

MORE EXAMPLESMORE EXAMPLES

•The SELECT statement shown below generates a The SELECT statement shown below generates a result table that includes all DISTINCT rows where result table that includes all DISTINCT rows where the employee social security number in the the employee social security number in the assignmentassignment table ends with the numbers 555. table ends with the numbers 555.

SELECT DISTINCT work_emp_ssn "Emp SSN"SELECT DISTINCT work_emp_ssn "Emp SSN"

FROM assignmentFROM assignment

WHERE work_emp_ssn LIKE '%555';WHERE work_emp_ssn LIKE '%555';

Emp SSNEmp SSN

------------------------

999555555999555555

Page 48: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-48

MORE EXAMPLESMORE EXAMPLES

ExpressionExpression ResultResult

LIKE ‘5%’ LIKE ‘5%’ Returns any row where the column data Returns any row where the column data value is '5' followed by any string of zero value is '5' followed by any string of zero

or more characters.or more characters.

LIKE ‘5[%]’ LIKE ‘5[%]’ Returns any row where the data value is Returns any row where the data value is 5%.5%.

LIKE ‘_n’ LIKE ‘_n’ Returns any row where the column data Returns any row where the column data value is a two-character value ending in value is a two-character value ending in

the letter 'n', e.g., an, in, on, etc.the letter 'n', e.g., an, in, on, etc.

LIKE ‘[ _ ]n’ LIKE ‘[ _ ]n’ Returns any row where the column data Returns any row where the column data value is _n.value is _n.

LIKE ‘[ ] ]’ LIKE ‘[ ] ]’ Returns any row where the column data Returns any row where the column data value is ].value is ].

Page 49: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-49

UNKNOWN VALUESUNKNOWN VALUES (IS NULL and IS NOT NULL) (IS NULL and IS NOT NULL)

• NULL value is NULL value is not synonymous not synonymous with "zero" with "zero" (numerical values) or "blank" (character values).(numerical values) or "blank" (character values).

• NULL values allow users to distinguish between a NULL values allow users to distinguish between a deliberate entry of zero/blank and a non-entry of deliberate entry of zero/blank and a non-entry of data.data.

SELECT *SELECT *

FROM assignment FROM assignment

WHERE work_hours IS NULL;WHERE work_hours IS NULL;

WORK_EMP_ WORK_PRO_NUMBER WORK_HOURSWORK_EMP_ WORK_PRO_NUMBER WORK_HOURS

--------- --------------- ------------------- --------------- ----------

999444444 1999444444 1

999666666 20999666666 20

Page 50: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-50

MORE EXAMPLESMORE EXAMPLES

SELECT *SELECT *

FROM assignmentFROM assignment

WHERE work_hours = 0;WHERE work_hours = 0;

no rows selectedno rows selected

• The query did not return a result table because none The query did not return a result table because none of the rows in the assignment table have a zero of the rows in the assignment table have a zero value for work_hours. Thus, you can see that zero value for work_hours. Thus, you can see that zero (0) is a value, not an "unknown value." (0) is a value, not an "unknown value."

Page 51: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-51

MORE EXAMPLESMORE EXAMPLES

• The NOT NULL condition can also be tested.The NOT NULL condition can also be tested.

SELECT *SELECT *

FROM assignment FROM assignment

WHERE work_hours IS NOT NULL;WHERE work_hours IS NOT NULL;

15 rows selected.15 rows selected.

• The result table contains all the rows where The result table contains all the rows where work_hours column contains a value. work_hours column contains a value.

Page 52: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-52

EXPRESSIONS IN SELECT CLAUSESEXPRESSIONS IN SELECT CLAUSES

• An expression is formed by combining a column An expression is formed by combining a column name or constant with an arithmetic operator.name or constant with an arithmetic operator.

• The arithmetic operators used in SQL are The arithmetic operators used in SQL are

SYMBOL OPERATION ORDER

* Multiplication 1

/ Division 1

% Modulo 1

+ Addition 2

- Subtraction 2

Page 53: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-53

MORE EXAMPLESMORE EXAMPLES

SELECT emp_last_name "Last Name", emp_first_name "First SELECT emp_last_name "Last Name", emp_first_name "First Name", Name",

emp_salary/12 "Monthly Salary"emp_salary/12 "Monthly Salary"FROM employeeFROM employeeWHERE emp_salary/12 > 3500WHERE emp_salary/12 > 3500ORDER BY emp_last_name;ORDER BY emp_last_name;  Last Name First Name Monthly SalaryLast Name First Name Monthly Salary--------------- --------------- ----------------------------- --------------- --------------Bordoloi Bijoy $4,583.33Bordoloi Bijoy $4,583.33Joyner Suzanne $3,583.33Joyner Suzanne $3,583.33Zhu Waiman $3,583.33 Zhu Waiman $3,583.33 

Page 54: Bordoloi and BockCopyright 2004 Prentice Hall, Inc.3-1 COS 346 Day 14

Bordoloi and BockBordoloi and Bock Copyright 2004 Prentice Hall, Inc. 3-54

EXPRESSIONSEXPRESSIONS

• When expression is used in select with a column When expression is used in select with a column name it has no effect on the table’s underlying name it has no effect on the table’s underlying values.values.

• The data contained in each column must be The data contained in each column must be defined as int, smallint, tinyint, float, money, and defined as int, smallint, tinyint, float, money, and smallmoney.smallmoney.

• The result of an arithmetic operation on NULL is The result of an arithmetic operation on NULL is NULL. NULL.