- •Preface
- •Introduction
- •Lesson Objectives
- •Lesson Agenda
- •Course Objectives
- •Course Agenda
- •Appendixes Used in the Course
- •Lesson Agenda
- •Oracle Database 11g: Focus Areas
- •Oracle Database 11g
- •Oracle Fusion Middleware
- •Oracle Enterprise Manager Grid Control 10g
- •Oracle BI Publisher
- •Lesson Agenda
- •Relational and Object Relational Database Management Systems
- •Data Storage on Different Media
- •Relational Database Concept
- •Definition of a Relational Database
- •Data Models
- •Entity Relationship Model
- •Relating Multiple Tables
- •Relational Database Terminology
- •Lesson Agenda
- •Using SQL to Query Your Database
- •SQL Statements
- •Development Environments for SQL
- •Lesson Agenda
- •The Human Resources (HR) Schema
- •Tables Used in the Course
- •Lesson Agenda
- •Oracle Database 11g Documentation
- •Additional Resources
- •Summary
- •Practice I: Overview
- •Objectives
- •Lesson Agenda
- •Capabilities of SQL SELECT Statements
- •Basic SELECT Statement
- •Selecting All Columns
- •Selecting Specific Columns
- •Writing SQL Statements
- •Column Heading Defaults
- •Lesson Agenda
- •Arithmetic Expressions
- •Using Arithmetic Operators
- •Operator Precedence
- •Defining a Null Value
- •Null Values in Arithmetic Expressions
- •Lesson Agenda
- •Defining a Column Alias
- •Using Column Aliases
- •Lesson Agenda
- •Concatenation Operator
- •Literal Character Strings
- •Using Literal Character Strings
- •Alternative Quote (q) Operator
- •Duplicate Rows
- •Lesson Agenda
- •Displaying the Table Structure
- •Using the DESCRIBE Command
- •Quiz
- •Summary
- •Practice 1: Overview
- •Objectives
- •Lesson Agenda
- •Limiting Rows Using a Selection
- •Limiting the Rows That Are Selected
- •Using the WHERE Clause
- •Character Strings and Dates
- •Comparison Operators
- •Using Comparison Operators
- •Range Conditions Using the BETWEEN Operator
- •Membership Condition Using the IN Operator
- •Pattern Matching Using the LIKE Operator
- •Combining Wildcard Characters
- •Using the NULL Conditions
- •Defining Conditions Using the Logical Operators
- •Using the AND Operator
- •Using the OR Operator
- •Using the NOT Operator
- •Lesson Agenda
- •Rules of Precedence
- •Lesson Agenda
- •Using the ORDER BY Clause
- •Sorting
- •Lesson Agenda
- •Substitution Variables
- •Using the Single-Ampersand Substitution Variable
- •Character and Date Values with Substitution Variables
- •Specifying Column Names, Expressions, and Text
- •Using the Double-Ampersand Substitution Variable
- •Lesson Agenda
- •Using the DEFINE Command
- •Using the VERIFY Command
- •Quiz
- •Summary
- •Practice 2: Overview
- •Objectives
- •Lesson Agenda
- •SQL Functions
- •Two Types of SQL Functions
- •Single-Row Functions
- •Lesson Agenda
- •Character Functions
- •Case-Conversion Functions
- •Using Case-Conversion Functions
- •Character-Manipulation Functions
- •Using the Character-Manipulation Functions
- •Lesson Agenda
- •Number Functions
- •Using the ROUND Function
- •Using the TRUNC Function
- •Using the MOD Function
- •Lesson Agenda
- •Working with Dates
- •RR Date Format
- •Using the SYSDATE Function
- •Arithmetic with Dates
- •Using Arithmetic Operators with Dates
- •Lesson Agenda
- •Date-Manipulation Functions
- •Using Date Functions
- •Using ROUND and TRUNC Functions with Dates
- •Quiz
- •Summary
- •Practice 3: Overview
- •Objectives
- •Lesson Agenda
- •Conversion Functions
- •Implicit Data Type Conversion
- •Explicit Data Type Conversion
- •Lesson Agenda
- •Using the TO_CHAR Function with Dates
- •Elements of the Date Format Model
- •Using the TO_CHAR Function with Dates
- •Using the TO_CHAR Function with Numbers
- •Using the TO_NUMBER and TO_DATE Functions
- •Using the TO_CHAR and TO_DATE Function with RR Date Format
- •Lesson Agenda
- •Nesting Functions
- •Lesson Agenda
- •General Functions
- •NVL Function
- •Using the NVL Function
- •Using the NVL2 Function
- •Using the NULLIF Function
- •Using the COALESCE Function
- •Lesson Agenda
- •Conditional Expressions
- •CASE Expression
- •Using the CASE Expression
- •DECODE Function
- •Using the DECODE Function
- •Quiz
- •Summary
- •Practice 4: Overview
- •Objectives
- •Lesson Agenda
- •What Are Group Functions?
- •Types of Group Functions
- •Group Functions: Syntax
- •Using the AVG and SUM Functions
- •Using the MIN and MAX Functions
- •Using the COUNT Function
- •Using the DISTINCT Keyword
- •Group Functions and Null Values
- •Lesson Agenda
- •Creating Groups of Data
- •Creating Groups of Data: GROUP BY Clause Syntax
- •Using the GROUP BY Clause
- •Grouping by More than One Column
- •Using the GROUP BY Clause on Multiple Columns
- •Illegal Queries Using Group Functions
- •Restricting Group Results
- •Restricting Group Results with the HAVING Clause
- •Using the HAVING Clause
- •Lesson Agenda
- •Nesting Group Functions
- •Quiz
- •Summary
- •Practice 5: Overview
- •Objectives
- •Lesson Agenda
- •Types of Joins
- •Joining Tables Using SQL:1999 Syntax
- •Qualifying Ambiguous Column Names
- •Lesson Agenda
- •Creating Natural Joins
- •Retrieving Records with Natural Joins
- •Creating Joins with the USING Clause
- •Joining Column Names
- •Retrieving Records with the USING Clause
- •Using Table Aliases with the USING Clause
- •Creating Joins with the ON Clause
- •Retrieving Records with the ON Clause
- •Creating Three-Way Joins with the ON Clause
- •Applying Additional Conditions to a Join
- •Lesson Agenda
- •Joining a Table to Itself
- •Self-Joins Using the ON Clause
- •Lesson Agenda
- •Nonequijoins
- •Retrieving Records with Nonequijoins
- •Lesson Agenda
- •INNER Versus OUTER Joins
- •LEFT OUTER JOIN
- •RIGHT OUTER JOIN
- •FULL OUTER JOIN
- •Lesson Agenda
- •Cartesian Products
- •Generating a Cartesian Product
- •Creating Cross Joins
- •Quiz
- •Summary
- •Practice 6: Overview
- •Objectives
- •Lesson Agenda
- •Using a Subquery to Solve a Problem
- •Subquery Syntax
- •Using a Subquery
- •Guidelines for Using Subqueries
- •Types of Subqueries
- •Lesson Agenda
- •Single-Row Subqueries
- •Executing Single-Row Subqueries
- •Using Group Functions in a Subquery
- •The HAVING Clause with Subqueries
- •What Is Wrong with This Statement?
- •No Rows Returned by the Inner Query
- •Lesson Agenda
- •Multiple-Row Subqueries
- •Lesson Agenda
- •Null Values in a Subquery
- •Quiz
- •Summary
- •Practice 7: Overview
- •Objectives
- •Lesson Agenda
- •Set Operators
- •Set Operator Guidelines
- •The Oracle Server and Set Operators
- •Lesson Agenda
- •Tables Used in This Lesson
- •Lesson Agenda
- •UNION Operator
- •Using the UNION Operator
- •UNION ALL Operator
- •Using the UNION ALL Operator
- •Lesson Agenda
- •INTERSECT Operator
- •Using the INTERSECT Operator
- •Lesson Agenda
- •MINUS Operator
- •Using the MINUS Operator
- •Lesson Agenda
- •Matching the SELECT Statements
- •Matching the SELECT Statement: Example
- •Lesson Agenda
- •Using the ORDER BY Clause in Set Operations
- •Quiz
- •Summary
- •Practice 8: Overview
Date-Manipulation Functions
Function |
Result |
|
|
MONTHS_BETWEEN |
Number of months between two dates |
|
|
ADD_MONTHS |
Add calendar months to date |
|
|
NEXT_DAY |
Next day of the date specified |
LAST_DAY |
Last day of the month |
|
|
ROUND |
Round date |
TRUNC |
Truncate date |
|
|
|
|
|
Copyright © 2009, Oracle. All rights reserved. |
Academy |
|
||
Date-Manipulation Functions |
|
|
|
|
|||
|
Date functions operate on Oracle dates. All date functions return a value of the DATE data type |
||||||
|
except MONTHS_BETWEEN, which returns a numeric value. |
|
|
||||
|
• |
MONTHS_BETWEEN(date1, date2): Finds the number of months between date1 and |
|||||
|
|
date2. The result can be positive or negative. If date1 is later than date2, the result is |
|||||
|
|
positive; if date1 is earlier than date2, theOracleresult is negative. The noninteger part of the result |
|||||
|
|
represents a portion of the month. |
& |
|
|
|
|
|
• |
|
|
|
|
|
|
|
ADD_MONTHS(date, n): Adds n number of calendar months to date. The value of n must |
||||||
|
|
be an integer and can be negative. |
Only |
|
|
||
|
|
|
|
|
|||
|
• |
NEXT_DAY(date, 'char'): Finds the date of the next specified day of the week |
|||||
|
|
('char') following date. The value of char may be a number representing a day or a |
|||||
|
|
character string. |
|
Use |
|
|
|
|
• LAST_DAY(date): Finds the date of the last day of the month that contains date |
||||||
|
|
Internal |
|
|
|
||
The above list is a subset of the available date functions. ROUND and TRUNC number functions can also be used to manipulate the date values as shown below:
•ROUND(date[,'fmt']): Returns date rounded to the unit that is specified by the format model fmt. If the format model fmt is omitted, date is rounded to the nearest day.
•TRUNC(date[, 'fmt']): Returns date with the time portion of the day truncated to the unit that is specified by the format model fmt. If the format model fmt is omitted, date isOracle
truncated to the nearest day.
The format models are covered in detail in the next lesson titled “Using Conversion Functions and Conditional Expressions.”
Oracle Database 11g: SQL Fundamentals I 3 - 28
Using Date Functions
Function |
|
Result |
|
|
|
MONTHS_BETWEEN |
19.6774194 |
|
|
('01-SEP-95','11-JAN-94') |
|
|
|
|
ADD_MONTHS |
(‘31-JAN-96',1) |
‘29-FEB-96' |
|
|
|
NEXT_DAY |
('01-SEP-95','FRIDAY') |
'08-SEP-95' |
|
|
|
LAST_DAY |
('01-FEB-95') |
'28-FEB-95' |
|
|
|
|
Copyright © 2009, Oracle. All rights reserved. |
Academy |
Using Date Functions |
|
|
In the slide example, the ADD_MONTHS function adds one month to the supplied date value, “31- |
||
|
Oracle |
For example, display the employee number, hire date, number of months employed, six-month |
|
review date, first Friday after hire date, and&the last day of the hire month for all employees who have |
|
been employed for fewer than 100 months. |
Only |
|
|
SELECT employee id, hire dat , |
|
MONTHS BETWEEN (SYSDATE, hire_date) TENURE, |
|
Use |
|
FROM Internalemployees |
|
JAN-96” and returns “29-FEB-96.” The function recognizes the year 1996 as the leap year and hence returns the last day of the February month. If you change the input date value to “31-JAN-95,” the function returns “28-FEB-95.”
ADD_MONTHS (hire date, 6) REVIEW,
NEXT DAY (hire date, 'FRIDAY'), LAST_DAY(hire_date) OracleWHERE MONTHS_BETWEEN (SYSDATE, hire_date) < 100;
Oracle Database 11g: SQL Fundamentals I 3 - 29
Using ROUND and TRUNC Functions with Dates
Assume SYSDATE = '25-JUL-03':
Function |
Result |
|
|
ROUND(SYSDATE,'MONTH') |
01-AUG-03 |
|
|
ROUND(SYSDATE ,'YEAR') |
01-JAN-04 |
|
|
TRUNC(SYSDATE ,'MONTH') |
01-JUL-03 |
|
|
TRUNC(SYSDATE ,'YEAR') |
01-JAN-03 |
|
|
Copyright © 2009, Oracle. All rights reserved. |
Academy |
|
Using ROUND and TRUNC Functions with Dates
The ROUND and TRUNC functions can be used forOraclenumber and date values. When used with dates, result in January 1 of the current year. Months 7-12 result in January 1 of the next year.
these functions round or truncate to the specified format model. Therefore, you can round dates to the nearest year or month. If the format model is month, dates 1-15 result in the first day of the current month. Dates 16-31 result in the first day of the next month. If the format model is year, months 1-6
Example: |
|
|
|
& |
|
|
Internal |
||
Compare the hire dates for all employees who startedOnlyin 1997. Display the employee number, hire |
||||
date, and starting month using the |
Use |
|||
ROUND and TRUNC functions. |
||||
|
SELECT employ |
id, hire date, |
||
|
ROUND(hire date, 'MONTH'), TRUNC(hire_date, 'MONTH') |
|||
|
FROM |
|
employees |
|
Oracle |
hire_date LIKE '%97'; |
|||
|
WHERE |
|||
Oracle Database 11g: SQL Fundamentals I 3 - 30
Quiz
Which of the following statements are true about single-row functions?
1.Manipulate data items
2.Accept arguments and return one value per argument
3.Act on each row that is returned
4.Return one result per set of rows
5.May not modify the data type
6.Can be nested
7.Accept arguments that can be a column or an expression
Answer: 1, 3, 6, 7 |
Copyright © 2009, Oracle. All rights reserved. |
Academy |
|||
|
|
|
|||
|
|
|
Oracle |
||
|
|
& |
Only |
|
|
|
Internal |
|
|
|
|
Oracle |
Use |
|
|||
|
|
|
|
||
Oracle Database 11g: SQL Fundamentals I 3 - 31
