You are on page 1of 26

RDBMS Using Oracle

Lecture Week 7
Introduction to Oracle 9i SQL
Last Lecture

kamran.munir@gmail.com

Joining Tables
Multiple Table Queries
Simple Joins
Complex Joins
Cartesian Joins
Outer Joins
Multi table Joins
Other Multiple Table Queries

The

tables being joined are listed in the


FROM clause.

The

join is performed in the WHERE


clause. Several operators can be used to
join tables, such as =, <, >, <>, <=, >=,
!=, BETWEEN, LIKE, and NOT; they can
all be used to join tables. However, the
most common operator is the equal
symbol.
kamran.munir@gmail.com

Simple Joins

This most common join is Simple Join that is


joining two tables with equality operator, it is an
equality join also known as equijoin.

EQUIJOIN, also referred to as an INNER JOIN.

The EQUIJOIN joins two tables with a common


column in which each is usually the primary key.

kamran.munir@gmail.com

The

syntax for an EQUIJOIN is

SELECT TABLE1.COLUMN1, TABLE2.COLUMN2...FROM TABLE1,


TABLE2 [, TABLE3 ] WHERE
TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME
[AND TABLE1.COLUMN_NAME = TABLE3.COLUMN_NAME ]

Example:
Select emp.empno, emp.ename, dept.dname
from emp,dept
Where emp.deptno = dept.deptno
kamran.munir@gmail.com

Another Example
Select emp.*, dept.dname, dept.loc
from emp, dept
Where emp.deptno = dept.deptno
Remember that the asterisk (*) represents all columns of a table.

Cross Join

Select * from emp, dept

(Provides All Combinations)

kamran.munir@gmail.com

kamran.munir@gmail.com

Complex Joins
Apart

from specifying joining condition, we


can also write some other condition to
limit rows selected.
Such joins are known as complex joins
Select emp.*, dept.dname, dept.loc
from emp, dept
Where emp.deptno = dept.deptno
and emp.job = MANAGER
kamran.munir@gmail.com

Joins of Non-Equality

NON-EQUIJOIN joins two or more tables based on


a specified column value not equaling a specified
column value in another table.
The syntax for the NON-EQUIJOIN is

FROM TABLE1, TABLE2 [, TABLE3 ] WHERE

TABLE1.COLUMN_NAME != TABLE2.COLUMN_NAME
[ AND TABLE1.COLUMN_NAME !=

TABLE2.COLUMN_NAME ]

kamran.munir@gmail.com

Non-Equi Join Row count

Suppose only 6 rows exist in each table. For every


record in table A, there is a corresponding record
in table B. Because non-equality is to be tested in
the join of the two tables, each row in the first
table is paired with all rows from the second
table, except for its own corresponding row. This
means that each of the 6 rows are paired with 5
unrelated rows in the second table; 6 rows
multiplied by 5 rows equals 30 rows total.
kamran.munir@gmail.com

Natural Join

NATURAL JOIN is nearly the same as the EQUIJOIN;


EQUIJOIN;
however, the NATURAL JOIN differs from the EQUIJOIN
by eliminating duplicate columns in the joining columns.
There is also no need to specify join condition, This join
is based on all columns with the same name and
datatype in both tables.

Select empno,
empno, ename,
ename, dname from
emp NATURAL JOIN dept;

Select * from emp NATURAL JOIN dept;


kamran.munir@gmail.com

kamran.munir@gmail.com

kamran.munir@gmail.com

Joins - Explanation

When you join two or more tables, a good idea is to


precede the field names with the table names. This is not
mandatory unless the same field name is found in more
than one table.

If you precede the field name with a table name, place a


period between the two names. For example,
tablename.fieldname.
tablename.fieldname.

You must specify which fields are being joined.

If you do not specify which fields are being joined, the


result is what is commonly referred to as a "Cartesian
join" in which all rows in the first table are joined with all
rows in the second table.
kamran.munir@gmail.com

Outer Joins
An

OUTER JOIN is used to return all rows that exist in one


table, even though corresponding rows do not exist in the
joined table.

The

(+) symbol is used to denote an OUTER JOIN in a


query. The (+) is placed at the end of the table name in the
WHERE clause.

The

table with the (+) should be the table that does not
have matching rows.

In

many implementations, the OUTER JOIN is broken down


