Sunteți pe pagina 1din 64

LAB CYCLE I

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


1-

CREATION OF THE TABLE

TABLE NAME: CLIENT_MASTER


SQL>CREATE TABLE CLIENT_MASTER (
2 CLIENT _NO

VARCHAR2 (6)

PRIMARY KEY,

3 NAME

VARCHAR2 (20)

NOT NULL,

4 ADDRESS1

VARCHAR2 (20),

5 ADDRESS2

VARCHAR2 (20),

6 CITY

VARCHAR2 (10),

7 STATE

VARCHAR2 (10),

8 PINCODE

NUMBER (6)

9 BAL_DUE

NUMBER (10,2),

NOT NULL,

10 CHECK (CLIENT_NO LIKE C% )


11
);
Table created.
SQL> DESC CLIENT_MASTER;
Name

Null?

Type

------------------------------- -------- ---CLIENT_NO

NOT NULL

VARCHAR2 (6)

NAME

NOT NULL

VARCHAR2 (20)

ADDRESS1

VARCHAR2 (20)

ADDRESS2

VARCHAR2

CITY

VARCHAR2 (10)

STATE

VARCHAR2

PINCODE
BAL_DUE

NOT NULL

(2)

(10)

NUMBER (6)
NUMBER(10,2)

TABLE NAME: PRODUCT_MASTER


SQL> CREATE TABLE PRODUCT_MASTER (
2

PRODUCT_NO

VATCHAR2 (6)

PRIMARY KEY,

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


2-

DESCRIPTION

VARCHAR2(20) NOTNULL,

PROFIT_PERCENT

NUMBER (4,2) NOT NULL,

UNIT_MEASURE

VARCHAR2 (10),

QTY_ON_HAND

NUMBER (8),

RECORDER_LVL

NUMBER(8),

SELL_PRICE

NUMBER(8,2)NOTNULL CHECK(SELL_PRICE<>0),

COST_PRICE

NUMBER(8,2)NOTNULL CHECK(COST_PRICE<>0),

10

CHECK(PRODUCT_NO LIKE'P%')

11);
Table created.
SQL>DESC PRODUCT_MASTER:
Name

Null?

Type

------------------------------- -------- ---------------------------PRODUCT_NO

NOT NULL

VARCHAR2(6)

DESCRIPTION

NOT NULL

VARCHAR2(20)

PROFIT_PERCENT

NOT NULL

NUMBER(4,2)

UNIT_MEASURE

VARCHAR2(10)

QTY_ON_HAND

NUMBER(8)

REORDER_LVL

NUMBER(8)

SELL_PRICE

NOT NULL

NUMBER(8,2)

COST_PRICE

NOT NULL

NUMBER(8,2)

TABLE NAME:SALESMAN_MASTER
SQL>CREATE TABLE SALESMAN_MASTER (
2

SALESMAN_NO

VARCHAR2 (6)

PRIMARY KEY,

NAME

VARCHAR2 (20)

NOT NULL,

ADDRESS1

VARCHAR2 (10),

ADDRESS2

VARCHAR2 (10),

CITY

VARCHAR2 (10),

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


3-

STATE

VARCHAR2 (10),

PINCODE

NUMBER (6)

NOT NULL,

SAL_AMT

NUMBER (8,2)

DEFAULT 0 NOT NULL,

10

TARGET_AMT

NUMBER (6,2)

DEFAULT 0 NOT NULL,

11

REMARKS

VARCHAR2 (10),

12

CHECK(SALESMAN_NO LIKE 'S%')

13

);

Table created.
SQL>DESC SALESMAN_MASTER;
Name
Null?
Type
----------------------------------------------------SALESMAN

NOTNULL

VARCHAR2(6)

NAME

NOTNULL

VARCHAR2(10)

ADDRESS1

VARCHAR2(10)

ADDRESS2

VARCHAR2(10)

CITY

VARCHAR2(10)

STATE

VARCHAR2(10)

PINCODE

NOTNULL

NUMBER(6)

TARGET_AMT

NOTNULL

NUMBER(8,2)

SAL_AMT

NOTNULL

NUMBER(6,2)

REMARKS

VARCHAR2(10)

TABLE NAME:SALES_ORDER
SQL>CREATE TABLE SALES_ORDER(
2 S_ORDER_NO

VARCHAR2(6) PRIMARYKEY CHECK(S_ORDER_NO LIKEO%),

3 S_ORDER_DATE

DATE

4 CLIENT_NO

VARCHAR2(6) REFERENCES CLIENT_MASTER (CLIENT_NO),

5 DELVE_ADDRESS

VARCHAR2( 20),

6 SALESMAN_NO

VARCHAR2 (6) SALESMAN_MASTER


(SALESMAN_NO),

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


4-

7 DELVE_TYPE

VARCHAR2 (1) DEFAULT F,

8 BILLED_YN

CHAR(1),

9 DELVE_DATE

DATE,

10 ORDER_STATTUS

VARCHAR2 (10) CHECK (ORDER_STATUS IN(IN

PROGRESS,FULFILLED,BACK ORDERED,CANCELLED)),
11 CHECK DELVE_DATE>S_ORDER_DATE)
12 );
Table created.
SQL>DESC SALES_ORDER;
Name
Null?
Type
--------------------------------------------S-ORDER_NO

NOTNULL

VARCHAR2(6)

S_ORDER_DATE

DATE

CLIENT_NO

VARCHAR2(6)

DELVE_ADDRESS

VARCHAR2(6)

SALESMAN_NO

VARCHAR2(6)

DELVE_TYPE

VARCHAR2(6)

BILLED_YN

CHAR(1)

DELVE_DATE

DATE

ORDER_STATUS

VARCHAR2(10)

TABLE NAME>SALESORDERDETAILS
SQL>CREATE TABLE_SALES_ORDER_DETAILS(
2 S_ORDER_NO

VARCHAR2 (6) REFERENCES SALES_ORDER (S_ORDER_NO),

3 PRODUCT_NO

VARCHAR2 (6) REFERENCES PRODUCT_MASTER (PRODUCT_NO),

4 QTY_ORDERED

NUMBER(8),

5 QTY_DISP

NUMBER(8),

6 PRODUCT_RATE

NUMBER(10,2),

7 PRIMARYKEY (S_ORDER_NO,PRODUCT_NO)
8);
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
5-

Table created.
SQL>DESC SALES_ORDER_DETAILS;
Name
Null?
Type
-----------------------------------------------------------S_ORDER_NO

NOTNULL

VARCHAR2 (6)

PRODUCT_NO

NOTNULL

VARCHAR2 (6)

QTY_ORDERED

NUMBER (8)

QTY_DISP

NUMBER (8)

PRODUCT_RATE

NUMBER (10,2)

TABLE NAME: CHALLAN_MASTER


SQL>CREATE TABLE CHALLAN_MASTER (
2 CHALLAN_NO VARCHAR2 (6) PRIMARY KEY CHECK (CHALLAN_NO LIKE CH%),
3 S_ORDER_NO VARCHAR2 (6) REFERENCES SALES_ORDER(S_ORDER_NO),
4 CHALLAN_DATE
5 BILED_YN

DATE,
CHAR(1) DEFAULT N CHECK(BILLED_YN IN(,YN))

6 );
Table created.
SQL>DESC CHALLAN_MASTER;
NAME

NULL?

CHALLAN NO

NOT NULL

TYPE
VARCHAR2 (6)

S_ORDER_NO

VARCHAR2 (6)

CHALLAN_DATE

DATE

BILLED_YN

CHAR(1)

TABLE NAME: CHALLAN_DETAILS


SQL> CREATE TABLE CHALLAN_DETAILS (
2 CHALLAN_NO VARCHAR2 (6) REFERENCES CHALLAN_MASTER(CHALLAN_NO),
3 PRODUCT_NO VARCHAR2(6) REFERENCES PRODUCT_MASTER(PRODUCT_NO),

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


6-

4 QTY_DISP NUMBER(8) NOT NULL,


5 PRIMARY KEY(CHALLAN_NO, PRUDUCT_NO)
6 );
Table created.
SQL>DESC CHALLAN_DETAILS;
Name

Null?

Type

-------------------------------------------------------------CHALLAN_NO

NOT NULL

VARCHAR2(6)

PRODUCT_NO

NOT NULL

VARCHAR2(6)

QTY_DISP

NOT NULL

NUMBER(8)

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


7-

INSERTION OF DATA INTO THE TABLES


TABLE NAME: CLIENT_MASTER
SQL>INSERT INTO CLIENT _MASTER VALUES(
2 '&CLIENT_NO', '&NAME',&ADDRESS1', ADDRESS2' ,'&CITY',
'&STATE',&PINCODE,&BAL_DUE);
Enter value for client_no:C00001
Enter value for name: IVAN BAYROSS
Enter value for address1: HOSUR ROAD
Enter value for address2: KORAMANGALA
Enter value for city: BANGALORE
Enter value for state:KARNATAKA
Enter value for pincode:560034
Enter value for bal_due:15000
Old 2: '&CLIENT_NO', '&NAME',&ADDRESS1', ADDRESS2' ,'&CITY',
'&STATE',&PINCODE,&BAL_DUE)
New 2: 'C00001', 'IVAN' 'BAYROSS', 'HOSUR ROAD', 'KORAMANGALA',
'BANGALORE', 'KARNATAKA', 560034, 15000
1 row created.
Like this insert the values in to the table.
SQL>COMMIT;
Commit complete.
SQL>SET PAGESIZE 20
SQL>SET LINESIZE 120
SQL>SELECT *FROM CLIENT_MASTER;

CLIENT
_NO
C00001

NAME
IVAN

BAYROSS
C00002 VANDANA

ADDRESS1
HOSUR
ROAD
JAIHIND

ADDRESS2

CITY

STATE

PIN

CODE
KORAMANGALA BALGLORE KARNATAKA 560034
MANALI

CHENNAI

TAMILNADU

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


8-

BAL_DUE
15000

78001

'GOOD')
1row created.
Like this insert the values in to the table.
SQL>COMMIT;
SQL>SELECT*FROM SALESMAN_MASTER;
SALES
MAN_NO
S00001

NAME

ADDRE

ADDRE

CIT

STAT

PINCOD

KIRA

SS1
A/14

SS2
WORLI

Y
MUM

E
MAHA

BAI

RAST

NARIM

MUM

RA
MAHA

AN

BAI

RAST

BANDR

MUM

RA
MAHA

BAI

RAST

N
S00002

MANI

65

SH
S00003

RAVI

P-7

SAL_AMT

TARGE

REMARKS

E
400002

3000

T_AMT
100

GOOD

400001

3000

200

GOOD

400032

3000

200

GOOD

400044

3500

200

GOOD

RA
S00004

ASHI

A/5

JUHU

SH

MUM

MAHA

BAI

RAST
RA

