You are on page 1of 56

Getting Started with Oracle SQL

3

Copyright © 2004, Oracle. All rights reserved.

Objectives
After completing this lesson, you should be able to do the following: • Execute a SELECT statement • Implement pseudocolumns in SQL • Utilize the different types of operators, conditions, expressions, and functions in SQL • Convert between data types • Describe how SQL treats NULLs

3-2

Copyright © 2004, Oracle. All rights reserved.

Retrieving Data by Using the SELECT Statement
• • • SELECT identifies which columns are to be displayed. FROM identifies which table contains the columns. WHERE specifies how the rows are restricted.
[DISTINCT] {*, column [alias], expr...} table(s) condition(s)] expr [, expr]...] condition] {column, expr, alias} [ASC|DESC]];

SELECT FROM [WHERE [GROUP BY [HAVING [ORDER BY

3-3

Copyright © 2004, Oracle. All rights reserved.

3-4

Copyright © 2004, Oracle. All rights reserved.

Restricting Output by Using the WHERE Clause
Syntax:
SELECT FROM [WHERE [DISTINCT] {*, | column [alias], | expr...} table(s) condition(s)]

Example:
SELECT FROM WHERE OR last_name, job_id, hire_date, salary employees job_id = 'ST_CLERK' AND hire_date > '01-Jan-00' salary > 14000;

3-5

Copyright © 2004, Oracle. All rights reserved.

3-6

Copyright © 2004, Oracle. All rights reserved.

Defining Null
• • NULL represents a value that is unavailable, unassigned, unknown, or inapplicable. NULL is not the same as zero or a blank space.

SELECT department_id, employee_id, last_name, job_id, salary, commission_pct FROM employees WHERE job_id in ('ST_CLERK', 'SA_MAN') AND employee_id > 142;

3-7

Copyright © 2004, Oracle. All rights reserved.

Using Pseudocolumns
SELECT FROM WHERE ROWID, last_name employees department_id = 20;

Not found

3-8

Copyright © 2004, Oracle. All rights reserved.

Using Pseudocolumns
• ROWID returns a row’s address:
ROWID, last_name employees department_id = 20;

SELECT FROM WHERE

ROWNUM returns a number indicating the order in which the row is selected:
rownum, last_name, job_id, manager_id employees rownum < 5;

SELECT FROM WHERE

3-9

Copyright © 2004, Oracle. All rights reserved.

3-10

Copyright © 2004, Oracle. All rights reserved.

Manipulating Data Items by Using Operators
Operator LIKE IS NULL BETWEEN ...AND... IN (set) != ^=, := <> NOT LIKE IS NOT NULL Meaning Matches a character pattern Is a null Is between two values (inclusive) Matches any of a list of values Is not equal to (VAX, UNIX, PC) Is not equal to (IBM) Is not equal to (all systems) Is not like comparison string Is not a null

3-11

Copyright © 2004, Oracle. All rights reserved.

3-12

Copyright © 2004, Oracle. All rights reserved.

Calculating Using Arithmetic Expressions

Create expressions on NUMBER and DATE data by using arithmetic operators:
Operator * / + – Description Multiply Divide Add Subtract Priority 1 1 2 2

• •

Operators of the same priority are evaluated from left to right. Parentheses are used to force prioritized evaluation and to clarify statements.
Copyright © 2004, Oracle. All rights reserved.

3-13

Manipulating Strings by Using the Concatenation Operator
SELECT FROM WHERE last_name||' works as '||job_id AS "Employees" employees employee_id > 200;

3-14

Copyright © 2004, Oracle. All rights reserved.

Combining Results by Using Set Operators
UNION UNION ALL
A B A B

A

B

INTERSECT

A

B

MINUS

3-15

Copyright © 2004, Oracle. All rights reserved.

Using the UNION Operator
Display the current and previous job details of all employees, displaying each combination of employee and job ID only once:
SELECT FROM UNION SELECT FROM employee_id, job_id employees employee_id, job_id job_history;

… …
3-16 Copyright © 2004, Oracle. All rights reserved.

Using the UNION Operator

SELECT FROM UNION SELECT FROM

employee_id, job_id, department_id employees employee_id, job_id, department_id job_history;

3-17

Copyright © 2004, Oracle. All rights reserved.

Using Expressions
• Simple expression:
– – – – – Column Pseudocolumn Constant Sequence number NULL

Compound expression: A combination of other expressions

3-18

Copyright © 2004, Oracle. All rights reserved.

Conditional Querying Using the CASE Expression
Facilitates conditional queries by doing the work of an IF-THEN-ELSE statement:
CASE expr WHEN [WHEN WHEN ELSE END comparison_expr1 THEN return_expr1 comparison_expr2 THEN return_expr2 comparison_exprn THEN return_exprn else_expr]

Example:
SELECT last_name, job_id, salary, CASE job_id WHEN 'IT_PROG' THEN 1.10*salary WHEN 'ST_CLERK' THEN 1.15*salary WHEN 'SA_REP' THEN 1.20*salary ELSE salary END "REVISED_SALARY" FROM employees;

3-19

Copyright © 2004, Oracle. All rights reserved.

3-20

Copyright © 2004, Oracle. All rights reserved.

Using Conditions

Condition EXISTS IN

Description Tests for existence of rows in a subquery Tests a value for membership in a list of values or subquery

IS EMPTY

Tests whether a specified nested table is empty, regardless of whether any elements of the collection are NULL

3-21

Copyright © 2004, Oracle. All rights reserved.

Understanding Functions

Input arg 1 arg 2

Function Function performs the operation.

Output

Result value

arg n

3-22

Copyright © 2004, Oracle. All rights reserved.

Two Types of SQL Functions

Function

Single-row functions

Multiple-row functions

3-23

Copyright © 2004, Oracle. All rights reserved.

Manipulating Data Items Using Single-Row Functions
Character

General

Single-row functions

Number

Conversion

Datetime

Date

3-24

Copyright © 2004, Oracle. All rights reserved.

3-25

Copyright © 2004, Oracle. All rights reserved.

Using Numeric Functions
• • • • ROUND: Rounds a value to a specified decimal
– ROUND(45.926, 2) – TRUNC(45.926, 2) 45.93 45.92 100

TRUNC: Truncates a value to a specified decimal MOD: ABS: Returns remainder of division Returns the absolute value of the argument
15 –1

– MOD(1600, 300)

– ABS(–15)

SIGN: Returns –1 or 1 based on the argument
– SIGN(-15)

3-26

Copyright © 2004, Oracle. All rights reserved.

Using the ROUND and TRUNC Functions

SELECT ROUND(45.923, 2), ROUND(45.923, -1), ROUND(45.923, 0) FROM dual;

SELECT FROM

TRUNC(45.923, 2), TRUNC(45.923, -1), TRUNC(45.923,0) dual;

3-27

Copyright © 2004, Oracle. All rights reserved.

3-28

Copyright © 2004, Oracle. All rights reserved.

Practice 3: Overview of Part 1
This practice covers the following topics: • Executing a SELECT statement • Implementing pseudocolumns in SQL • Utilizing the different types of operators, conditions, expressions, and functions in SQL

3-29

Copyright © 2004, Oracle. All rights reserved.

Understanding Data Types
Data Type VARCHAR2(size) CHAR(size) NUMBER(p,s) DATE LONG CLOB RAW and LONG RAW BLOB BFILE ROWID Description Variable-length character data Fixed-length character data Variable-length numeric data Date and time values Variable-length character data, up to 2 GB Character data, up to 128 TB Raw binary data Binary data, up to 128 TB Binary data stored in an external file, up to 128 TB A 64-base number representing the unique address of a row in its table
Copyright © 2004, Oracle. All rights reserved.

3-30

3-31

Copyright © 2004, Oracle. All rights reserved.

3-32

Copyright © 2004, Oracle. All rights reserved.

Using Datetime Data Types
In Oracle SQL, you can include the time zone in your date and time data, and provide support for fractional seconds. Three additional data types: • TIMESTAMP • TIMESTAMP WITH TIME ZONE (TSTZ) • TIMESTAMP WITH LOCAL TIME ZONE (TSLTZ) Daylight savings support for datetime data types is provided.

3-33

Copyright © 2004, Oracle. All rights reserved.

3-34

Copyright © 2004, Oracle. All rights reserved.

Using Datetime Data Types

TIMESTAMP data type:

TIMESTAMP[(fractional_seconds_precision)]

TIMESTAMP WITH TIME ZONE data type:

TIMESTAMP[(fractional_seconds_precision)] WITH TIME ZONE

TIMESTAMP WITH LOCAL TIME ZONE data type:

TIMESTAMP[(fractional_seconds_precision)] WITH LOCAL TIME ZONE

3-35

Copyright © 2004, Oracle. All rights reserved.

3-36

Copyright © 2004, Oracle. All rights reserved.

Using INTERVAL Data Types
• INTERVAL YEAR TO MONTH stores a period of time using the YEAR and MONTH datetime fields.

INTERVAL YEAR [(year_precision)] TO MONTH

INTERVAL DAY TO SECOND stores a period of time in terms of days, hours, minutes, and seconds.

INTERVAL DAY [(day_precision)] TO SECOND [(fractional_seconds_precision)]

3-37

Copyright © 2004, Oracle. All rights reserved.

3-38

Copyright © 2004, Oracle. All rights reserved.

3-39

Copyright © 2004, Oracle. All rights reserved.

Converting Between Data Types
• • Data type conversion can be implicit or explicit. Implicit data type conversion: For assignments, the Oracle server can automatically convert the following:
From To

VARCHAR2 or CHAR NUMBER or DATE NUMBER DATE VARCHAR2 VARCHAR2

3-40

Copyright © 2004, Oracle. All rights reserved.

3-41

Copyright © 2004, Oracle. All rights reserved.

Converting Data Types Explicitly

TO_NUMBER

TO_DATE

NUMBER

CHARACTER

DATE

TO_CHAR

TO_CHAR

3-42

Copyright © 2004, Oracle. All rights reserved.

Displaying a Date in a Specific Format
The format model: • Must be enclosed in single quotation marks and is case sensitive • Can include any valid date format element • Has an fm element to remove padded blanks or suppress leading zeros • Is separated from the date value by a comma
TO_CHAR(date, 'fmt')

3-43

Copyright © 2004, Oracle. All rights reserved.

Date Format Model Elements

YYYY YEAR MM MONTH DY DAY

Full year in numbers Year spelled out Two-digit value for month Full name of the month Three-letter abbreviation of the day of the week Full name of the day

3-44

Copyright © 2004, Oracle. All rights reserved.

Date Format Model Elements
• Use time elements to format the time portion of the date:
15:45:32 p.m.

HH24:MI:SS AM

Add character strings by enclosing them in double quotation marks:
12 of OCTOBER

DD "of" MONTH

Use number suffixes to spell out numbers:
fourteenth

ddspth

3-45

Copyright © 2004, Oracle. All rights reserved.

3-46

Copyright © 2004, Oracle. All rights reserved.

Using the TO_CHAR Function with Dates and Numbers
SELECT last_name, TO_CHAR(hire_date, 'fmDD Month YYYY') HIREDATE FROM employees WHERE employee_id > 200;

SELECT FROM WHERE

TO_CHAR(salary,'$99,999') SALARY employees last_name = 'Kochhar';

3-47

Copyright © 2004, Oracle. All rights reserved.

3-48

Copyright © 2004, Oracle. All rights reserved.

3-49

Copyright © 2004, Oracle. All rights reserved.

Using the TO_NUMBER and TO_DATE Functions
• To convert a character string to a number format, use the TO_NUMBER function:

TO_NUMBER(char[, 'fmt'])

To convert a character string to a date format, use the TO_DATE function:

TO_DATE(char[, 'fmt'])

3-50

Copyright © 2004, Oracle. All rights reserved.

Summary
In this lesson, you should have learned how to: • Execute a SELECT statement • Implement pseudocolumns in SQL • Utilize the different types of operators, conditions, expressions, and functions in SQL • Convert between data types • Describe how SQL treats NULLs

3-51

Copyright © 2004, Oracle. All rights reserved.

Practice 3: Overview of Part 2
This practice covers the following topics: • Converting between data types • Describing how SQL treats NULLs

3-52

Copyright © 2004, Oracle. All rights reserved.

3-53

Copyright © 2004, Oracle. All rights reserved.

3-54

Copyright © 2004, Oracle. All rights reserved.

3-55

Copyright © 2004, Oracle. All rights reserved.

3-56

Copyright © 2004, Oracle. All rights reserved.