The WHERE clause, also called the predicate, provides the power to narrow down the scope of the data...

24
DATA WAREHOUSING TARLETON STATE UNIVERSITY MATH 586 INSTRUCTORS: KEITH EMMERT, SEAN PERRY, MICHA ROBERSON, AND MICHAEL SCHUCKING COURSE MATERIALS: ORACLE SQL BY EXAMPLE, 4 TH EDITION BY ALICE RISCHERT SOFTWARE: ORACLE 11G TEXTBOOK RESOURCE WEBSITE: HTTP://WWW.ORACLESQLBYEXAMPLE.COM/ COURSE OBJECTIVES: UNDERSTAND AND APPLY FUNDAMENTAL SQL FUNCTIONS, EXPRESSIONS, COMMANDS ETC. A. CREATE/DROP TABLES/DATABASE B. QUERY TABLES C. USE AGGREGATE FUNCTIONS D. USE INNER/OUTER/LEFT/RIGHT JOINS E. WRITE QUERIES WITH STRING, NUMERIC, CONVERSION, AND DATE/TIME FUNCTIONS. CONDUCT QA AND INTEGRITY CHECKS.

Transcript of The WHERE clause, also called the predicate, provides the power to narrow down the scope of the data...

DATA WAREHOUSINGTARLETON STATE UNIVERSITYMATH 586

INSTRUCTORS: KEITH EMMERT, SEAN PERRY, MICHA ROBERSON, AND

MICHAEL SCHUCKING

COURSE MATERIALS: ORACLE SQL BY EXAMPLE, 4TH EDITION BY ALICE RISCHERTSOFTWARE: ORACLE 11GTEXTBOOK RESOURCE WEBSITE:

HTTP://WWW.ORACLESQLBYEXAMPLE.COM/

COURSE OBJECTIVES:UNDERSTAND AND APPLY FUNDAMENTAL SQL FUNCTIONS, EXPRESSIONS, COMMANDS ETC.

A. CREATE/DROP TABLES/DATABASEB. QUERY TABLESC. USE AGGREGATE FUNCTIONSD. USE INNER/OUTER/LEFT/RIGHT JOINSE. WRITE QUERIES WITH STRING, NUMERIC,

CONVERSION, AND DATE/TIME FUNCTIONS. CONDUCT QA AND INTEGRITY CHECKS.

CHAPTER 3 THE WHERE CLAUSE The WHERE clause, also called the predicate,

provides the power to narrow down the scope of the data retrieved.

Comparison OperatorsComparison Operator

Definition

= Equal

!=,<> Not equal

>,>= Greater than, greater than or equal to

<,<= Less than, less than or equal to

BETWEEN…And.. Inclusive of two values

LIKE Pattern matching with wildcard characters % and _

IN(…) List of values

IS NULL Test for null values

THE EQUALITY AND INEQUALITY OPERATORS Equality, denoted by =

Select first_name, last_name, phone from instructor

where last_name = ‘Schorin’; Inequality, denoted by != Data types must be the same.

THE GREATER THAN OR LESS THAN OPERATORS >,<, >=,<=

Select description, costfrom coursewhere cost >= 1195;

THE BETWEEN OPERATOR Tests for a range of values.

Select description, costfrom coursewhere cost between 1000 and 1100;

THE IN OPERATOR Works with a list of value, separated by

commas, contained within a set of parentheses.

Select description, costfrom coursewhere cost in (1095, 1595);

THE LIKE OPERATOR Performs pattern matching using wild

cards % and _

Select first_name, last_name, phonefrom instructorwhere last_name like ‘%S%’;

Select first_name, last_name, phonefrom instructorwhere last_name like ‘_o%’;

THE NOT OPERATOR All previously mentioned operators can

be negated with the NOT comparison operator.

Select phonefrom instructorwhere last_name not like ‘%S%’;