into joins called LEFT OUTER JOIN, RIGHT OUTER JOIN,
and FULL OUTER JOIN.

Outer Join
The Oracle syntax is
FROM TABLE1, TABLE2 [, TABLE3 ]
WHERE TABLE1.COLUMN_NAME[(+)] = TABLE2.COLUMN_NAME[(+)]
[ AND TABLE1.COLUMN_NAME[(+)] = TABLE3.COLUMN_NAME[(+)]]

To write a query that performs an outer join of tables


A and B and returns all rows form A, apply the outerjoin operator (+) to all columns of B in the join
condition. For all rows in A that have no matching
rows in B, the query returns NULL values for the
columns in B.
kamran.munir@gmail.com

Outer Join
The next example accomplishes the desired output
through the use of an OUTER JOIN. Oracle's syntax is
used for the OUTER JOIN (right).
SELECT P.PROD_DESC,
P.PROD_DESC, O.QTY FROM
PRODUCTS_TBL P, ORDERS_TBL O
WHERE P.PROD_ID = O.PROD_ID(+);
O.PROD_ID(+);
It will display all records of Products table and only
matching records from Orders Table.

kamran.munir@gmail.com

Right Outer Join


SELECT P.PROD_DESC,
P.PROD_DESC, O.QTY FROM
PRODUCTS_TBL P, ORDERS_TBL O
WHERE P.PROD_ID (+) = O.PROD_ID;
O.PROD_ID;

Note:
IN KEYWORD
RIGHT OUTER JOIN
AND
LEFT OUTER JOIN
The word OUTER
Is Optional.

SELECT P.PROD_DESC,
P.PROD_DESC, O.QTY FROM
PRODUCTS_TBL P RIGHT OUTER JOIN ORDERS_TBL O
on P.PROD_ID = O.PROD_ID;
O.PROD_ID;

SELECT P.PROD_DESC,
P.PROD_DESC, O.QTY FROM
PRODUCTS_TBL P RIGHT join ORDERS_TBL O
USING (PROD_ID
); kamran.munir@gmail.com
(PROD_ID);

Left Outer Join


SELECT P.PROD_DESC,
P.PROD_DESC, O.QTY FROM
PRODUCTS_TBL P, ORDERS_TBL O
WHERE P.PROD_ID = O.PROD_ID(+);
O.PROD_ID(+);
SELECT P.PROD_DESC,
P.PROD_DESC, O.QTY FROM
PRODUCTS_TBL P LEFT OUTER JOIN ORDERS_TBL O
on P.PROD_ID = O.PROD_ID;
O.PROD_ID;

SELECT P.PROD_DESC,
P.PROD_DESC, O.QTY FROM
PRODUCTS_TBL P Left join ORDERS_TBL O
USING (PROD_ID
); kamran.munir@gmail.com
(PROD_ID);

kamran.munir@gmail.com

kamran.munir@gmail.com

kamran.munir@gmail.com

kamran.munir@gmail.com

kamran.munir@gmail.com

Self Join

Self Joins
The SELF JOIN is used to join a table to itself,
as if the table were two tables, temporarily
renaming at least one table in the SQL
statement. The syntax is as follows.

The following is an example:


SELECT A.LAST_NAME, B.LAST_NAME,
A.FIRST_NAME
FROM EMPLOYEE_TBL A, EMPLOYEE_TBL B
WHERE A.LAST_NAME = B.LAST_NAME;

kamran.munir@gmail.com

Multi table Joins


Joining

on Multiple Keys
We may have a table that has a primary
key that is comprised of more than one
column. You may also have a foreign key
in a table that consists of more than one
column, which references the multiple
column primary key.

kamran.munir@gmail.com

Consider the following Oracle tables that are


used here for examples only:
SQL> desc prod
Name
------------ -------SERIAL_NUMBER
VENDOR_NUMBER
PRODUCT_NAME
COST

Null?
--------------NOT NULL
NOT NULL
NOT NULL
NOT NULL

SQL> desc ord


Name
Null?
---------------ORD_NO
NOT NULL
PROD_NUMBER
NOT NULL
VENDOR_NUMBER NOT NULL
QUANTITY
NOT NULL
ORD_DATE
NOT NULL

Type
-------NUMBER(10)
NUMBER(10)
VARCHAR2(30)
NUMBER(8,2)
Type
---------------------------NUMBER(10)
NUMBER(10)
NUMBER(10)
NUMBER(5)
DATE