4rows selected.
TABLE NAME:SALES_ORDER
SQL>INSERT INTO SALES_ORDER VALUES(
2'&S_ORDER NO',& S_ORDER_DATE,'&CLINT_NO', '&DELVE_ADDRESS',
', '&SALESMAN NO&',DELVE_TYPE', '&BILLED_YN',&DELVE_DATE,
'&ORDER_STATUS');
Enter value for s_order_number:O19001
Enter value for s_order_date:SYSDATE
Enter value for client_no:C00001
Enter value for delve_address:F-5,L B NAGAR
Enter value for salesman_no: S00001
Enter value for delve_type:F
Enter value for billed_yn:N
Enter value for delve_ date:SYSDATE+7
Enter value for order_status:IN PROCESS
Old 2: 2'&S_ORDER NO',& S_ORDER_DATE,'&CLINT_NO', '&DELVE_ADDRESS',
', '&SALESMAN NO&',DELVE_TYPE'
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
9-

new 2:o19001,SYSDATE,C00001,F-5,L B
NAGAR ,S00001,F,N,SYSDATE+7,IN
PROCESS)
1 row created.
Like this insert the values

into the tables.

SQL>COMMIT;
SQL>SELECT * FROM SALES _ORDER;
S_ORDE

S_ORDER

CLIENT

DELVE

SALES

DELVE

BIL

DELVE

OREDR_STA

R_NO

_DATE

_NO

_ADDRES

MAN_NO

_TYPE

LED

_DATE

TUS

25-FEB-10

IN

S
O19001

18-FEB-10

C00001

F-5,LB

_YN
S00001

NAGAR
O19001
O46865

18-FEB-10
18-FEB-10

PROCESS

C00002

P/5,MAN

S00002

20-FEB-10

CANCELLED

C00003

LI
S-

S00003

22-FEB-10

FULFILLD

S00001

21-FEB-10

FULFILLD

S00002

24-FEB-10

CANCELLED

S00004

20-FEB-10

IN

10,AND
HARI
O19003

18-FEB-10

C00001

WEST
A/2,KRI
SH NA

O46866

18-FEB-10

C00004

MARG
F1,MEGA
ESTATES

O10008

18-FEB-10

C00005

58,R M
NAGAR

PROCESS

6 rows selected .
TABLE NAME: SALES _ORDER_DETAILS
SQL>INSERT INTO SALES_ORDER _DETAILS VALUES(
2 &S_0RDER _NO,&PRODUC
T_NO,&QTY_ORDERED,&QTY_DISP,&PRODUCT_RATE);
Enter value for s_order_no:O19001
Enter value for product_no:p00001
Enter value for qty_ordered:4
Enter value for qty_disp:4
Enter value for product_rate:525
old 2 :&S_ORDER_NO,&PRODUCT_NO,&QTY_ORDERED,&QTY_DISP,
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
10 -

&PRODUCT_RATE)
SATIWAL

NAGAR

HIGH RD

C00003

PRAMADA

AMBEDKAR

SARDER

C00004

JAGUSTE
BASU

MARG
WOOD

NAGAR
ANDERI

NAVINDGI

HOUSE

WEST

RAVI

ROAD
FATEHPURI

NAWAB

C00005

SREE

MUMBAI
MUMBAI

MAHARASTRA

400057

500

MAHARASTRA

5
400056

0
0

100001

200

DELHI

GANJ

DHARAN
C00006

RUKMINI

JUKKA

PUNJAB

CHOWK

CAMP

MUMBAI

MAHARASTRA

0
400050

COLONY
6 rows selected.
TABLE NAME:PRODUCT_MASTER
SQL>INSERT INTO PRODUCT_MASTER VALUES(
2&PRODUCT_NO,&DESCRIPTION,&PROFIT_PERCENT,&UNIT_MEASURE,
&QTY_NO_HAND,&REORDER_LVL,&SELL_PRICE,&COST_PRICE);
Enter values for product-no:P00001
Enter value for description: 1.44 FLOPPIES
Enter value for profit_percent:5
Enter value for unit_measure:PRICE
Enter value for qty_on_hand:100
Enter value for reorder_lvl:20
Enter value for sell-price:525
Enter value for cost_price:500
Old 2; 2&PRODUCT_NO,&DESCRIPTION,&PROFIT_PERCENT,&UNIT_MEASURE,
&QTY_NO_HAND,&REORDER_LVL,&SELL_PRICE,&COST_PRICE);
new 2: P00001,1.44 FLOPPIES,5,PRICE,100,20,525,500)
1 row created.

Like this insert the values into the list.


SQL>COMMIT;
SQL>SELECT * FROM PRODUCT_MASTER;
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
11 -

PRODUCT

DESCRIPTION

_NO

PROFIT_

UNIT_MEAS

QTY_O

REORD

SELL_

COST

PERCENT

URE

ER_LV

PRICE

_HAND

PRIC

P00001

1.44FLOPPIE

PRICE

100

20

525

E
500

P03453

S
MONITORS

PRICE

10

12000

1128

P06734
P07865

MOUSE
1.22

5
5

PRICE
PRICE

20
100

5
20

1050
525

0
1000
500

P07868
P07885

FLOPPIES
KEYBOARDS
CD DRIVE

2
2.5

PRICE
PRICE

10
10

3
3

3150
5250

3050
5100

P07965
P07975
P08865

540 HDD
PEN DRIVE
FLOPPY

4
5
5

PRICE
PRICE
PRICE

10
10
2

3
3
3

8400
1050
1050

8000
1000
1000

DRIVE

9 rows selected.
TABLE NAME :SALESMAN_MASTER
SQL>INSERT INTO SALESMAN_MASTER VALUES(
2 &SALESMAS_NO,&NAME,&ADDRESS1,&ADDRESS2,&CITY,&STATE,
&PINCODE,&SAL_AMT,&TARGET_AMT,&REMARKS);
Enter value for salesman_no: S00001
Enter value for name: KIRAN
Enter value for address1: A/14
Enter value for address2:WORLI
Enter value for city:MUMBAI
Enter value for state:MAHARASTRA
Enter value for pincode:400002
Enter value for sal_amt:3000
Enter value for target_amt:100
Enter value for remarks:GOOD
old 2:
&SALESMAS_NO,&NAME,&ADDRESS1,&ADDRESS2,&CITY,&STATE,
&PINCODE,&SAL_AMT,&TARGET_AMT,&REMARKS);
New
2:S00001,KIRAN,A/14,WORLI,MUMBAI,MAHARASTRA,400002,3000,10
0
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
12 -

new 2:O19001,P00001,4,4,525)
1 row created.
Like this insert the values into the list.
SQL>COMMIT;
SQL>SELECT * FROM SALES _ORDER_DETAILS;
S_OR_N

PRODUCT_

QTY_ORDER

QTY_DIS

PRODUCT

ODER
O19001
O19001

NO
P00001
P07965

ED
4
2

P
4
1

_RATE
525
8400

O19001
O19002

P07885
P00001

2
10

1
0

5250
525

O46865
O46865
O46865
O46865
O19003
O19003

P07868
P07885
P00001
P03453
P03453
P06734

3
3
10
4
2
1

3
1
10
4
2
1

3150
5250
525
12000
12000
1050

O46866
O46866
O10008

P07965
P07975
P00001

1
1
10

0
0
5

8400
1050
525

O10008

P0797500

1050

TABLE NAMME: CHALLAN_MASTER


SQL> INSERT INTO CHALLAN_MASTER VALUES (
2 &CHALLAN_NO,&S_ORDER_NO,&CHALLAN_DATE,&BILLED_YN);
Enter value for challan_no:CH9001
Enter value for s_order_no:O19001
Enter value for challan_date:18-feb-10
Enter value for billed-yn : Y
Old 2:CHALLAN-NO,&S_ORDER_NO,&CHALLAN_DATE,&BILLED_YN)
NEW 2: CH9001,019001,18-FEV-10,Y)
1 row created.
SQL>/
Enter value for cahllan_no:CH6865
Enter value for s_order_no:046865
Enter value for cahllan_date:18-FEB-10
Enter value for billed_yn:Y
Old 2:&CHALLAN_NO,&S_ORDER_NO,&CHALLAN_DATE,&BILLED_YN)
New 2:CH6865,04665,18-FEB-10,Y,)
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
13 -

1row created.
SQL>/
Enter value for cahllan_no:CH3965
Enter value for s_order_no:010008
Enter value for cahllan_date:18-FEB-10
Enter value for billed_yn:Y
Old 2:&CHALLAN_NO,&S_ORDER_NO,&CHALLAN_DATE,&BILLED_YN)
New 2:CH39665,010008,18-FEB-10,Y,)
1row created.
SQL>/ COMMIT;
SQL>/SELECT *FROM CHALLAN_MASTER;
CHALLA

S_ORDER_

CHALLAN_D

BILLED_Y

N_NO
CH9001
CH6865

NO
O19001
O46865

ATE
18-FEB-10
18-FEB-10

N
Y
Y

O19001

O10008

18-FEB-10

CH3965

TABLE NAME:CHALLAN_DETAILS
SQL> INSERT INTO CHALLAN_DETAILS VALUES(
2 &CHALLAN_NO,&PRODUCT_NO,&QTY_DISP,);
Enter value for challan_no:CH9001
Enter value for product_no:p00001
Enter value for qty_disp:4
Old 2:&CHALLAN_NO,PRODUCT_NO,&QTY_DISP)
New 2:&ch9001,,p00001,,4)
1 row created.
Like this insert the values into the table.
SQL>COMMIT;
SQL>SELECT*FROM CHALLAN_DETAILS;
CHALLA

PRODUCT

QTY_DISP

N_NO
CH9001
CH9001

_NO
P00001
P07965

4
1

CH9001
CH6865
CH6865
CH6865
CH3965
CH3935

P07885
1
P07868
3
P03453
4
P00001
10
P00001
5
Lab
Cycle-I, DBMS
Lab Record, Dept. of Computer Applications, ANU
14 P07975
2

8 rows selected.

SOLVE THE FOLLOWING QUERIES BY USING ABOVE TABLES


1.RETRIEV THE LIST OF NAMES AND CITIES OF ALL THE CLIENTS
SQL>SELECT NAME, CITY FRLM CLIENT_MASTER;
NAME

CITY

------------------------IVAN BAYROSS

BANGALORE

VANDANA SAITVAL

CHENNAI

PRAMADA JAGUSTE

MUMBAI

BASU NAVINDGIV

MUMBAI

RAVI SREEDHARAN

DELHI

RUKMINI

MUMBAI
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
15 -

rows are selected.

2.LIST THE VARIOUS PRODUCTS AVAILABLE FROM PRODUCTS_MASTER


SQL>SELECT DESCRIPTION FROM PRODUCTS_MASTER;
DESCRIPTION
1.44FLOPPIES
MONITORS
MOUSE
1.22 FLOPPIES
KEYBOARDS
CD DRIVE
540 HDD
PEN DRIVE
FLOPPY DRIVE
9rows selected.
3.FIND OUT THE CLIENTS WHO STAY IN A CITY WHOSE SECOND LETTER IS A
SQL>SELECT NAME FROM CLIENT_MALTER WHERE CITY LIKE_A%;
NAME
-----IVAN BAYROSS

4.FIND THE LIST OF ALL CLIENTS WHO STAY IN THE CITYCHENNAIORDELHI


SQL>SELECT NAME FROM CLIENT_MASTER WHERE CITY IN (CHENNAI,DELHI);
NAME
-------VANDANA SAITWAL
RAVI SREEDHARAN
5.LIST ALL THE CLIENTS LOCATESD AT CHEMMAI
SQL>SELECT NAME FROM