THE IS NULL AND IS NOT NULL OPERATORS Is Null and Is Not Null determines

whether there is an unknown value in the data.

Select description, prerequisitefrom coursewhere prerequisite Is Null;

LOGICAL OPERATORS Comparison operators can be combined with the help of the

logical operators AND and OR

Select description, costfrom coursewhere cost = 1095 or cost = 1195 and description LIKE ‘I

%’;

AND always takes precedence over OR, but precedence can be changed with parentheses.

Select description, costfrom coursewhere (cost = 1095 or cost = 1195) and description like ‘I

%’;

NULLS AND LOGICAL OPERATORS SQL uses tri-value logic; this means a

condition can evaluate to true, false, or unknown(null);

Null values cannot be compared so they cannot be returned for queries using non-null values with comparison operators.

AND TRUTH TABLE

TRUE FALSE UNKNOWN

TRUE TRUE FALSE UNKNOWN

FALSE FALSE FALSE FALSE

UNKNOWN UNKNOWN FALSE UNKNOWN

OR TRUTH TABLE TRUE FALSE UNKNOW

N

TRUE TRUE TRUE TRUE

FALSE TRUE FALSE UNKNOWN

UNKNOWN

TRUE UNKNOWN UNKNOWN

NOT TRUTH TABLE

TRUE FALSE UNKNOWN

NOT FALSE TRUE UNKNOWN

LAB 3.1 EXCERCISES Go to page 111 in book Answer and discuss answers in class.

LAB 3.2 THE ORDER BY CLAUSE Data is not stored in any particular order Result sets are displayed in whatever order

they are returned from the database The ORDER BY clause to is used to order data

any way you wish. You can use ASC or DESC and the sequence

number of the column instead of the name.

Select course_no, descriptionfrom coursewhere prerequisite is NULLORDER BY DESCRIPTION.

DISTINCT AND ORDER BY If the SELECT list contains DISTINCT,

the column(s)the keyword pertains to must also be listed in the ORDER BY clause.

SELECT distinct first_name, last_namefrom studentwhere zip =‘10025’order by first_name, last_name;

NULLS FIRST AND NULLS LAST The default sort order is to have nulls

listed last. This can be changed by using NULLS

FIRST or NULLS LAST in the ORDER BY clause.

Select DISTINCT costfrom courseorder by cost nulls first;

SORTING DATA USING SQL DEVELOPER’S GUI FUNCTIONALITY Double-click one of the columns in the

column header to sort. You can also use the SQL Developer

Data tab, which allows you to retrieve data from a table without writing a SQL statement.

Sort and filters can be done using this tab.

COLUMN ALIAS A column alias can be used in the

SELECT list to give a column or an expression an alias.

You can order by an alias name.

Select first_name first, first_name “First Name”, first_name as “First”

From studentWhere zip = ‘10025’;

COMMENTS IN SQL STATEMENTS Placing comments or remarks in a SQL

statement is very useful for documenting purpose, thought, and ideas.

Very handy for when you have developed multiple statements saved in a script.

You must identify a comment with either a – or /* */

--This is a single line comment/*This is a multi-linecomment */

SAVING YOUR SQL STATEMENTS Click CTRL+S on the keyboard, the save

icon, or go to FileSave to save the sql script .

To open the file, use CTRL+O, the open icon, or go to FileOpen.

ORACLE ERROR MESSAGES You will inevitably make mistakes. Oracle

returns an error number and an error message to inform you of any mistake.

Errors can be one or many and the error message is not always indicative of the problem.

The parser works from the front of the query and works backward; therefore, the first error message will be for the first error the parser encounters.

You can look up the Oracle error message in the Oracle Database Error Messages manual at Oracle.com or refer to Appendix G and Appendix H.

LAB 3.2: EXERCISES Go to page 127 in book Answer and discuss answers in class.

CHAPTER 3 COMPLETE! Quiz will be given at the beginning of

our next class