Professional Documents
Culture Documents
Oracle Database
A structured collection and repository of records or
data
Latest Version of Oracle database is Oracle 10g
Databases before Oracle 8i are pure Relational
Database Management Systems (RDBMS)
Databases after Oracle 8i are Object - Relational
Databases.
Oracle E-business Suite can use Oracle 8i/9i or 10g
SQL
SQL stands for Structured Query
Language
It is used to communicate with the
Oracle Database
Provides the ability to insert, update or
delete data in database
Tables
A relational database contains many
tables
Data or information for the database is
stored in these tables
Tables have one or more columns and
each column has a datatype
The table rows contain the actual data
Tables
Example of a Employees table
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%‘;
NULL Values
A NULL value means a value is unavailable,
unassigned, unknown or inapplicable
It is not equal to 0 or ‘ ‘ or ‘’ or ‘NULL’. It is a
big VOID
We cannot use the regular comparison
operators. To test if a value in a particular
column is NULL we use ‘IS NULL’
Example:
SELECT last_name, first_name
FROM employees
WHERE manager_id IS NULL;
Logical Conditions
AND Operator
AND condition requires both the
conditions to be true inorder to be true
The following is the truth table for AND
OR Operator
OR Condition requires atleast one
condition to be true
Following is the truth table for OR
condition
NOT Operator
NOT operator return TRUE if the
condition returns FALSE and vice versa
Examples:
Character Functions
Case-Manipulation Functions:
LOWER, UPPER, INITCAP
Character Manipulation Functions:
CONCAT, SUBSTR, LENGTH, INSTR, LPAD, TRIM, REPLACED
Number Functions
ROUND, TRUNC, MOD
Dates in Oracle
Dates are stored in a numeric format internally:
century, year, month, day, hours, minutes and
seconds
Default date format is ‘DD-MON-RR’
Function SYSDATE returns the current date and time
SELECT SYSDATE FROM DUAL;
Add and subtract a number from a date to get a resultant date
Subtract two dates to get number of days between
Date Functions
Conversion Functions
Oracle performs implicit conversion within the server
For explicit conversion you have to use appropriate
functions
TO_CHAR to display Dates
Use TO_CHAR(pDate, ‘format’); to display Dates in
desired format
Assume pDate = ’25-MAR-08’
TO_CHAR(pDate, ‘DD-MONTH-YYYY’) => 25-MARCH-2008
TO_CHAR(pDate, ‘DDspth “of” MONTH,YYYY’) => Twenty Fifth of
MARCH, 2008
There are many additional formats available for date conversion
NVL Function
Conditional Expression
They give the ability to perform IF-THEN-ELSE conditions in SQL
Two Options: CASE and DECODE
CASE and DECODE Examples
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
FROM employees;
This is Employees tables does not have any records with department_id
= 190. Employees table is deficient in values. Put an outer join (+
symbol) on the deficient column in the join
SELECT d.department_id, d.department_name, e.last_name, e.first_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
Self Joins
Joining a table to itself in order to retrieve data
Self Join
Create two different aliases to the self join table.
Group Functions
Group Functions work on set of rows to give one result per
group.
The set of rows may be entire table or table split into groups
(GROUP BY)
Common Group Functions
AVG: Calculate Average of n values excluding null value
MIN: Calculate Minimun of n values excluding null values
MAX: Calculate Maximum of n values excluding null values
SELECT AVG(salary), min(salary), max(salary)
FROM employees WHERE job_id like ‘%IT%’
COUNT: Returns the count of number of records
SELECT count(*) FROM employees WHERE department_id = 50
COUNT(EXPR) returns the count of non-NULL values of the expression
SELECT count(commission) FROM employees WHERE department_id = 80
Use the DISTINCT keyword to return count of distinct non-NULL values
SELECT count(distinct commission) FROM employees WHERE
department_id = 80
Use NVL to force the group functions to include NULL value columns
SELECT AVG(nvl(salary,0)), min(nvl(salary,0)), max(nvl(salary,0))
FROM employees WHERE job_id like ‘%IT%’
GROUP BY: Create groups of
data
Split the rows of the table into smaller groups with GROUP BY
SELECT department_id, avg(salary)
FROM employees
GROUP BY department_id
All columns in SELECT clause which are not in group function
should be in group by clause
The GROUP BY column does not need to be in SELECT clause
Can group records based on multiple columns
SELECT department_id, job_id, avg(salary)
FROM employees
GROUP BY department_id, job_id
GROUP BY restrictions
Cannot use WHERE clause to restrict groups. It can only restrict
the rows.
Cannot use GROUP functions in WHERE clause
Restrict groups using HAVING clause
Following statement gives an error “Group function is not
allowed here”
SELECT department_id, avg(salary) FROM employees
WHERE avg(salary) > 500
GROUP BY department_id
Following statement give “not a single-group function”
SELECT department_id, avg(salary) FROM employees
Restricting Group Results
Use HAVING clause to restrict or exclude group results.
Following is the sequence of operations.
1. Rows are divided into groups
2. Group functions are applied
3. The groups which satisfy the HAVING clause are selected.