CLIENT_MASTER WHERE CITY=CHENNAI;

NAME
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
16 -

-----VANDANA SAITWAL
6.PRINT THE INFORMATION FROM SALES ORDER AS THE ORDER PLACES IN
THE MONTH OF JANUARY
SQL>SELECT*FROM SALES_ORDER WHERE
2 TO_CHAR (S_ORDER_DATE,MONTH)= JANUARYL
No rows selected
7. FIND THE PRODUCCTS WITH DESCRIPTION AS FLOPPY DRIVE AND PEN
DRIVE
SQL>SELECT PRODUCTS_NO, DESCRIPTION FROM PRODUCT_MASTER
2 WHERE DERSCIPTION + FLOPPY DRIVE OR DESCRIPTION = PEN DRIVE;
PRODUC

DESCRIPTION

-----------------------------------------------------P07975

PEN DRIVE

P08865

FLOPPY DRIVE

8. FIND THE PRODUCTS WHOSE SELLING PRICE IS GRATER THAN 2000 AND LESS
THAN OR EQUL TO 5000
SQL>SELECT DESCRIPTION FROM PRODUCT_MASTER WHERE SELL_PRICE> 2000 AND
SELL_PRICE<=5000;

DESCRIPTION
--------------------KEYWORDS
9.FIND THE PRODUCTS WHOSE SELLING PRICE IS MORE THAN 1500 AND ALSO
FIND THE NEW SELLING PRICE AS ORIGINAL SELLING PRICE*15
SQL>SELECT DESCRIPTION, SELL_PRICE*0.15 NEW_PRICE
2 FROM PRODUCT_MASTER WHERE SELL_PRICE>1500;
DESCRIPTION

NEW_PRICE

--------------------------------------------------MONITORS

1800

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


17 -

KEYBOARDS

472.5

CD DRIVE

787.5

540HDD

1260

10. FIND THE PRODUCTS IN THE SORTED ORDER OF THEIR DESCRIPTION


SQL>SELECT DESCRIPTION FROM PRODUCT_MASTER ORDER BY DESCRIPTION;
DESCRIPTION
-----------------------1.22 FLOPPIES
1.44 FLOPPIES
540 HDD
CD DRIVE
FLOPPY DRIVE
KEYBOARDS
MONITORS
MOUSE
PENDRIVE
9row selected.
11. DIVIDE THE COST OF PRODUCT 540 HDD BY DIFFERENCE BETWEEN ITS
PRICE AND 100
SQL>SELECT COST_PRICE, COST_PRICE/(COST_PRICE 100) NEW_COST FROM
PRODUCT_MASTER WHERE DESCRIPTION=540 HDD;

COST_PRICE

NEW_COST

-------------------------------------------------8000

1.01265823

12.LIST THE PRODUCT NUMBER, DESCTIPTION, SELL PRICE OF PRODUCTS


WHOSE DESCRIPTION BEGIN WITH LETTER M
SQL> SELECT PROUCT_NO, DESCRIPTION, SELL_PRICE FROM PRODUCT_MASTER
WHERE DESCRIPTION LIKE M%;

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


18 -

PRODUC

DESCRIPTION

SELL_PRICE

--------------------------------------------------------------------------

P03453

MONITORS

12000

P06734

MOUSE

1050

13.LIST ALL THE ORDERS THAT WERE CANCELLED IN THE MONTH OF MARCH
SQL>SELECT S_ORDER_NO FROM SALES_ORDER WHERE
2 ORDER_STATUS + CANCELLED AND TO_CHAR (S_ORDER_DATE, MONTH) =
MARCH;
No rows selected
14.COUNT THE TOTAL NUMBER OF ORDERS
SQL>SELECT COUNT(*) TOTAL_ORDERS FROM SALES_ORDER;
TOTAL_ORDERS
------------------------6
15.CALCUATE THE AVERAGE PRICE OF ALL THE PRODUCTS
SQL>SELECT AVG(SELL_PRICE), AVG(COST_PRICE)FROM PRODUCT_MASTER;
AVG(SELL_PRICE) AVG(COST_PRICE)
-----------------------------------------------------------3666.66667

3492.22222

16. DETERMINT THE MAXIMUM AND MINIMUM PRODUCT PRICES


SQL>SELECT MIN(SELL_PRICE) MIN_PRICE, MAX(SELL_PRICE) MAX_PRICE
FORM PRODUCT_MASTER;

MIN_PRICE

MAX_PRICE

--------------------------------------------------525

12000

17. COUNT THE NUMBER OF PRODUCTS HAVING PRICE GRATER THAN OR EQUL TO
1500
SQL>SELECT COUNT(*)

FROM PRODUCT_MASTER WHERE SELL_PRICE>=1500;

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


19 -

COUNT(*)
---------------4
18. FINE ALL THE PRODUCTS WHOSE QUATITY ON HAND IS LESS THAN REORDER
LEVEL
SQL> SELECT DESCRIPTION FROM PRODUCT_MASTER WHERE QTY_ON_HAND <
RECORDER_LVL,;
DESCRIPTION
-------------------FLOPPY DRIVE

19.FIND OUT THE CHALLAN DETAILS WHOSE QUANTITY DISPATCH IS HIGN


SQL>SELECT*FROM CHALLAN_DETAILS WHERE QTY_DISP IN
2(SELECT MAX(QTY_DISP)FROM CHALLAN_DETAILS);
CHALLAN

PRODUC

QTY_DISP

---------------------------------------------------------CH6865

P00001

10

20. FIND OUT THE ORDER STATUS OF THE SALES ORDER, WHOSE ORDER
DELIVERY IS MAXIMUM IN THE MONTH OF MARCH (FEBRUARY)
SQ>

SELECT S_ORDER_NO, ORDER_STATUS FROM SALES_ORDER WHERE

TO_CHAR(S_ORDER_DATE,MON) = FEB AND S_ORDER _NO IN (SELECT


S_ORDER_NO FROM SALES_ORDER_DETAILS WHERE QTY_ORDERED =(SELCECT
AMX(QTY_ORDERED FROM SALES_ORDER_DETAILS));
S_ORDE

ORDER_STAT

--------------------------------------------O1000

IN PROCESS

O19002

CANCELLED

O46868

FULFILLED

21. FINE OUT THE TOTAL SALES MADE BY THE EACH SALESMAN
SQL>SELECT SM.SALESMAN_NO,SM.NAME,SUM(SOD.QTY_ORDERED)SALES FROM
2 SALESMAN_MASTER SM,SALES_ORDER SO, SALES_ORDER_DETAISL SOD WHERE
3 SM.SALESMAN_NO =SO.SALESMAN_NO AND SO.S_ORDER_NO =SOD.S_ORDER_NO
4 GROUP BY SM.SALESMAN_NO, SM.NAME;
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
20 -

SALESM

NAME SALES

-----------------------------------------S00001

KIRAN

11

S00002

MANISH

12

S00003

RAVI

20

S00004

ASHISH

15

22. FIND THE TOTAL REVENGE GAINED BY THE EACH PRODUCT SALES IN THE
PERIOD OF Q1 AND Q2 OF YEAR 2006 (OR 2010)
SQL>SELECT SOD.PRODUCT_NO,PM.DESCRIPTION,
2 SUM(SOD.QTY_DISP*SOD.PRODUCT_RATE)SALES_PER PRODUCT FORM
3 SALES_ORDER_DETAILS SOD, PRODUCT_MASTER PM, SALES_ORDER SO WHERE
4 SO.S_ORDER_DATE BETWEEN01-JAN-0 AND
5 SO.S_ORDER_NO =SOD.S_ORDER_NO AND
6 PM.PRODUCT_NO =SOD.PRODUCT_NO GOUPT BY
7 SOD.PRODUCT_NO, PM.DESCRIPTION
8 UNION
9 SELECT SOD.PRODUCT_NO,PM.DESCRIPTION,
10 SUM(SOD.QTY_DISP*SOD.PRODUCT_RATE)SALES_PER_PRODUCT FORM
11 SALES_ORDER_DETAILS SOD,PRODUCT_MASTER PM, SALES_ORDER SO WHERE
12 SO.S_ORDER_DATE BETWEEN 01-JAN-10 AND 31-DEC-10 AND
13 SO.S_ORDER_NO =SOD.S_ORDER_NO AND
14 PM.PRODUCT_NO=SOD.PRODUCT _NO GROUP BY
15 SOD.PRODUCT_NO, PM.DESCRIPTION;

PRODUC

DESCRIPTION

SALES_PER_PRODUCT

--------------------------------------------------------------------P00001

1.44 FLOPPIES

9975

P03453

MONITORS

6300

P06734

MOUSE

12000

P07868

KEYBOARDS

9450

P07885

CDD DRIVE

10500

P07965

540 HDD

8400

P07975

PEN DRIVE

3150

7 rows selceted.

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


21 -

23.PRINT THE DESCRITPTION AND TOTAL QTY SOLD FOR EACH PRODUCT
SQL>SELECT DESCRITPTION,SUM(QTY_DISP) FORM PRODUCT_MASTER PM,
SALES_ORDER_DETAILS SOD WHERE PM.PRODUCT_NO = SOD.PRODUCT_NO GOUP BY
DESCRIPTION;
DESCRIPTION

SUM(QTY_DISP)

------------------------------------------------------------1.44 FLOPPIES

19

540 HDD

CD DRIVE

KEYBOARDS

MONITORS

MOUSE

PEN DRIVE

7 rows selected.

24. FIND THE VALUES OF EACH PRODUCT SOLD


SQL>SELECTE SOD.PRODUCT_NO, PM.DESCRIPTION, SUM(SOD.QTY_DISO*
SOD.PRODUCT_RATE)SALES_PER_PRODUCT FORM SALES_ORDER_DETAILS SOD,
PRODUCT_MASTER PM WHERE PM. PRODUCT_NO=SOD.PRODUCT_NO GOURP BY
SOD.PRODUCT_NO, PM.DESCRIPTION;

PRODUC

DESCRIPTION

SALES_PER_PRODUCT

--------------------------------------------------------------------P00001

1.44 FLOPPIES

9975

P03453

MONITORS

6300

P06734

MOUSE

12000

P07868

KEYBOARD

9450

P07885

CD DRIVE

10500

P07965

540 HDD

8400

P07975

PEN DRIVE

3150

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


22 -

7 rows selceted.

25. CALCULATE THE AVERAGE QTY SOLD FOR EACH CLIENT THAT HAS MAXIMUM
ORDER VALUE OF 15,000
SQL>SELECT CM.CLIENT_NO, AVG(SOD.QTY_DISP)AVGSALES FORM CLIENT_MASTER
CM,SALESS_ORDER SO, SALES_ORDER_DETAILS SOD WHERE CM.CLIENT_NO=
SO.CLIENT_NO AND SO.S _ORDER_NO=SOD.S_ORDER_NO GROUP BY CM.CLIENT_NO
HAVING MAX(SOD.QTY_ORDERED *SOD.PRODUCT_RATE)>15000;
CLIENT

AVGSALES

---------------------------------------C00001

1.8

C00003

4.5

26. LIST THE PRODUCTS WHICH HAS HIGHEST SALES


SQL> SELECT PRODUCT_NO, DESCRIPTION FORM PRODUCT_MASTER WHERE
PRODUCT_O IN(SELECT PRODUCT_NO FROM SALES_ORDER_DETAILS WHERE
QTY_ORDERED =(SELECT MAX(QTY_ORDERED)FROM SALES_ORDER_DETAILS));
PRODUC

DESCRIPTION

-------------------------------------------P00001

1.44 FLOPPIES

27.FIND OUT THE PRODUCTS AND EHEIR QUANTITIES THAT WILL HAVE TO
DELIVER IN THE CURRENT MONTH
SQL> SELECT SOD.PRODUCT_NO, PM.DESCRIPTION,SUM(SOD.QTY_ORDERED) FROM
SALES_ORDER_DETAILS SOD, SALES_ORDER SO, PRODUCT_MASTER PM WHERE
PM.PRODUCT_NO=SOD.PRODUCT_NO AND SO.S_ORDER_NO =SOD.S_ORDER_NO AND
TO_CHAR(DELVE_DATE,

MON-YY)

TO_CHAR(SYSDATE,MON-YY)

GROUP

BY
SOD.PRODUCT_NO,PM DESCRIPTION;
PRODUC

DESCRIPTION

SUM(SOD.QTY_ORDERED)

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


23 -

-------------------------------------------------------------------------------------------P00001

1.44 FLOPPIES

34

P03453

MONITORS

P06734

MOUSE

P07868

KEYBOARDS

P07885

CD DRIVE

P07965

540 HDD

P07975

PENDRIVE

7 rows selected.
28.FIND THE PRODUCT NUMBER AND DESCRIPTIONS OF MOVING PRODUCTS
SQL> SELECT PROCUCT_NO, DESCRIPTION FORM PRODUCT_MASTER WHERE
PRODUCT_NO IN(SELECT PRODUCT_NO FORM SALES_ORDER_DETAILS);
PRODUC

DESCRIPTION

-----------------------------------------P00001

1.44 FLOPPIES

P03453

MONITORS

P06734

MOUSE

P07868

KEYBOARD

P07885

CD DRIVE

P07965

540 HDD

P07975

PEN DRIVE

7 rows selected.

29. FIND THE NAMES OF CLIENTS WHO HAVE PURCHASED CD DRIVE


SQL>SELECT DISTINCT SO.CLIENT_NO, CM.NAME FORM SALES_ORDER_DETAILS
SOD,
SALES_ORDER SO, PRODUCT_MASTER PM, CLIENT_MASTER CM WHERE
PM.PRODUCT_NO=SO.CLIENT_NO AND DESCRIPTION =CD DRIVE;
CLEINT

NAME

-------------------------------------------C00001

IVAN BAYROSS

C00003

PRAMADA JAGUSTE

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


24 -

30.LIST THE PRODUCT NUMBER AND SALES ORDER NUUMBER OF CUSTOMERS


HAVING QUANTITY ORDERED LIST THAN 5 FORM THE ORDER DETAILS FOR
THE PRODUCT1.44 FLOPPIES
SQL>SELECT SOD.PRODUCT_NO,SOD.S_ORDER_NO FORM SALES_ORDER_DETAILS
SOD,
SALES_ORDER SO, PRODUCT_MASTER PM WHERE SO.S_ORDER_NO =SOD.S_ORDER_NO
AND PM.PRODUCT_NO = SOD.PRODUCT_NO AND SOD.QTY_ORDERED < 5 AND
DESCRIPTION =1.44 FLOPPIES;
PRODUC

S_ORDE

---------------------------------P00001

O19001

31. FIND THE PRODUCT NUMBERS AND DESCRIPTIONS OF NOIN-MOVING PRODUCTS


SQL> SELECT PRODUCT_NO, DESCRIPTION FORM PRODUCT_MASTER WHERE
PRODUCT_NO NOT IN(SELECT PRODUCT_NO FORM SALES_ORDER_DETAILS);
PRODUC

DESCRIPTION

-------------------------------------------P07865

1.22 FLOPPIES

P08865

FLOPPY DRIVE

32.FIND THE CUSTOMER NAME AND ADDRESS FOR THE CLIENTS, WHO PLACED THE
ORDER 019001
SQL> SELECT NAME, ADDRESS1, ADDRESS2, CITY,STATE, PINCODE FORM
CLINET_MASTER WHERE CLIENT_NO IN (SELECT CLIENT_NO FORM SALES_ORDER
WHERES_ORDER_NO =O19001);
NAME

ADDRESS1

ADDRESS2

CITY STATE

PINCODE

---------------------------------------------------------------------------------------------------------------IVAN BAYROSS HOSUR ROAD KORAMANGALA BANGLORE KARNATAKA 560034

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


25 -

33. FIND THE CLIENT NAMES, WHO HAVE PLACED ORDERS BEFORE THE MONTH OF
MAY,2006
SQL>SELECT CLIENT_ORDER WHERE TO_CHAR(S_ORDER_DATE, MON,YY)<
MAY,06);
CLIENT

