ADBMS Lab Manual
ADBMS Lab Manual
ADBMS Lab Manual
2MCS5
List of Experiments
storage media and, in a multi-user system, who else may also be accessing the data. In handling user requests, the
DBMS ensures the integrity of the data (that is, making sure it continues to be accessible and is consistently
organized as intended) and security (making sure only those with access privileges can access the data). The most
typical DBMS is a relational database management system (RDBMS). A standard user and program interface is the
Structured Query Language (SQL). A newer kind of DBMS is the object-oriented database management system
(ODBMS).
A DBMS can be thought of as a file manager that manages data in databases rather than files in file systems. In IBM's
mainframe operating systems, the nonrelational data managers were (and are, because these legacy application
systems are still used) known as access methods.
A DBMS is usually an inherent part of a database product. On PCs, Microsoft Access is a popular example of a
single- or small-group user DBMS. Microsoft's SQL Server is an example of a DBMS that serves database requests
from multiple (client) users. Other popular DBMSs (these are all RDBMSs, by the way) are IBM's DB2, Oracle's line
of database management products, and Sybase's products.
IBM's Information Management System (IMS) was one of the first DBMSs. A DBMS may be used by or combined
with transaction managers, such as IBM's Customer Information Control System (CICS).
Use of Database in Real Life
Real-Life Database Examples
To say that the databases are everywhere would be an understatement. They virtually permeate our lives: Online
stores, health care providers, clubs, libraries, video stores, beauty salons, travel agencies, phone companies,
government agencies like FBI, INS, IRS, and NASA they all use databases. These databases can be very
different in their nature and usually have to be specifically designed to cater to some special customer needs.
Here are some examples.
Note
All relational databases can be divided into two main categories according to their primary
function online transaction processing (OLTP) and data warehouse systems. OLTP typically
has many users simultaneously creating and updating individual records; in other words it's
volatile and computation-intensive. Data warehouse is a database designed for information
processing and analysis, with focus on planning for the future rather than on day-to-day
operations. The information in these is not going to change very often, which ensures the
information consistency (repeatable result) for the users. In the real world most systems are
hybrids of these two, unless specifically designed as data warehouse.
Order management system database
A typical database for a company that sells building materials might be arranged as follows: The company must
have at least one customer. Each customer in the database is assigned one or more addresses, one or more contact
phones, and a default salesperson who is the liaison between the customer and the company. The company sells a
variety of products. Each product has a price, a description, and some other characteristics. Orders can be placed
for one or more product at a time. Each product logically forms an order line. When an order is complete it can
be shipped and then invoiced. Invoice number and shipment number are populated automatically in the database
and can not be changed by users. Each order has a status assigned to it: COMPLETE, SHIPPED, INVOICED,
and so on. The database also contains specific shipment information (bill of lading number, number of boxes
shipped, dates, and so on). Usually one shipment contains one order, but the database is designed in such a way
that one order can be distributed between more than one shipment, as well as one shipment can contain more
than one order. Some constraints also exist in the database. For example, some fields cannot be empty, and some
other fields can contain only certain types of information.
You already know that a database is a multi user environment by definition. It's a common practice to group
users according to the functions they perform and security levels they are entitled to. The order management
system described here could have three different user groups: Sales department clerks' function is to enter or
modify order and customer information; shipping department employees create and update shipment data;
warehouse supervisors handle products. In addition, all three user groups view diverse database information
under different angles, using reports and ad-hoc queries.
We'll use this database, which we'll call ACME, throughout this book for examples and exercises. ACME
database is a simplified version of a real production database. It has only 13 tables, and the real one would easily
have over a hundred.
Health care provider database
A health provider company has multiple offices in many different states. Many doctors work for the company,
and each doctor takes care of multiple patients. Some doctors just work in one office, and others work in
different offices on different days. The database keeps information about each doctor, such as name, address,
contact phones, area of specialization, and so on. Each patient can be assigned to one or more doctors. Specific
patient information is also kept in the database (name, address, phones, health record number, date of birth,
history of appointments, prescriptions, blood tests, diagnoses, etc.). Customers can schedule and cancel
appointments and order prescription drugs either over the phone or using the company Web site. Some
restrictions apply for example, to see a specialist, the patient needs an approval from his/her primary
physician; to order a prescription the patient should have at least one valid refill left, and so on.
Now, what are the main database user groups? Patients should be able to access the database using a Web
browser to order prescriptions and make appointments. This is all that patients may do in the database. Doctors
and nurses can browse information about their patients, write and renew prescriptions, schedule blood tests and
X-Rays, and so on. Administrative staff (receptionists, pharmacy assistants) can schedule appointments for
patients, fill prescriptions, and run specific reports.
Again, in real life this database would be far more complicated and would have many more business rules, but
our main goal now is just to give a general idea what kind of information a database could contain.
The health provider and order management system databases are both examples of a typical hybrid database
(though the former is probably closer to an OLTP).
Scientific database
A database for genome research and related research areas in molecular and cellular biology can be a good
example of a scientific database. It contains gene catalogs for completely sequenced genomes and some partial
genomes, genome maps and organism information, and data about sequence similarities among all known genes
in all organisms in the database. It also contains information on molecular interaction networks in the cell and
chemical compounds and reactions.
A database of an antique automobile club can be pretty simple. Also, such an organization would not typically
have too many members, so the database is not going to be very large. You need to store members' personal
information such as address, phone number, area of interest, and so on. The database might also contain the
information about the autos (brand, year, color, condition, etc.). Autos are tied to their owners (members of the
club). Each member can have one or more vehicles, and a vehicle can be owned by just one member.
The database would only have a few users possibly, the chairman of the club, an assistant, and a secretary.
The last two examples are not business-critical databases and don't have to be implemented on expensive
enterprise software. The data still have to be kept safely and should not be lost, but in case of, let's say, hardware
failure it probably can wait a day or two before the database is restored from a backup. So, the use of a free
database, like mySQL, PostgreSQL, or even nonrelational Posgres is appropriate. Another good choice might be
MS Access, which is a part of Microsoft Office Tools; if you bought MS Office just because you want to use
Word and Excel, you should be aware that you've got a free relational database as well. (MS Access works well
with up to 15 users.)
Exp 1: Overview of SQL DDL, DML and DCL Commands With Examples.
DDL is Data Definition Language statements. Some examples:
CREATE - to create objects in the database
ALTER - alters the structure of the database
DROP - delete objects from the database
TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
COMMENT - add comments to the data dictionary
GRANT - gives user's access privileges to database
REVOKE - withdraw access privileges given with the GRANT command
DML is Data Manipulation Language statements. Some examples:
SELECT - retrieve data from the a database
INSERT - insert data into a table
UPDATE - updates existing data within a table
DELETE - deletes all records from a table, the space for the records remain
CALL - call a PL/SQL or Java subprogram
EXPLAIN PLAN - explain access path to data
LOCK TABLE - control concurrency
DCL is Data Control Language statements. Some examples:
COMMIT - save work done
SAVEPOINT - identify a point in a transaction to which you can later roll back
ROLLBACK - restore database to original since the last COMMIT
SET TRANSACTION - Change transaction options like what rollback segment to use
Basic SQL DDL Commands.
1. SQL - CREATE TABLE
Syntax: CREATE TABLE tablename (column_name data_ type constraints, )
Example:
INPUT:
SQL> CREATE TABLE Emp ( EmpNo short CONSTRAINT PKey PRIMARY KEY,
EName VarChar(15), Job Char(10) CONSTRAINT Unik1 UNIQUE,
Mgr short CONSTRAINT FKey1 REFERENCES EMP (EmpNo),
Hiredate Date, DeptNo short CONSTRAINT FKey2 REFERENCES DEPT(DeptNo));
RESULT: Table created.
SQL>Create table prog20 (pname varchar2(20) not null), doj date not null,dob date not null,
sex varchar(1) not null, prof1 varchar(20),prof2 varchar(20),salary number(7,2) not null);
RESULT:
Table created.
SQL>desc prog20;
Name Null? Type
--------------------------------- -------- ---------------------------PNAME NOT NULL VARCHAR2(20)
DOJ NOT NULL DATE
DOB NOT NULL DATE
SEX NOT NULL VARCHAR2(1)
PROF1 VARCHAR2(20)
PROF2 VARCHAR2(20)SALARY NOT NULL NUMBER(7,2)
Examples:
DELETE FROM SP WHERE PNO= P1
DELETE FROM SP
INPUT:
SQL>Delete from emp where empno=7369;
RESULT: 1 row deleted.
Basic SQL DCL Commands.
1. COMMIT
Save changes (transactional).
Syntax:
COMMIT [WORK] [COMMENT 'comment_text']
COMMIT [WORK] [FORCE 'force_text' [,int] ]
FORCE - will manually commit an in-doubt distributed transaction
force_text - transaction identifier (see the DBA_2PC_PENDING view)
int - sets a specific SCN.
If a network or machine failure prevents a distributed transaction from committing
properly, Oracle will store any commit comment in the data dictionary along with the
transaction ID.
INPUT:
SQL>commit;
RESULT: Commit complete.
2. ROLLBACK
Undo work done (transactional).
Syntax:
ROLLBACK [WORK] [TO [SAVEPOINT]'savepoint_text_identifier'];
ROLLBACK [WORK] [FORCE 'force_text'];
FORCE - will manually rollback an in-doubt distributed transaction
INPUT:
SQL>rollback;
RESULT:Rollback complete.
3. SAVEPOINT
Save changes to a point (transactional).
Syntax:
SAVEPOINT text_identifier
Example:
UPDATE employees
SET salary = 95000
WHERE last_name = 'Smith';
SAVEPOINT justsmith;
UPDATE employees
SET salary = 1000000;
SAVEPOINT everyone;
SELECT SUM(salary) FROM employees;
ROLLBACK TO SAVEPOINT justsmith;
COMMIT;
5. List all employee names and their and their manager whose manager is
7902 or 7566 0r 7789.
INPUT SQL>select ename from emp where mgr in(7602,7566,7789);
RESULT
ENAME
------SCOTT
FORD
6. List all employees which starts with either J or T.
INPUT SQL>select ename from emp where ename like J% or ename like T%;
RESULT:
ENAME
--------JONES
TURNER
JAMES
7. List all employee names and jobs, whose job title includes M or P.
INPUT SQL>select ename,job from emp where job like M% or job like P%;
RESULT:
ENAME JOB
---------- --------JONES MANAGER
BLAKE MANAGER
CLARK MANAGER
KING PRESIDENT
8. List all jobs available in employee table.
INPUT SQL>select distinct job from emp;
RESULT:
JOB
--------ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN
assistant
clerk
7 rows selected.
9. List all employees who belongs to the department 10 or 20.
INPUT SQL>select ename from emp where deptno in (10,20);
RESULT:
ENAME
---------SMITH
JONES
CLARK
SCOTT
KING
ADAMS
FORD
MILLER
8 rows selected.
10. List all employee names , salary and 15% rise in salary.
INPUT SQL>select ename , sal , sal+0.15* sal from emp;
RESULT:
ENAME SAL SAL+0.15*SAL
---------- ---------- -----------SMITH 800 920
ALLEN 1600 1840
WARD 1250 1437.5
JONES 2975 3421.25
MARTIN 1250 1437.5
BLAKE 2850 3277.5
CLARK 2450 2817.5
7 rows selected.
11. List minimum , maximum , average salaries of employee.
INPUT SQL>select min(sal),max(sal),avg(sal) from emp;
RESULT:
MIN(SAL) MAX(SAL) AVG(SAL)
--------- ---------- ---------3 5000 1936.94118
12. Find how many job titles are available in employee table.
INPUT SQL>select count (distinct job) from emp;
RESULT:
COUNT(DISTINCTJOB)
-----------------7
13. What is the difference between maximum and minimum salaries of
employees in the organization?
INPUT SQL>select max(sal)-min(sal) from emp;
RESULT:
MAX(SAL)-MIN(SAL)-----------------4997
14. Display all employee names and salary whose salary is greater than
minimum salary of the company and job title starts with M.
INPUT SQL>select ename,sal from emp where job like M% and sal > (select min (sal)
from emp);
RESULT
ENAME SAL
---------- ---------JONES 2975
BLAKE 2850
CLARK 2450
15. Find how much amount the company is spending towards salaries.
INPUT SQL>select sum (sal) from emp;
RESULT
SUM(SAL)
--------32928
Exp 4. To write queries using Set operations and to write nested queries.
Set Operations:
UNION - OR
INTERSECT - AND
EXCEPT - - NOT
NESTED QUERY:- A nested query makes use of another sub-query to compute or retrieve
the information.
1. Find the name of the institute in which the person studied and
developed the costliest package.
INPUT SQL>select splace, pname from study where pname = (select pname from software
where scost = (select max (scost) from software);
RESULT
SPLACE PNAME
------------ ------------SAHBHARI MARY
2. Find the salary and institute of a person who developed the highest
selling package.
INPUT SQL> select study.pname, sal, splace from study, programmer where study.pname =
programmer.pname and study.pname = (select pname from software where scost = (select
max (scost) from software));
RESULT
PNAME SAL SPLACE
----------- ------ ----------MARY 4500 SABHARI
3. How many packages were developed by the person who developed the
cheapest package.
INPUT SQL>select pname, count (title) from software where dcost = (select min(dcost)
from software) group by pname;
RESULT
PNAME COUNT(TITLE)
------------- ---------------------VIJAY 1
4. Calculate the amount to be recovered for those packages whose
development cost has not yet recovered.
INPUT SQL>select title , (dcost-scost) from software where dcost > scost;
5. Display the title, scost, dcost, difference of scost and dcost in the
descending order of difference.
INPUT SQL> select title, scost, dcost, (scost - dcost) from software descending order by
(scost-dcost);
6. Display the details of those who draw the same salary.
INPUT SQL> select p.pname, p.sal from programmer p, programmer t where p.pname <>
t.pname and p.sal = t.sal;(or)
INPUT SQL>select pname,sal from programmer t where pname<>t.pname and sal= t.sal;
Exp 5. To write queries using single row functions and group functions.
1. Display the names and dob of all programmers who were born in
january.
INPUT SQL>select pname, dob from programmer where to_char (dob,MON)=JAN;
2. Calculate the experience in years of each programmer and display along
with programmer name in descending order.
INPUT SQL> select pname, round (months_between(sysdate, doj)/12, 2) "EXPERIENCE"
from programmer order by months_between (sysdate, doj) desc;
3. List out the programmer names who will celebrate their birthdays
during current month.
INPUT SQL>select pname from programmer where to_char(dob,MON) like to_char
(sysdate, MON);
4. Display the least experienced programmers details.
INPUT SQL>select * from programmer where doj = (select max (doj) from programmer);
5. Who is the most experienced programmer knowing pascal.
INPUT SQL>select pname from programmer where doj = (select min (doj) from
programmer);
6. Who is the youngest programmer born in 1965.
INPUT SQL> select pname , dob from programmer where dob = (select max (dob) from
programmer where to_char (dob,'yy') = 65);
7. In which year, most of the programmers are born.
INPUT SQL>select to_char (dob , YY) from programmer group by to_char (dob, YY)
having count(*) = (select max (count(*)) from programmer group by to_char(dob,YY);
8. In which month most number of programmers are joined.
INPUT SQL>select to_char (doj,YY) from programmer group by to_char (doj,YY)
having count (*) = (select max (count(*)) from programmer group by to_char (doj,YY);
9. What is the length of the shortest name in programmer table ?
INPUT SQL>select length (pname) from programmer where length (pname) = select min
( length (pname) from programmer);
10. Display the names of the programmers whose name contains up to 5
characters.
INPUT SQL>select pname from programmer where length (pname) <=5;
11. Display all packages names in small letters and corresponding
programmer names in uppercase letters.
INPUT SQL>select lower (title), upper (pname) from software;
Cust_id
NOT NULL
NUMBER(4)
Item_name
VARCHAR2(15)
PRICE
NUMBER(6,2)
SQL>insert into item values(&item_id,&item_name,&price);
SQL> select * from item;
Output:
ITEM_ID
ITEM_NAME PRICE
..
2334
geera
6.25
4532
corn soup
34.65
2124
lays chips
20
4531
setwet
99.99
2319
duracell
45.5
SQL>create table sale(bill_no number(5) primary key,bill_date date, cust_id number(5) references
customer(cust_id), item_id number(4) references item(item_id),qty_sold number(4));
Out put: Table Created.
SQL>dsec sale
Output:
Name
Null?
Type
..
BILL_NO
NOT NULL
NUMBER(4)
BILL_DATE
DATE
CUST_ID
NUMBER(5)
ITEM_ID
NUMBER(4)
QTY_SOLD
NUMBER(4)
SQL>insert into Sale values(&bill_no, &bill_date,
&cust_id, &item_id, &qty_sold);
SQL>select * from sale;
Output:
BILL_NO
BILL_DATE CUST_ID
ITEM_ID
QTY_SOLD
...
1450
04-JAN-06
100
2124
2
1451
04-JAN-06
101
2319
1
1452
04-JAN-06
103
4531
2
1453
04-JAN-06
102
2334
3
1454
04-JAN-06
104
4532
3
c) List all the bills for the current date with the customer names and item numbers
SQL> select c.custname, i.itemid, s.billno from customer c, item I, sale s
where c.custid=s.custid and
s.billdate=to_char(sysdate);
CUSTNAME
------------John
ITEMID
--------5001
BILLNO
--------332
d) List the total Bill details with the quantity sold, price of the item and the final amount
234
SQL> select i.price, s.qty,(i.price*s.qty) total from item I, sale s where i.itemid=s.itemid;
PRICE QTY
------- ----120
20
5
10
350
2
3
2
1
4
TOTAL
-------240
60
10
10
1400
e) List the details of the customer who have bought a product which has a price>200
SQL> select c.custid, c.custname from customer c, sale s, item i where i.price>200 and
c.custid=s.custid and i.itemid=s.itemid;
CUSTID
--------f)
CUSTNAME
-------------duffy
Give a count of how many products have been bought by each customer
SQL> select custid, count(itemid) from sale group by custid;
CUSTID
---------1
3
4
5
COUNT(ITEMID)
--------------------2
1
1
1
ITEMNAME
------------pencil
i)
Create a view which lists out the bill_no, bill_date, cust_id, item_id, price,
qty_sold, amount
SQL>create view cust as (select s.billno, s.billdate, c.custid, i. iitemid, i.price, s.qty from customer c,sale s item
I where c.custid=s.custid and i.iemid=s.itemid);
view created.
SQL>select * from cust;
BILLNO BILLDATE
CUSTID ITEMID PRICE QTY
3432
12-JAN-06
3
3244
120
2
4424
20-FEB-06
1
3456
20
3
332
13-MAR-06
1
1234
5
2
2343
10-MAR
5
5001
10
1
1331
11-MAR-06
4
76776 350
4
HARISH
BALAJI
RAKESH
PAVAN
JOYCE
ISS_NO
NOT NULL
NUMBER
ISS_DATE
DATE
MEM_NO
NUMBER(5)
BOOK_NO
NUMBER(5)
SQL>select * from iss_rec;
ISS_NO
ISS_DATE
MEM_NO
BOOK_NO
43
05-JAN-06
5443
4523
81
28-DEC-05
5441
8723
22
08-DEC-05
5440
7821
53
07-JAN-06
5442
9123
35
06-JAN-06
5444
2342
c) List all the student names with their membership numbers
SQL> select s.studname, m.memno from student s, membership m where m.studno=s.studno;
STUDNAME
------------abhijeet
arun
arvind
ashish
ashwin
MEMNO
-------1001
1002
1003
1004
1005
d) List all the issues for the current date with student and Book names
SQL> select i.issno, s.studname, b.bookname from iss_rec I, membership m, student s, book b
2 where i.memno=m.memno and m.studno=s.studno and
i.issdate=to_char(sysdate);
ISSNO STUDNAME BOOKNAME
------- -------------------------13
arvind
P&S
e) List the details of students who borrowed book whose author is CJDATE
SQL> select * from student where studno in(select studno from membership where memno in
2 (select memno from iss_rec where bookno in(select bookno from book where author=CJDATE)));
STUDNO
----------
05
STUDNAME
------------ashwin
f) Give a count of how many books have been bought by each student
SQL> select s.studno, count(i.bookno) from student s.membership m, book b, 2 iss_rec I where
s.studno=m.studno and b.bookno=i.bookno group by s.studno;
STUDNO
---------501
502
503
504
505
COUNT(I.BOOKNO)
----------------------5
5
5
5
5
Create a view which lists out the iss_no, iss _date, stud_name, book name
j) Create a view which lists the daily issues-date wise for the last one week
920
50
981
51
897
52
820
53
928
54
SQL>create table cassette(cass_no number(5) primary key,
Cass_name varchar2(15),language varchar2(15));
SQL>desc cassette;
Name
Null?
Type
..
CASS_NO
NOT NULL
NUMBER(5)
CASS_NAME
VARCHAR2(15)
LANGUAGE
VARCHAR2(15)
SQL>insert into cassette values(&cass_no,&cass_name,&language);
SQL>select * from cassette;
CASS_NO
CASS_NAME
LANGUAGE
1
tagore
telugu
2
the lion king
English
3
anniyan
tamil
4
indra
telugu
5
lord of rings
English
SQL>create table issu_rec(iss_no number(5) primary key,iss_date date,mem_no number(5)references
memship(mem_no),cass_no number(5) references cassette(cass_no));
SQL>desc issu_rec;
Name
Null?
Type
...
ISS_NO
NOT NULL
NUMBER(5)
ISS_DATE
DATE
MEM_NO
NUMBER(5)
CASS_NO
NUMBER(5)
SQL>select * from issu_rec;
ISS_NO
ISS_DATE
MEM_NO CASS_NO
22
07-JAN-06
920
1
23
10-JAN-00
981
2
26
10-JAN-06
897
5
3
01-JAN-06
820
4
34
31-DEC-05
928
3
c) List all the customer names with their membership numbers
SQL>select c.custname,m.memno from customer1 c,membership1 m where c.custno=m.custno;
CUSTNAME MEMNO
.. ..
NIKHIL
51
VIVEK
52
SHRAVAN
58
VAMSI
57
SHIVA
56
d) List all the issues for the current date with the customer names and cassette names
SQL>select i.issno,c.custname,cc.cassettename from customer1 c,membership1 m,cassette cc,issrec1 I where
i.issdate=to_char(sysdate) and c.custno=m.custno and i.cassno=cc.cassno and i.memno=m.memno;
Correlated subqueries may appear elsewhere besides the WHERE clause; for example, this query uses a correlated
subquery in the SELECT clause to print the entire list of employees alongside the average salary for each employee's
department. Again, because the subquery is correlated with a column of the outer query, it must be re-executed for each
row of the result.
SELECT
employee_number,
name,
(SELECT AVG(salary)
FROM employees
WHERE department = Bob.department) AS department_average
FROM employees AS Bob;
If a sql join condition is omitted or if it is invalid the join operation will result in a Cartesian product. The
Cartesian product returns a number of rows equal to the product of all rows in all the tables being joined. For
example, if the first table has 20 rows and the second table has 10 rows, the result will be 20 * 10, or 200
rows. This query takes a long time to execute.
database table "product";
product_id
100
101
102
103
104
product_name
Camera
Television
Refrigerator
Ipod
Mobile
supplier_name
Nikon
Onida
Vediocon
Apple
Nokia
unit_price
300
100
150
75
50
produ
ct_id
total_
units
5100
104
30
5101
102
5102
103
25
5103
101
10
custo
mer
Infos
ys
Satya
m
Wipr
o
TCS
SQL Joins can be classified into Equi join and Non Equi join.
1) SQL Equi joins
It is a simple sql join condition which uses the equal sign as the comparison operator. Two types of equi joins
are SQL Outer join and SQL Inner join.
An equi-join is further classified into two categories:
a) SQL Inner Join
b) SQL Outer Join
a) SQL Inner Join:
All the rows returned by the sql query satisfy the sql join condition specified.
For example: If you want to display the product information for each order the query will be as given below.
Since you are retrieving the data from two tables, you need to identify the common column between these two
tables, which is theproduct_id.
The query for this type of sql joins would be like,
SELECT order_id, product_name, unit_price, supplier_name, total_units
FROM product, order_items
WHERE order_items.product_id = product.product_id;
The columns must be referenced by the table name in the join condition, because product_id is a column in
both the tables and needs a way to be identified. This avoids ambiguity in using the columns in the SQL
SELECT statement.
The number of join conditions is (n-1), if there are more than two tables joined in a query where 'n' is the
number of tables involved. The rule must be true to avoid Cartesian product.
We can also use aliases to reference the column name, then the above query would be like,
p
r
o
d
u
c
t
_
n
a
m
e
C
a
m
e
r
a
T
e
o
r
d
e
r
_
i
d
t
o
t
a
l
_
u
n
i
t
s
5
1
1
0
l
e
v
i
0
1
s
3
i
o
n
R
e
f
r
i
5
1
g
1
0
5
e
0
2
r
1
a
t
o
r
I
5
1
p
1
2
0
o
0
5
3
d
2
M
o
5
1
b
1
3
0
i
0
0
4
l
0
e
NOTE:If the (+) operator is used in the left side of the join condition it is equivalent to left outer join. If used
on the right side of the join condition it is equivalent to right outer join.
SQL Self Join:
A Self Join is a type of sql join which is used to join a table to itself, particularly when the table has a
FOREIGN KEY that references its own PRIMARY KEY. It is necessary to ensure that the join statement
defines an alias for both copies of the table to avoid column ambiguity.
The below query is an example of a self join,
SELECT a.sales_person_id, a.name, a.manager_id, b.sales_person_id, b.name
FROM sales_person a, sales_person b
WHERE a.manager_id = b.sales_person_id;
la
st
_
n
a
m
e
------B
ha
g
w
at
G
o
w
da
S
ha
r
m
a
Fl
e
m
in
g
su
bj
ec
t
--------M
at
hs
M
at
hs
Sc
ie
nc
e
Sc
ie
nc
e