kamran.munir@gmail.com

The primary key in PROD is the combination of the


columns SERIAL_NUMBER and VENDOR_NUMBER.
VENDOR_NUMBER.

The foreign key in ORD is also the combination of the


columns SERIAL_NUMBER and VENDOR_NUMBER.
VENDOR_NUMBER.

When selecting data from both tables (PROD and ORD),


ORD),
the join operation may appear as follows:
SELECT P.PRODUCT_NAME, O.ORD_DATE,
O.QUANTITYFROM PROD P, ORD O
WHERE P.SERIAL_NUMBER = O.SERIAL_NUMBER
AND
P.VENDOR_NUMBER = O.VENDOR_NUMBER;
kamran.munir@gmail.com

Creating VIEWS
Improve Security Through Views

A view
is often referred to as a virtual table
allows a user to see a customized selection of one or
more tables
is stored as an SQL query, which is executed
whenever the view is used
reflects the current state of the database
can be treated as another table (with special
restrictions on modifying the data within the view)
kamran.munir@gmail.com

A view
is created using the create view command
is displayed using a normal select command
can be referred to by a select, insert, update
or delete command
is dropped using the drop view command

kamran.munir@gmail.com

view name
create view dept30_emps
as
(select empno, ename, job, mgr
from emp
where deptno = 30);

select * from dept30_emps;


kamran.munir@gmail.com

query

emp table

dept30_emps view

kamran.munir@gmail.com

view name
create view annual_costs
as
(select empno, ename, comm,
sal*12

annual_sal

from emp);

query

kamran.munir@gmail.com

emp table

annual_costs view
kamran.munir@gmail.com

All columns produced using expressions such


as sal*12 must be given aliases:
create view annual_costs as (select empno,
ename, comm,
sal*12 annual_sal
from emp);

kamran.munir@gmail.com

Any other columns may also be given aliases:


create view annual_costs as
(select
empno, ename name, comm commission,
sal*12 annual_sal
from emp);

kamran.munir@gmail.com

kamran.munir@gmail.com

10

All the columns may be given aliases as


follows:
create view annual_costs (employee, name,
commission, annual_sal)
as
(select empno, ename, comm,
sal*12
from emp);

kamran.munir@gmail.com

kamran.munir@gmail.com

11

12

The order by clause may not be used in


creating a view as the order of rows of a table
or view is not defined.
After the view is created, a query can be
written to display the view in the required
order:
select * from annual_costs
commission, annual_sal;

kamran.munir@gmail.com

kamran.munir@gmail.com

order by

13

14

Views are often used to collect summary


data, e.g.
create view dept_size (department,
no_of_emps) as
(select dname, count(empno) from dept, emp
Where
dept.deptno = emp.deptno (+)
group by dname);
kamran.munir@gmail.com

kamran.munir@gmail.com

15

16

A view
provides an additional level of security
different groups of users have different
database privileges
a view can be used to control the
information the user has access to

kamran.munir@gmail.com

21

A view
can be used to convert units
e.g. to see salary in terms of rather than $,
a view can be used to convert the values

kamran.munir@gmail.com

24

A view
can be treated as a table in its own right
can be used in a query
can be joined to another table or view

kamran.munir@gmail.com

27

Modifying data in a view:


you cannot use delete on multiple-table views
you cannot use insert unless all the non-null
columns in the underlying table are included in the
view
records updated through a multiple-table view must
belong to the same underlying table
records cannot be inserted or updated through a
view which was defined using distinct
new columns (e.g. ansal) cannot be updated
kamran.munir@gmail.com

29

The END
Introduction to SQL

First 1OHT Course Review

Chapter 1
SQL Fundamentals

Chapter 2
SQL Plus Overview

Chapter 3
Single row functions

Chapter 4
Aggregating data and single row functions

Chapter 5
Joins and sub queries
kamran.munir@gmail.com

Chapter 6
Modifying Data (Insert, update, delete)

Chapter 7
Managing tables (alter table, constraints)

Chapter 8
Working with views

Chapter 10
Creating users, granting access to users etc

BOOK Introduction to Oracle 9i SQL


Book topics covered approx 75%
After completing 100% you are ready for OCP first paper
kamran.munir@gmail.com