NAME

-----------------------------------C00001

IVAN BAYROSS

C00002

VANDANA SAITWAL

C00003

PRAMADA JAGUSTE

C00004

BASU NAVINDGA

C00005

RAVI SREEDHARAN

34. FIND THE NAMES OF CLIENTS WHO HAVE PLACED ORDERS WORTH OF 10000
OR MORE
SQL>SELECT NAME FORM CLIENT_MASTER WHERE CLIENT _NO IN (SELECT
CLIENT_NO
FORM SALES_ORDER WHERE S_ORDER_NO IN (SELECT_NO FORM
SALES_ORDER_DETAILS WHERE(QTY_ORDERED*PRODUCT_RATE)>=10000));

NAME
-------------------------IVAN BAYROSS
PRAMADA JAGUSTE

35.FINE OUT IF THE PRODUCT IS 1.44 DRIVE / 1.44 FLOPPIES IS


ORDERED BY ANY CLIENT AND PRONT THE CLIENT NUMBER, NAME TO WHOM IT IS
SOLD
SQL>SELECT CLINET_NO, NAME FORM CLIENT_MASTER WHERE CLIENT_NO IN
(SELECT CLIENT_NO FORM SALES_ORDER WHERE

S_ORDER_NO IN (SELECT

S_ORDER_NO FORM SALES_ORDER_DETAILS WHERE PRODUCT_NO IN (SELECT


PRODUCT_NO FORM PRODUCT_MASTER WHERE DESCRIPTION=1.44 FLOPPIES)));
CLIENT

NAME

-----------------------------------------C00001

IVAN BAYROSS

C00002

VANDANA SAITWAL
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
26 -

C00003

PRAMADA JAGUSTE

C00005

RAVI SREEDHARAN

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


27 -

LAB CYCLE - II

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


28 -

CREATION OF THE TABLES


TABLE NAME:
SQL>CREATE
2 SNO
3 SNAME
4 CITY
5 STATUS

TABLE S (
VARCHAR2(2)
VARCHAR2(10)
VARCHAR2(10)
NUMBER(2)

PRIMARY KEY,
NOT NULL,
NOT NULL,
NOT NULL);

Table Created.
SQL>DESC S;
Name
Null?
Type
----------------------------------------------------------SNO
SNAME
CITY
STATUS

NOT
NOT
NOT
NOT

NULL
NULL
NULL
NULL

VARCHAR2(2)
VARCHAR2(10)
VARCHAR2(10)
NUMBER(2)

TABLE NAME: P
SQL>CREATE
2 PNO
3 PNAME
4 COLOR
5 WEIGHT
6 CITY
7 COST

TABLE P(
VARCHAR2(2)
VARCHAR2(10)
VARCHAR2(6)
NUMBER(3)
VARCHAR2(10)
NUMBER(4)

PRIMARY KEY,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL);

Table created.
SQL>DESC P;
Name
Null?
Type
----------------------------------------------------------PNO
PNAME
COLOR
WEIGHT
CITY
COST

NOT NULL
NOT NULL
NOT NULL
NOT NULL
NOT NULL
NOT NULL

VARCHAR2(2)
VARCHAR2(10)
VARCHAR2(6)
NUMBER(3)
VARCHAR2(10)
NUMBER(4)

TABLE NAME: SP
SQL>CREATE TABLE SP(
2 SNO
VARCHAR2(2)
3 PNO
VARCHAR2(2)
4 QTY
NUMBER(3)
5 PRIMARY KEY (SNO,PNO));

REFERENCES S(SNO),
REFERENCES P(PNO),
NOT NULL,

Table created.
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
29 -

SQL>DESC SP;
Name
Null?
Type
----------------------------------------------------------SNO
PNO
QTY

NOT NULL
NOT NULL
NOT NULL

VARCHAR2(2)
VARCHAR2(2)
NUMBER(3)

TABLE NAME: J
SQL>CREATE
2 JNO
3 JNAME
4 CITY

TABLE J(
VARCHAR2(2)
VARCHAR2(10)
VARCHAR2(10)

PRIMARY KEY,
NOT NULL,
NOT NULL);

Table created.
SQL>DESC J;
Name
Null?
Type
----------------------------------------------------------JNO
JNAME
CITY

NOT NULL
NOT NULL
NOT NULL

VARCHAR2(2)
VRCHAR2(10)
VARCHAR2(10)

TABLE NAME: SPJ


SQL>CREATE TABLE SPJ(
2 SNO
VARCHAR2(2)
REFERENCES S(SNO),
3 PNO
VARCHAR2(2)
REFERENCES P(PNO),
4 JNO
VARCHAR2(2)
REFERENCES J(JNO),
5 QTY
NUMBER(3)
NOT NULL,
6 PRIMARY KEY(SNO,PNO,JNO);
Table created.
SQL>DESC SPJ;
Name
Null?
Type
----------------------------------------------------------SNO
NOT NULL
VARCHAR2(2)
PNO
NOT NULL
VARCHAR2(2)
JNO
NOT NULL
VARCHAR2(2)
QTY
NOT NULL
NUMBER(3)

INSERTION OF THE DATA INTO THE TABLES


TABLE NAME: S

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


30 -

SQL>INSERT INTO S VALUES(&SNO,&SNAME,&CITY,&STATUS);


Enter values for sno: S1
Enter values for sname: SMITH
Enter values for city: LONDON
Enter values for status: 20
Old 1:INSERT INTO VALUES(&SNO,&SNME,&CITY,&STARUS)
New 1:INSERT INTO VALUES(S1,SMITH,LONDON,20);
1 row created.
Like this enter the values into the table.
1 row created.
SQL>COMMIT;
SQL>SELECT * FROM S;
SNO

SNAME

CITY

STATUS

S1

SMITH

20

LONDON

S2

JONES

10

PARIS

S3

BLAKE

30

PARIS

S4

CLARK

20

LONDON

S5

ADAMS

30

ATHENS

TABLE NAME: P
SQL>INSERT INTO P VALUES
(&PNO,&PNAME,&COLOR,&WEIGHT,&CITY,&COST);
Enter the value for pno: P1
Enter the value for pname: NUT
Enter the value for color: RED
Enter the value for weight: 12
Enter the value for city: LONDON
Enter the value for cost: 350
Old 1: INSERT INTO P
VALUES(&PNO,&PNAME,&COLOR,&WEIGHT,&CITY,&COST)
New 1: INSERT INTO P VALUES(P1,NUT,RED,12,LONDON,350)
1 row creted.
Like this enter the values into the table.
SQL>COMMIT;
SQL>SELECT * FROM P;
PNO

PNAME

COLOR

WEIGHT CITY

P1

NUT

RED

12

LONDON

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


31 -

P2

BOLT

GREEN

17

PARIS

P3

SCREW

BLUE

17

ROME

P4

SCREW

RED

14

LONDON

P5

CAM

BLUE

12

PARIS

P6

COG

RED

19

LONDON

6 rows selected.
TABLE NAME: SP
SQL>INSERT INTO SP VALUES(&SNO,'&PNO,&QTY);
Enter value for sno: S1
Enter value for pno: P1
Enter value for qty: 300
Old 1: INSERT INTO SP VALUES(&SNO,'&PNO,&QTY)New 1:
INSERT INTO SP VALUES(S1,'P1,300)
1 row created.
Like this enter the values into the records.
SQL>COMMIT;
SQL>SELECT * FROM SP;
SNO

PNO

QTY

S1

P1

300

S1

P2

200

S1

P3

400

S1

P4

200

S1

P5

100

S1

P6

100

S2

P1

300

S2

P2

400

S3

P2

200

S4

P2

200

S4

P4

300

S4

P5

400

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


32 -

12 rows selected.
TABLE NAME: J
SQL>INSERT INTO
Enter value for
Enter value for
Enter value for

J VALUES(&JNO,&JNAME,&CITY);
jno: J1
jname: SORTER
city: PARIS

Old 1:INSERT INTO J VALUES(&JNO,&JNAME,&CITY)


New 1:INSERT INTO J VALUES(J1,SORTER,PARIS)
1 row created.
Like this enter the values into the table.
SQL>COMMIT;
SQL>SELECT * FROM J;
JNO

JNAME

CITY

J1

SORTER

PARIS

J2

DISPLAY

ROME

J3

OCR

ATHENS

J4

CONSOLE

ATHENS

J5

RAID

LONDON

J6

EDS

OSLO

J7

TAPE

LONDON

7 rows selected.
TABLE NAME: SPJ
SQL>INSERT INTO SPJ VALUES(&SNO,&PNO,&JNO,&QTY);
Enter value for sno: S1
Eneter value for pno: P1
Enter value for jno: J1
Enter value for qty: 200
Old 1:INSERT INTO SPJ VALUES(&SNO,&PNO,&JNO,&QTY)
New 1:INSERT INTO SPJ VALUES (S1,P1,J1,200)
1 row created.
Like this enter the values into the table.
SQL>COMMIT;
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
33 -

SQL>SELECT * FROM SPJ;


SNO
S1
S1
S2
S2
S2
S2
S2
S2
S2
S2
S3
S3
S4
S4
S5
S5
S5
S5
S5
S5
S5
S5
S5
S5

PNO
P1
P1
P3
P3
P3
P3
P3
P3
P3
P5
P3
P4
P6
P6
P2
P2
P5
P5
P6
P1
P3
P4
P5
P6

JNO
J1
J4
J1
J2
J3
J4
J5
J6
J7
J2
J1
J2
J3
J7
J2
J4
J5
J7
J2
J4
J4
J4
J4
J4

QTY
200
700
400
200
200
500
600
400
800
100
200
500
300
300
200
100
500
100
200
100
200
800
400
500

24 rows selected.

36. GET SUPPLIERS NAMES FOR SUPPLIERS WHO SUPPLY AT LEAST ONE RED
PART
SQL>SELECT S.SNME FROM S WHERE S.SNO IN (
2 SELECT SP.SNO FROM SP WHERE SP.PNO IN (
3 SELECT P.PNO FROM P WHERE P.COLOR=RED));
SNAME
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
34 -

-------------SMITH
JONES
CLARK
37. GET SUPPLIERS NAMES FOR SUPPLIERS WHO DO NOT SUPPLY PART P2
SQL>SELECT SNAME FROM S WHERE SNO NOT IN
2 (SELECT SNO FROM SP WHERE PNO=P2);
SNAME
-------------ADAMS
38. USING GROUP BY WITH HAAVING CLAUSE, GET THE PART NUMBERS FOR ALL
THE PARTS SUPPLIED BY MORE THAN ONE SUPPLIER
SQL>SELECT PNO FROM SP GROUP BY PNO HAVING COUNT(SNO)>1;
PNO
-------------P1
P2
P4
P5
39. GET SUPPLIER NUMBERS FOR SUPPLIERS WITH STATUS VALUE LESS THE
CURRENT MAX STATUS VALUE
SQL>SELECT SNO FROM S WHERE STATUS< (
2 SELECCT MAX (STATUS) FROM S);
SNO
-------------S1
S2
S4
40. GET THE TOTAL QUANTITY OF THE PART P2 SUPPLIED
SQL>SELECT SUM(QTY) FROM SP WHERE PNO=P2;
SUM(QTY)
-------------1000
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
35 -

41. GET THE PART COLOR SUPPLIED BY THE SUPPLIER S1


SQL>SELECT COLOR FROM P WHERE PNO IN (
2 SELECT PNO FROM SPJ WHERE SNO=S1);
COLOR
-------------RED
42. GET THE NAMES OF THE PARIS SUPPLIED BY THE SUPPLIER SMITH AND
BLAKE
SQL>SELECT PNAME FROM P WHERE PNO IN (
2 SELECT PNO FROM SP WHERE SNO IN (
3 SELECT SNO FROM S WHERE SNAME IN (SMITH,BLAKE)));
PNAME
-------------NUT
BOLT
SCREW
SCREW
CAM
COG
6 rows selected.
43. GET THE PROJECT NUMBERS, WHOSE PARTS ARE NOT IN RED COLOR , FROM
LONDON
SQL>SELECT JNO FROM SPJ WHERE PNO IN
2 (SELECT PNO FROM P WHERE COLOR < >RED AND CITY=LONDON);
No rows selected.
44. GET THE SUPPLIERS LOCATED FROM THE SAME CITY
SQL>SELECT S.SNO, SECOND.SNO FROM S, S SECOND
2 WHERE S.CITY=SECOND.CITY AND S.SNO < SECOND.SNO;
SNO
SNO
--------------------------------------S2
S1

S3
S4

45. GET THE SUPPLIERS, WHO DOES NOT SUPPLY ANY PART
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
36 -

SQL>SELECT SNO, SNAME FROM S WHERE SNO NOT IN


2 (SELECT SNO FROM SP WHERE SP.SNO=S.SNO);
SNO
SNAME
--------------------------------------S5

ADAMS

46. FIND THE PNAMES OF PARTS SUPPLIED BY LONDON SUPPLIER AND BY NO


ONE ELSE
SQL>SELECT PNO, PNAME FROM P WHERE PNO IN
2 (SELECT PNO FROM SP WHERE SNO IN
3 (SELECT SNO FROM S WHERE CITY=LONDON));

PNO
PNAME
--------------------------------------P1
P2
P3
P4
P5
P6

NUT
BOLT
SCREW
SCREW
CAM
COG

47. FIND THE SNOS OF SUPPLIERS WHO CHARGE MORE FOR SOME PART THAN
THE AVERAGE COST OF THAT PART
SQL>SELECT DISTINCT SNO FROM SP WHERE PNO IN
2 (SELECT PNO FROM P WHERE COST >
3 (SELECT AVG (COST) FROM P));
SNO
-------------S1
S2
S3
S4
48. FIND THE SIDS OF SUPPLIERS WHO SUPPLY ONLY RED PARTS
SQL>SELECT SNO FROM S WHERE SNO IN (SELECT SNO FROM SP WHERE PNO IN
2 (SELECT PNO FROM P WHERE COLOR=RED));
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
37 -

SNO
-------------S1
S2
S4

49. FIND THE SIDS OF SUPPLIERS WHO SUPPLY A RED AND A GREEN PART
SQL>SELECT SP.SNO FROM SP, P WHERE SP.PNO=P.PNO AND P.COLOR=RED
2 INTERSECT SELECT SP.SNO FROM SP, P
3 WHERE SP.PNO=P.PNO AND P.COLOR=GREEN;
SNO
-------------S1
S2
S4
50. FIND THE SIDS OF SUPPLIERS WHO SUPPLY A RED OR GREEN PART
SQL>SELECT SP.SNO FROM SP, P WHERE SP.PNO=P.PNO AND P.COLOR=RED
UNION
2 SELECT SP.SNO FROM SP, P WHERE SP.PNO AND P.COLOR =GREEN;

SNO
-------------S1
S2
S3
S4
SQL>SELECT SNO FROM S WHERE SNO IN (
2 SELECT SNO FROM SP WHERE PNO IN (
3 SELECT PNO FROM P WHERE COLOR IN (RED,GREEN)));
SNO
-------------S1
S2
S3
S4

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


38 -

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


39 -

LAB CYCLE III

CREATION OF THE TABLES


TABLE NAME: FLIGHTS
SQL>CREATE TABLE FLIGHTS(
2 FLNO
3 ORIGIN
4 DESIGNATION
5 DISTANCE
6 PRICE
Table created.

NUMBER(4) PRIMARY KEY,


VARCHAR2(20),
VARCHAR2(20),
NUMBER(6),
NUMBER(7,2));

SQL>DESC FLIGHTS;
Name
Null?
Type
-----------------------------------------------------FLNO
NOT NULL
NUMBER(4)
ORIGIN
VARCHAR2(20)
DESIGNATION
VARCHAR2(20)
DISTANCE
NUMBER(6)
PRICE
NUMBER(7,2)
TABLE NAME: AIRCRAFT
SQL>CREATE TABLE AIRCRAFT(
2 AID
NUMBER(9)
3 ANAME
VARCHAR2(30),
4 CRUSING_RANGE
NUMBER(6));
Table

PRIMARY KEY,

created

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


40 -

SQL>DESC AIRCRAFT;
Name
Null?
Type
-----------------------------------------------------AID
NOT NULL
NUMBER(9)
ANAME
VARCHAR2(30)
CRUISING_RANGE

NUMBER(6)

TABLE NAME: EMPLOYEE


SQL> CREATE TABLE EMPLOYEE(
2 EID
NUMBER(9)
PRIMARY KEY,
3 ENAME
VARCHAR2(30),
4 SALARY
NUMBER(6),5);
Table created
SQL> DESC EMPLOYEE;
Name
Null?
Type
---------------------------------------------------EID
NOT NULL
NUMBER(9)
ENAME
VARCHAR(30)
SALARY
NUMBER(6)
TABLE NAMR:CERTIFIED
SQL>CREATE TABLE CERTIFIED(
2 EID
NUMBER(9),
3 AID
NUMBER(9),
4 PRIMARY KEY(EID),
5 FOREIGN KEY(EID) REFERENCES EMPLOYEES,
6
FOREIGN KEY(AID) REFERENCES AIRCRAFT,
7 );
Table created.
SQL> DESC CERTIFIED;
Name
EID
AID

Null?
NOT NULL
NOT NULL

Type
NUMBER(9)
NUMBER(9)

INSERTION OF THE DATA INTO THE TABLES

TABLE NAME:FLIGHTS
SQL>INSERT INTO FLIGHTS VALUES(
2&FLNO, &ORIGN,&DESIGNATION,&DISTANCE,&PRICE);
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
41 -

Enter value for flno:99


Enter value for origin :LOS ANGELES
Enter value for distance:WASHINGTON D.C.,
Enter value for distance:2308
Enter value for price:235.98
Old 2:&FLNO, &ORIGN,&DESIGNATION,&DISTANCE,&PRICE)
New 2:99,LOS ANGELES,WASHINGTON D.C.,,2308,235.98)
1 row created.
Like this enter the values into the table.
SQL>COMMIT;
Commit complete.
SQL>SET PAGESSIZE 25
SQL>SET LINESIZE 100
SQL>SELECT *FROM FLIGHTS;
FLNO
ORIGN
DESIGNATION

DISTANCE

PRICE

99

LOS ANGELES

2308

235.98

13
346
387
7
2
33
34

LOS
LOS
LOS
LOS
LOS
LOS
LOS

WASHINGTOND.C.
,
CHICAGO
DALLLAS
BOSTON
SYDNEY
TOKYO
HONOLULU
HONOLULU

1749
1251
2606
7487
5478
2551
2551

220.98
225.43
261.56
1278.56
780.99
375.23
421.98

76
68
7789
701
702
4884
2223
5694
304
149

CHICAGO
CHICAGO
MADISON
DETROIT
MADISON
MADISON
MADISON
MADISON
MINNEAPOLIS
PITTSBURG

LOS ANGELES
NEW YORK
DETROIT
NEW YORK
NEW YORK
CHICAGO
PITTSBURGH
MINNEAPOLIS
NEW YORK
NEW YORK

1749
802
319
470
789
84
517
247
991
303

220.98
202.45
120.33
180.56
202.34
112.45
189.98
120.11
101.56
116.50

ANGELES
ANGELES
ANGELES
ANGELES
ANGELES
ANGELES
ANGELES

TABLE NAME: AIRCRAFT


SQL>INSERT INTO AIRCRAFT VALUES(&AID,&ANAME,&CRUSING_RANGE);
Enter value for aid:1
Enter value for aname:BOEING 747-400
Enter value for crusing:8430
Old 1: INSERT INTO AIRCRAFT
VALUES(&AID,&ANAME,&CRUSING_RANGE);
New 1: INSERT INTO AIRCRAFT VALUES(1,BOEING 747-400,8430)
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
42 -

1 row created
Like this enter the values into the list
SQL>COMMIT;
SQL>SELECT *FROM AIRCRAFT;
AID
ANAME
CRUSHING_R
ANGE
1
BOEING 747-400
8430
2
BOEING 737-800
3383
3
AIRBUS
7120
4
BRITISH AEROSPACE JETSTREAM41
1502
5
EMBREAR ERJ-145
1502
6
SAAB 340
2128
7
PIPER ARCHER III
520
8
TUPOLEV 154
4103
9
LOCKHEED L1011
6900
10
BOIENG 757-300
4010
11
BOEING 777-400ER
6441
12
BOEING 767-400ER
6475
13
AIRBUS A320
2605
14
AIRBUS A319
1809
15
BOEING 727
1504
16
SCHWTTZER2-33
30
TABLE NAME:EMPLOYEE
SQL>INSERT INTO EMPLOYEE VALUES(&EID,&ENAME,&SALARY);
Enter value for eid :242518965
Enter value for ename:JAMES SMITH
Enter value for salary:120433
Old 1: INSERT INTO EMPLOYEE VALUES(&EID,&ENAME,&SALARY);
New 1:INSERT INTO EMPLOYEE VALUES(242518965,JAMES
SMITH,120433);
1 row created
like this enter the values into the table
SQL>COMMIT;
Commit complete
SQL>SELECT *FROM EMPLOYEE;
EID
ENAME
SALARY
242518965 JAMES SMITH
141582651 MARY JOHNSON
011563812 JOHN WILLIAMS
LISA WALKER
567354612
552455318 LARRY WEST
550156548 KAREN SCOTT
390487451 LAWRENCE SPERRY

120433
178345
153972
256481
101745
205187
212156

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


43 -

274878947
254099823
356187925
355548984
310454876
489456522
489221823
548977562
310454877
142519864
269734834
287321212
552455348
248965255
159542516
348121541
090873519
486512566
619023588
015645489
556784565
573284895
574489456
574489457

MICHAEL MILLER
PATRICIA JONES
ROBERT BROWN
ANGELA MARTINEZ
JOSEPH THOMPSON
LINDA DAVIS
RICHARD JOCKSON
WILLIAM WARD
CHAD STEWART
BETTY ADAMS
GEORGE WRIGHT
MICHAEL MILLER
DORTHY LEWIS
BARBARA WILSON
WILLIAM MOORE
HAYWOOD KELLY
ELIZBETH TAYLOR
DAVID ANDERSON
JENNIFER THOMAS
DONALD KING
MARK YOUNG
ERIC COOPER
WILLIAM JONES
MILO BROOKS

99890
24450
44740
212156
212156
27984
23980
84476
33546
227489
289950
48090
152013
43723
48250
32899
32021
743001
54921
18050
205187
114323
105743
205187

TABLE NAME:CERTIFIED
SQL>INSERT INTO CERTIFIED VALUES(&EID,&AID);
Enter value for eid : 567354612
Enter value for aid: 1
Old 1: INSERT INTO CERTIFIED VALUES(&EID,&AID);
New 1: INSERT INTO CERTIFIED VALUES(567354612,1);
1 row created
Like this enter the values into the table.
SQL>COMMIT;
Commit complete.
SQL>SELECT *FROM CERTIFIED;
EID
AID
567354612
567354612
567354612
567354612
567354612
567354612
567354612
567354612

1
2
10
11
12
15
7
9

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


44 -

567354612
567354612
567354612
552455318
552455318
550156548
550156548
390487451
390487451
390487451
274878974
274878974
355548984
355548984
310454876
310454876
548977562
142519864
142519864
142519864
142519864
142519864
142519864
142519864
142519864
269734834
269734834
`269734834
269734834
269734834
269734834
269734834
269734834
269734834
269734834
269734834
269734834
269734834
269734834
269734834
552455318
556784565
556784565
556784565
573284895
573284895
573284895
573284895
573284895
573284895

3
4
5
2
14
1
12
3
13
14
10
12
8
9
8
9
7
1
11
12
10
3
2
13
7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
7
5
2
3
3
4
5
8
6
7

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


45 -

242518965
242518965
141582651
141582651
141582651
011564812
011564812
356187925
159542516
159542516
090873519

2
10
2
10
12
2
10
6
5
7
6

51.FOR EACH PILOT WHO IS CERTIFIED FOR MORE THAN THREE


AERCRAGT,
FIND THE EIDS AND THE MAXIMUM CRUSHING RANGE OF THE AERCRAFT
THAT
HE (OR SHE) CERTIFIED FOR
SQL>SELECT C.EID,MAX(A.CRUSHING_RANGE)FROM CERTIFIED C,AIRCRAFT
A
WHERE C.AID=A.AID GROUP BY C.EID HAVING COUNT(*)>3;
EID
MAX(A.CRUSHING_RANGE)
142519864
8430
269734834
8430
567354612
8430
52.FIND THE NAMES OF PILOTS WHOSE SALARY IS LESS THAN THE PRICE
OF
THE CHEAPEST ROUTEF FROM LOS ANGELS TO HONOLULU
SQL>SELECT DISTANCE E.NAME FROM EMPLOYEES E WHERE E.SALARY/50<
(SELECT MIN (F.PRICE) FROM FLIGHTS F
3 WHERE F.ORIGIN=LOS ANGELES AND F.DESTINATION=HONOLULU);
ENAME
DONALD KING
53.FIND THE NAME OF THE PILOTS CERTIFIED FROM SOME BOEING
AIRCRAFT
SQL>SELECT DISTANCT E.NAME FROM EMPLOYEES E,CERTIFIED
C,AERCRAFT A
WHERE E.ID=C.EID AND C.AID=A.AID AND A.NAME LIKE BOEING;
ENAME
-----------BETTY ADAMS
GEORGE WRIGHT
JAMES SMITH
JOHN WILLIAMS
KAREN SCOTT
LARRY WEST
LISA WALKER
MARY JOHNSON
MARY YOUNG
MICHAEL MILLLER
10 ROWS SELECTED
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
46 -

54.FOR ALL AIRCRAFT WITH CRUSHING RANGE OVER 1,000 MILES, FIND
THE NAME OF THE AIRCRAFT AND THE AVEREAGE SALARY OF ALL PILOTS
CERTIFIED FOR THIS AIRCRAFT
NOTE:Observe that aid is the key for aircraft, but the
question asks for aircraft names;
we deal with this complication by using an intermediate relation Temp

SQL>SELECT TEMP_NAME, TEMP.AVGSALARY FORM


2 (SELECT A.AID,A.ANAME,AVG(E.SALARY) AVGSALARY FORM AIRCRAFT
A,
CERTIFIED C,EMPLOYEES E,WHERE A.AID=C.AID AND C.EID=E.EID AND
A.CRUSHING_RANGE>1000 GROUP BY A.AID,A.NAME) TEMP;
OR
SQL>SELECT A.AID,A.NAME NAME,AVG(E.SALARY) AVGSALARY FROM
2 AIRCRAFT A,CERTIFIED C,EMPLOYEES E WHERE
3 A.AID=C.AID AND
4 C.EID=E.EID AND
5 A.CRUSHING_RANGE>1000
6 GROUP BY A.AID,A.NAME;
AID
ANAME
AVGSALARY
---------------------------------------------1
BOIENG 747-400
244776.75
2
BOIENG 737-800
191700.25
3

AIRBUS A340-300

217597.667

BRITISH AEROSPACE JETSTREAM 41

220251.333

5
6
8
9

EMBRAFER ERJ-145
SAAB 340
TUPOLEV 154
LOCKHEED L1011

10

BOEING 757-300

11
12
13

BOEING 777-300
BOEING 767-400ER
AIRBUS 1320

182838.2
118113.5
205001.25
242685.75
189508.571
257973.333
209557
243198.333

14
AIRBUS A319
201283.667
15
BOEING 727
273215.5
14 rows selected
55. FIND THE AIDS OF ALL AIRCRAFT THAN CAN BE USED FROM LOS
ANGELS
TO CHICAGO
SQL>SELECT A.AID FROM AIRCRAFT THAN CAN BE USED FROM LOS ANGELS
TO CHICAGO
SQL>SELECT A.AID FROM AIRCRAFT A WHERE A.CRUSHING_RANGE>
2 (SELECT MIN(F.DISTNACE) FROM FLIGHTS F
3 WHERE F.ORIGIN=LOS ANGELS AND F.DESTINATION=CHICAGO);
AID
1
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
47 -

2
3
6
8
9
10
11
12
13
14
11 rows selected
56.PRINT THE ENAMES OF PHILOTS WHO CAN OPERATE PLANING WITH
CRUSHING
RANGE GREATER THAN 3,000 MILES,BUT ARE NOT CERTIFIED BY BOEING
AIRCRAFT
SQL>SELECT DISTANCE E.NAME FROM EMPLOYEES E
2 WHERE E.EID IN ((SELECT C.EID FROM CERTIFIED C
3 WHERE EXITS(SELECT A.AID FROM AIRCRAFT A
4 WHERE A.AID=C.AID AND A.CRUSHING_RANGE>3000)
5 AND
6 NOT EXITS(SELECT A1.AID FROM AIRCRAFT A1
7 WHERE A1.AID=C.AID AND A1.ANAME LIKE BOEING %)));
OR
SQL>SELECT DISTINCT ENAME FROM EMPLOYEES E
2 WHERE E.EID IN (SELECT C.EID FROM CERTIFIED C
3 WHERE C.AID IN(SELECT A.AID FROM AIRCRAFT A
4 WHERE A.AID=C.AID AND A.CRUSHING_RANGE>3000)
5 AND
6 C.AID NOT IN (SELECT A1.AID FROM AIRCRAFT A1
7 WHERE A1.AID=C.AID A1.ANAME LIKE BOEING%));
ENAME
------------------ANGELA MARTINEZ
BETTY ADAMS
ERIC COOPER
GEORGE WRIGHT
JOSEPH THOMPSON
LAWRENCE SPERRY
LISA WALKER
MARY YOUNG
WILLIAM JONES
57. FIND THE TOTAL AMOUNT PAID TO EMPLOYEES AS SALARIES
SQL>SELECT SUM (E.SALARY) FROM EMPLOYEES E;
SUM(SALARY)
------------4312544

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


48 -

58.FIND THE EIDS OF EMPLOYEES WHO ARE CERTIFIED FOR EXACTLY


THREE
AIRCRAFTS
SQL>SELECT EID,COUNT(EID) FROM CERTIFIED GROUP BY EID HAVING
COUNT(EID)=3 ORDER BY EID;
EID
COUNT(EID)
-------------------------------141582651
3
390487451
3
552455318
3
556784565
3
573284895
3
59.FIND THE EIDS OF EMPLOYEE WHO MAKE SECOND HIGHEST SALARY
SQL>SELECT E.EID FROM EMPLOYEES E WHERE E.SALARY=
2 (SELECT MAX(E2.SALARY) FROM EMPLOYEES E2 WHERE E2.SALARY<
3(SELECT MAX(E3.SALARY) FROM EMPLOYEES E3));
OR
SQL>SELECT A.EID FROM EMPLOYEES A WHERE 2=
2 (SELECT COUNT(DISTINCT(B.SALARY)) FROM EMPLOYEES B
3 WHERE A.SALARY<=B.SALARY);
OR
SQL>SELECT EID FROM EMPLOYEES WHERE SALARY=(SELECT
2 MAX(SALARY) FROM EMPLOYEES WHERE SALARY<(SELECT
3 MAX (SALARY) FROM EMPLOYEES));
EID
--------269734834
60.FIND THE AIDS OF ALL THAN CAN BE USED ON NON-STOP FLIGHTS
FROM
LOSANGELES TO NEW YARK.
SQL>SELECT A.AID FROM AIRCRAFT A,FLIGHTS F
2 WHERE F.ORIGIN=LOS ANGELES AND F.DESTINATION=NEW YARK
3 AND A.CRUSHING_RANGE>F.DISTANCE;
no rows selected

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


49 -

LABCYCLE-IV

Cycle-IV: Employee Database


Aim: An enterprise wishes to maintain a database to automate its operations. Enterprise is
divided into two departments and each department consists of employees. The following two
tables describe the automation schemas.
DEPT(DEPTNO, DNAME, LOC)
EMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
SQL> CREATE TABLE DEPT(
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
50 -

2 DEPTNO NUMBER(2) PRIMARY KEY,


3 DNAME
VARCHAR2(14),
4 LOC
VARCHAR2(13) );
Table created.
SQL> DESC DEPT
Name
Null?
Type
----------------------------------------------------DEPTNO
NOT NULL
NUMBER(2)
DNAME
VARCHAR2(14)
LOC
VARCHAR2(13)
SQL> CREATE TABLE EMP(
2 EMPNO
NUMBER(4)
PRIMARY KEY,
3 ENAME
VARCHAR2(10),
4 JOB
VARCHAR2(9),
5 MGR
NUMBER(4)
REFERENCES EMP(EMPNO),
6 HIREDATE
DATE,
7 SAL
NUMBER(7,2),
8 COMM
NUMBER(7,2),
9 DEPTNO
NUMBER(2)
REFERENCES DEPT(DEPTNO));
Table created.
SQL> DESC EMP;
Name
Null?
----------------------------------------------------EMPNO
NOT NULL
ENAME
JOB
MGR
HIREDATE
SAL
COMM
DEPTNO

Type
NUMBER(4)
VARCHAR2(10)
VARCHAR2(9)
NUMBER(4)
DATE
NUMBER(7,2)
NUMBER(7,2)
NUMBER(2)

INSERTION OF DATA INTO THE TABLES


TABLE NAME: DEPT
SQL> INSERT INTO DEPT VALUES(&DEPTNO,'&DNAME','&LOC');
Enter value for deptno: 10
Enter value for dname: ACCOUNTING
Enter value for loc: NEWYORK
old 1: INSERT INTO DEPT VALUES(&DEPTNO,'&DNAME','&LOC')
new 1: INSERT INTO DEPT VALUES(10,ACCOUNTING,NEWYORK)
1 row created.
SQL> INSERT INTO DEPT VALUES(&DEPTNO,'&DNAME','&LOC');
Enter value for deptno: 20
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
51 -

Enter value for dname: RESEARCH


Enter value for loc: DALLAS
old 1: INSERT INTO DEPT VALUES(&DEPTNO,'&DNAME','&LOC')
new 1: INSERT INTO DEPT VALUES(20,'RESEARCH','DALLAS')
1 row created.
SQL> INSERT INTO DEPT VALUES(&DEPTNO,'&DNAME','&LOC');
Enter value for deptno: 30
Enter value for dname: SALES
Enter value for loc: CHICAGO
old 1: INSERT INTO DEPT VALUES(&DEPTNO,'&DNAME','&LOC')
new 1: INSERT INTO DEPT VALUES(30,'SALES','CHICAGO')
1 row created.
SQL> INSERT INTO DEPT VALUES(&DEPTNO,'&DNAME','&LOC');
Enter value for deptno: 40
Enter value for dname: OPERATIONS
Enter value for loc: BOSTON
old 1: INSERT INTO DEPT VALUES(&DEPTNO,'&DNAME','&LOC')
new 1: INSERT INTO DEPT VALUES(40,'OPERATIONS','BOSTON')
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM DEPT;
DEPTNO
10
20
30
40

DNAME
ACCOUNTING
RESEARCH
SALES
OPERATIONS

LOC
NEWYORK
DALLAS
CHICAGO
BOSTON

TABLE NAME: EMP


SQL> INSERT INTO EMP VALUES(
2 &EMPNO,'&ENAME','&JOB',&MGR,'&HIREDATE',&SAL,&COMM,&DEPTNO);
Enter value for empno: 7369
Enter value for ename: SMITH
Enter value for job: CLERK
Enter value for mgr: 7902
Enter value for hiredate: 17-DEC-80
Enter value for sal: 800
Enter value for comm: NULL
Enter value for deptno: 20
old 2: &EMPNO,'&ENAME','&JOB',&MGR,'&HIREDATE',&SAL,&COMM,&DEPTNO)
new 2: 7369,'SMITH','CLERK',7902,'17-DEC-80',800,NULL,20)
1 row created.
Insert remaining rows into the table as shown above.
SQL> COMMIT;
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
52 -

Commit complete.
SQL> SELECT * FROM EMP;
EMPNO
7369
7499
7521
7566
7654
7698
7782
7788
7839
7844
7876
7900
7902
7934

ENAME
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER

JOB
CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER
MANAGER
ANALYST
PRESIDENT
SALESMAN
CLERK
CLERK
ANALYST
CLERK

MGR
7902
7698
7698
7839
7698
7839
7839
7566
7698
7788
7698
7566
7782

HIREDATE
17-DEC-80
20-FEB-81
22-FEB-81
02-APR-81
28-SEP-81
01-MAY-81
09-JUN-81
19-APR-87
17-NOV-81
08-SEP-81
23-MAY-87
03-DEC-81
03-DEC-81
23-JAN-82

SAL
800
1600
1250
2975
1250
2850
2450
3000
5000
1500
1100
950
3000
1300

COMM
300
500
1400

DEPTNO
20
30
30
20
30
30
10
20
10
30
20
30
20
10

14 rows selected.

QUERIES BASED ON ABOVE TABLES


1. Create a view, which contain employee names and their manager
names working in sales department.
SQL> CREATE OR REPLACE VIEW SALESMAN(ENAME, MGRNAME)
AS SELECT E.ENAME, M.ENAME
FROM EMP E, EMP M
WHERE E.MGR = M.EMPNO AND E.DEPTNO IN
(SELECT DEPTNO FROM DEPT WHERE DNAME='SALES');
View created.
SQL> SELECT * FROM SALESMAN;
ENAME
MGRNAME
-----------------BLAKE
KING
MARTIN BLAKE
TURNER BLAKE
JAMES
BLAKE
WARD
BLAKE
ALLEN
BLAKE

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


53 -

6 rows selected.
2. Determine the employee names, who earn more than their
managers.
SQL> SELECT E.ENAME "EMPLOYEE", E.SAL, M.ENAME "MANAGER", M.SAL
FROM EMP E, EMP M
WHERE E.MGR=M.EMPNO
AND E.SAL>M.SAL;
EMPLOYEE
SAL MANAGER
------------------------------------------SCOTT
3000 JONES
2975
FORD
3000 JONES
2975

SAL

3. Determine the employee names, who take highest salary in


their departments.
SQL> SELECT ENAME, DEPTNO, SAL
FROM EMP
WHERE SAL IN (SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO);
ENAME
DEPTNO
SAL
------------------------------KING
10
5000
FORD
20
3000
SCOTT
20
3000
BLAKE
30
2850
4. Determine the count of employees, who are located at the same
place.
SQL> SELECT LOC, COUNT(*) "EMP COUNT"
FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO
GROUP BY LOC;
LOC
EMP COUNT
-----------------------CHICAGO
6
DALLAS
5
NEW YORK
3
5. Determine the employees, whose total salary is like the
minimum salary of any department.
SQL> SELECT DEPTNO, ENAME, SAL FROM EMP
WHERE SAL IN (SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO);
DEPTNO
ENAME
-------------------------------10
MILLER
20
SMITH
30
JAMES

SAL
1300
800
950

6. Update the employee salary by 25%, whose experience is greater


than 10 years.
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
54 -

SQL> SELECT ENAME, SAL, ROUND((SYSDATE-HIREDATE)/365) EXP


FROM EMP
WHERE ROUND((SYSDATE-HIREDATE)/365)>10;
ENAME
SAL
EXP
-------------------------------SMITH
800
30
ALLEN
1600
30
WARD
1250
30
JONES
2975
30
MARTIN
1250
30
BLAKE
2850
30
CLARK
2450
30
SCOTT
3000
24
KING
5000
29
TURNER
1500
30
ADAMS
1100
24
JAMES
950
29
FORD
3000
29
MILLER
1300
29
14 rows selected.
SQL> UPDATE EMP SET SAL = ROUND(SAL+SAL*25/100)
WHERE ROUND((SYSDATE-HIREDATE)/365)>10;
14 rows updated.
SQL> SELECT ENAME, SAL, ROUND((SYSDATE-HIREDATE)/365) EXP
FROM EMP;
ENAME
SAL
EXP
-------------------------------SMITH
1000
30
ALLEN
2000
30
WARD
1563
30
JONES
3719
30
MARTIN
1563
30
BLAKE
3563
30
CLARK
3063
30
SCOTT
3750
24
KING
6250
29
TURNER
1875
30
ADAMS
1375
24
JAMES
1188
29
FORD
3750
29
MILLER
1625
29
14 rows selected.
7. Delete the employees, who completed 32 years of service.
SQL> SELECT ENAME, ROUND((SYSDATE-HIREDATE)/365) EXP
FROM EMP;

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


55 -

ENAME
EXP
--------------------SMITH
30
ALLEN
30
WARD
30
JONES
30
MARTIN
30
BLAKE
30
CLARK
30
SCOTT
24
KING
29
TURNER
30
ADAMS
24
JAMES
29
FORD
29
MILLER
29
14 rows selected.
SQL> DELETE FROM EMP WHERE ROUND((SYSDATE-HIREDATE)/365) > 32;
0 rows deleted.
8. Determine the minimum salary of an employee and his details,
who join on the same date.
9. Determine the count of employees, who are taking commission
and not taking commission.
SQL> SELECT COUNT(*) EMP_COMM, 14-COUNT(*) EMP_NOCOMM FROM EMP
WHERE COMM IS NOT NULL;
EMP_COMM EMP_NOCOMM
---------- ---------4
10
10. Trace out the department name which doesnt contain any
employees.
SQL> SELECT DEPTNO, DNAME
FROM DEPT
WHERE DEPTNO NOT IN (SELECT DEPTNO FROM EMP);
DEPTNO
DNAME
--------------------40
OPERATIONS
11. Find out the details of top 5 earners of company. (Note:
Employee Salaries should be distinct).
SQL> SELECT DISTINCT(SAL), JOB FROM EMP E
WHERE 5 >= (SELECT COUNT(*) FROM EMP WHERE E.SAL<SAL)
ORDER BY SAL DESC;
SAL

JOB
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
56 -

--------------5000 PRESIDENT
3000 ANALYST
2975 MANAGER
2850 MANAGER
2450 MANAGER
12. Display the manager names whose salary is more than
average salary of his employees.
SQL> SELECT ROUND(AVG(SAL)) AVERAGE_SAL FROM EMP;
AVERAGE_SAL
----------2073
SQL> SELECT E.ENAME EMPNAME, M.ENAME MGRNAME, E.SAL SAL
FROM EMP E, EMP M
WHERE M.EMPNO = E.MGR
AND E.SAL>(SELECT AVG(SAL) FROM EMP);
EMPNAME MGRNAME
---------- ---------- ---------JONES
KING
2975
BLAKE
KING
2850
CLARK
KING
2450
SCOTT JONES
3000
FORD
JONES
3000

SAL

13. Display the names of the managers who is having maximum


number of employees working under him.
SQL> SELECT MGR, COUNT(*) TOTAL FROM EMP GROUP BY MGR HAVING
COUNT(*) =(SELECT MAX(COUNT(*)) FROM EMP GROUP BY MGR);
MGR
---------------7698

TOTAL
5

14. In which year did most people join the company? Display the
year and number of employees.
SQL> SELECT TO_CHAR(HIREDATE,'YEAR') YEAR, COUNT(*) EMP_COUNT
FROM EMP GROUP BY TO_CHAR(HIREDATE,'YEAR') HAVING COUNT(*) IN
(SELECT MAX(COUNT(*)) FROM EMP GROUP BY
TO_CHAR(HIREDATE,'YEAR'));
YEAR
-----------------------------------NINETEEN EIGHTY-ONE

EMP_COUNT
10

15. Display employee name, department name even if there no


employees working in a particular department(use outer join).

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


57 -

SQL> SELECT ENAME, DNAME FROM EMP RIGHT OUTER JOIN DEPT ON
EMP.DEPTNO = DEPT.DEPTNO;

ENAME
DNAME
--------------------CLARK ACCOUNTING
KING
ACCOUNTING
MILLER ACCOUNTING
SMITH
RESEARCH
ADAMS
RESEARCH
FORD
RESEARCH
SCOTT RESEARCH
JONES
RESEARCH
ALLEN
SALES
BLAKE
SALES
MARTIN SALES
JAMES
SALES
TURNER SALES
WARD
SALES
OPERATIONS
15 rows selected.

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


58 -

LAB CYCLE-V

PL/SQL PROGRAMS
1. Write a program to fetch salary from emp table where empno is 7369.
SQL> set serveroutput on
declare
eno emp.empno %type;
salary emp.sal %type;
begin
eno:='&empno';
select sal into salary from emp where empno=eno;
dbms_output.put_line('The salary of employee with empno '|| eno ||' is '||salary);
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
59 -

end;
Testcase(s):
Enter value for empno: 7369
old 5: eno:='&empno';
new 5: eno:='7369';
The salary of employee with empno 7369 is 800
PL/SQL procedure successfully completed.
2. Write a program to fetch salary and empname from emp table for the given user
input (i.e. given empno) when no data found raise an exception.
declare
empsal emp.sal %type;
ename1 emp.ename %type;
begin
select ename,sal into ename1, empsal from emp where
empno=&EmployeeNumber;
dbms_output.put_line(' The employee name is '|| ename1||' and salary is '||
empsal);
exception
when no_data_found then
dbms_output.put_line('No record found with given Employee Number.');
end;
Testcase(s):
Enter value for employeenumber: 1234
old 5: select ename,sal into ename1, empsal from emp where
empno=&EmployeeNumber;
new 5: select ename,sal into ename1, empsal from emp where empno=1234;
No record found with given Employee Number
Enter value for employeenumber: 7499
old 5: select ename,sal into ename1, empsal from emp where
empno=&EmployeeNumber;
new 5: select ename,sal into ename1, empsal from emp where empno=7499;
The employee name is ALLEN and salary is 1600.
PL/SQL procedure successfully completed.
CURSORS
Explicit Cursors:
3. Write a program to fetch all the data from a table and display it on the screen using
% rowtype attribute.
declare
cursor c2 is select * from dept;
drec dept %rowtype;
begin
dbms_output.put_line('The records of Department table are: ');
open c2;
loop
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
60 -

fetch c2 into drec;


exit when (c2%notfound);
dbms_output.put_line('DeptNo: '||drec.deptno||', Dname: '||
rpad(drec.dname,10,' ')||', Location: '||drec.loc);
end loop;
close c2;
end;
The records of Department table are:
DeptNo: 10, Dname: ACCOUNTING, Location: NEW YORK
DeptNo: 20, Dname: RESEARCH, Location: DALLAS
DeptNo: 30, Dname: SALES, Location: CHICAGO
DeptNo: 40, Dname: OPERATIONS, Location: BOSTON
PL/SQL procedure successfully completed.
4. Display the names, department names and salary of the first 10 employees getting
the highest salary using % rowcount attribute.
[%ROWCOUNT: returns the number of rows fetched from the active set. It is set
to zero when the cursor is opened.]
declare
cursor c_emp is select ename, dname, sal from emp, dept where dept.deptno =
emp.deptno
order by sal desc;
empname emp.ename%type;
deptname dept.dname%type;
salary emp.sal%type;
begin
open c_emp;
dbms_output.put_line('EmpName Department
salary ');
dbms_output.put_line('-------------------------------------');
loop
fetch c_emp into empname, deptname, salary;
dbms_output.put_line(rpad(empname,10,' ') || ' ' || rpad(deptname,15,' ')||'
'||salary);
exit when c_emp%rowcount =10;
end loop;
end;
EmpName
Department
salary
----------------------------------------------------KING
ACCOUNTING
5000
FORD
RESEARCH
3000
SCOTT
RESEARCH
3000
JONES
RESEARCH
2975
BLAKE
SALES
2850
CLARK
ACCOUNTING
2450
ALLEN
SALES
1600
TURNER
SALES
1500
MILLER
ACCOUNTING
1300
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
61 -

MARTIN

SALES

1250

PL/SQL procedure successfully completed.


5. Display the employee numbers, names and their department numbers who belong to
the
department numbet 10 using %rowtype attribute.
declare
cursor c_emp is select * from emp where deptno=10;
e emp%rowtype;
begin
open c_emp;
dbms_output.put_line('empno ename deptno');
dbms_output.put_line('---------------------------');
loop
fetch c_emp into e;
exit when c_emp%notfound;
dbms_output.put_line(rpad(e.empno,8,' ')||' '||rpad(e.ename,10,' ')||' '||e.deptno);
end loop;
close c_emp;
end;
empno ename deptno
-------------------------------7782
CLARK
10
7839
KING
10
7934
MILLER
10
PL/SQL procedure successfully completed.
Implicit Cursors:
6. Write a program to update the salary of employees working as Programmers by
0.15 and display
appropriate message based on the existence of the record in the emp table.
[%ROWCOUNT: returns the number of rows affected by an insert, update or
delete, or select into statement.]
declare
rows_affected char(4);
begin
update emp set sal = sal * 0.15 where job='Programmers';
rows_affected := to_char(sql%rowcount);
if sql%rowcount > 0 then
dbms_output.put_line(rows_affected || 'Employee records modified
successfully ');
else
dbms_output.put_line('There are no employees working as
programmers');
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
62 -

end if;
end;
There are no employees working as programmers
PL/SQL procedure successfully completed.
STORED PROCEDURES AND FUNCTIONS
7. Write a program to display ename, sal when user inputs empno using in/out
parameters.
--Creating the procedure
create or replace procedure emp_details (eno in emp.empno%type, en out emp.ename
%type, esal out emp.sal%type)
as
begin
select ename,sal into en, esal from emp where empno=eno;
exception
when no_data_found then
dbms_output.put_line('Record not found.');
end;
--Using the procedure
declare
eno emp.empno%type:=&EmployeeNo;
en emp.ename%type;
esal emp.sal%type;
begin
emp_details(eno,en,esal);
dbms_output.put_line('The employee name is '||en||' and the salary is '||esal);
end;
Testcase(s):
Enter value for employeeno: 7499
old 2: eno emp.empno%type:=&EmployeeNo;
new 2: eno emp.empno%type:=7499;
The employee name is ALLEN and the salary is 1600
PL/SQL procedure successfully completed.
8. Write a procedure to update sal of emp table taking empno as in parameter.
create or replace procedure Upd_Sal (eno in number)
as
begin
update emp set sal=sal+sal*0.01 where empno=eno;
if sql%rowcount>0 then
Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU
63 -

dbms_output.put_line('The salary of empno '||eno||' is increased by 10%');


else
dbms_output.put_line('The given empno no does not exist in emp table');
end if;
commit;
end;
Testcase(s): Execute the procedure using exec command
exec upd_sal(7499);
The salary of empno 7499 is increased by 10%.
The change in the salary value can also be noticed by using the following query.
select * from emp where empno=7499;
exec upd_sal(8765);
The given empno no does not exist in emp table.

Lab Cycle-I, DBMS Lab Record, Dept. of Computer Applications, ANU


64 -