0% found this document useful (0 votes)
34 views26 pages

Dbms Complete Record

The document outlines a series of RDBMS lab programs for a B.Sc CS course, detailing the creation and manipulation of various database tables such as customer, sales_man, and hospital. It includes tasks like inserting records, modifying table structures, applying constraints, and performing queries to extract specific data. The document serves as a comprehensive guide for students to practice SQL commands and database management concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views26 pages

Dbms Complete Record

The document outlines a series of RDBMS lab programs for a B.Sc CS course, detailing the creation and manipulation of various database tables such as customer, sales_man, and hospital. It includes tasks like inserting records, modifying table structures, applying constraints, and performing queries to extract specific data. The document serves as a comprehensive guide for students to practice SQL commands and database management concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Fourth semester B.

Sc CS
Part B: RDBMS LAB PROGRAMS
1. Create a table customer (cust_no varchar (5), cust_name varchar (15), age
number, phone varchar (10))
a) insert 5 records and display it
b) add new field d_birth with date datatype
c) create another table cust_phone with fields cust_name and phone from customer
table
d) remove the field age
e) change the size of the cust_name to 25
f) delete all the records from the table
g) rename the table cutomer to cust
h) drop the table
2. Create a table sale_man ( salesman_no primary key, s_name not null, place,
phone unique)
Create table sales_order (order_no primary key order_date not null
salesman_no foreign key references salesman_no in sales_man
del_type values should be either P or F (check constraints)
order_status values should be 'Inprocess','Fullfilled','Backorder', 'Cancelled' (check
constraints))
a) Insert few records in both tables
b) Delete primary key from sales_man table
c) Delete Foreign key and Check constraints from sales_order table
d) Add primary key in sales_man using ALTER TABLE
e) Add foreign key and CHECK constraints in sales_order table using ALTER
TABLE
3. Create a table Hospital with the fields
(doctorid,doctorname,department,qualification,experience).
Write the queries to perform the following.
a) Insert 5 records
b) Display the details of Doctors
c) Display the details of doctors who have the qualification ‘MD’
d) Display all doctors who have more than 5 years experience but do not have the
qualification ‘MD’
e) Display the doctors in ‘Skin’ department
f) update the experience of doctor with doctored=’D003’ to 5
g) Delete the doctor with DoctorID=’D005’
4. Create the following tables
Bank_customer (accno primary key, cust_name, place)
Deposit (accno foreign key, deposit_no, damount)
Loan (accno foreign key loan_no, Lamount)
Write the following queries
a) Display the details of the customers
b) Display the customers along with deposit amount who have only deposit with
the bank
c) Display the customers along with loan amount who have only loan with the
bank
d) Display the customers they have both loan and deposit with the bank
e) Display the customer who have neither a loan nor a deposit with the bank
5. Create a table employee with fields (EmpID, EName, Salary, Department, and
Age).
Insert some records. Write SQL queries using aggregate functions and group by
clause
A. Display the total number of employees.
B. Display the name and age of the oldest employee of each department.
C. Display the average age of employees of each department
D. Display departments and the average salaries
E. Display the lowest salary in employee table
F. Display the number of employees working in purchase department
G. Display the highest salary in sales department;
H. Display the difference between highest and lowest salary
6. Create a table product with the fields (Product_code primary key,
Product_Name, Category, Quantity, Price).
Insert some records Write the queries to perform the following.
a. Display the records in the descending order of Product_Name
b. Display Product_Code, Product_Name with price between 20 and 50
c. Display the details of products which belongs to the categories of ‘bath soap’,
‘paste’, or ‘washing powder’
d. Display the products whose Quantity less than 100 or greater than 500
e. Display the products whose names starts with 's'
f. Display the products which not belongs to the category 'paste'
g. Display the products whose second letter is 'u' and belongs to the Category
'washing powder'
7. Consider the employee database given below. Give an expression in SQL for
each of
the following queries:
EMPLOYEE (Employee-Name, City)
WORKS (Employee-Name, Company-Name, Salary)
COMPANY (Company-Name, City)
MANAGES (Employee-Name, Manager-Name)
A) Find the names of all employees who work in Infosys
B) Find the names and cities of residence of all employees who works in Wipro
C) Find the names, and cities of all employees who work in Infosys and earn more
than Rs. 10,000.
D) Find the employees who live in the same cities as the companies for which they
work.
E) Find all employees who do not work in Wipro Corporation.
F) Find the company that has the most employees.
8. Write a program code to calculate the area of a circle for a value of radius
varying from 3 to 7. Store the radius and the corresponding value of calculated area
in an empty table named areas with field’s radius and area.
9. Write a program block to calculate the electricity bill by accepting cust_no and
units_consumed
10. Create a procedure to print Fibonacci number up to a limit, limit is passed as an
argument
11. Create a function to check whether a given number is prime or not
12. create a table emp_salary(empno,enamedept,salary)
Write a function to return the average salary of a particular department by
accepting departmentname as argument.
Program 1: CUSTOMER DETAILS

Create a table customer (cust_no varchar (5), cust_name varchar (15), age
number, phone varchar (10))
A) insert 5 records and display it
B) add new field d_birth with date datatype
C) create another table cust_phone with fields cust_name and phone from
customer table
D) remove the field age
E) change the size of the cust_name to 25
F) delete all the records from the table
G) rename the table cutomer to cust
H) drop the table

create table CUSTOMER(CUST_NO NUMBER(5),CUST_NAME VARCHAR(10),AGE


NUMBER(3),PHONE NUMBER(10));

desc customer;
Lengt Precisi Scal Primary Nullab Defau Comme
Table Column Data Type h on e Key le lt nt

CUSTOME
CUST_NO NUMBER - 5 0 - - -
R
CUST_NA VARCHA
10 - - - - -
ME R2

AGE NUMBER - 3 0 - - -

PHONE NUMBER - 10 0 - - -

D_BIRTH DATE 7 - - - - -

1-5

A.) insert into customer values(1,'devi',25,9874561230);


insert into customer values(2,'devi',25,9874561230);
insert into customer values(3,'devi',25,9874561230);
insert into customer values(4,'devi',25,9874561230);
insert into customer values(5,'devi',25,9874561230);

select * from customer;

CUST_NO CUST_NAME AGE PHONE D_BIRTH

1 devi 25 9874561230 -
2 devi 25 9874561230 -
3 devi 25 9874561230 -
4 devi 25 9874561230 -
5 devi 25 9874561230 -

B). alter table customer add d_birth date;


desc customer;
Data Lengt Precisio Scal Primary Nullabl Defau Comme
Table Column Type h n e Key e lt nt

CUSTOM
CUST_NO NUMBER - 5 0 - - -
ER
CUST_NA VARCHA
10 - - - - -
ME R2
AGE NUMBER - 3 0 - - -
PHONE NUMBER - 10 0 - - -
D_BIRTH DATE 7 - - -

C). create table cust_phone as (select cust_name,phone from customer);


desc cust_phone;

Lengt Precisio Scal Primary Nullabl Defau Comme


Table Column Data Type h n e Key e lt nt

CUST_NA VARCHA
CUST_PHONE 10 - - - - -
ME R2
PHONE NUMBER - 10 0 - - -

1-2

D). alter table customer drop column age;


desc customer;

Data Lengt Precisi Scal Primary Nullab Defau Comme


Table Column Type h on e Key le lt nt

CUSTOM
CUST_NO NUMBER - 5 0 - - -
ER
CUST_NA VARCHA
10 - - - - -
ME R2
PHONE NUMBER - 10 0 - - -
D_BIRTH DATE 7 - - -

E). alter table customer modify column cust_name varchar25);


Desc customer;

Data Lengt Precisio Scal Primary Nullabl Defau Comme


Table Column Type h n e Key e lt nt

CUSTOM
CUST_NO NUMBER - 5 0 - - -
ER
CUST_NA VARCHA
25 - - - - -
ME R2
PHONE NUMBER - 10 0 - - -
D_BIRTH DATE 7 - - -

F.) DELETE CUSTOMER;

G). alter table customer rename to cust;


Desc cust;
Lengt Precisio Scal Primary Nullabl Defaul Commen
able Column Data Type h n e Key e t t

CUS
CUST_NO NUMBER - 5 0 - - -
T
CUST_NAM VARCHAR
25 - - - - -
E 2
PHONE NUMBER - 10 0 - - -
D_BIRTH DATE 7 - - - - -

Dbms lab program 2: sales man details

13. Create a table sale_man ( salesman_no primary key, s_name not null,
place, phone unique)
Create table sales_order (order_no primary key order_date not null
salesman_no foreign key references salesman_no in sales_man
del_type values should be either P or F (check constraints)
order_status values should be 'Inprocess','Fullfilled','Backorder', 'Cancelled' (check
constraints))
a. Insert few records in both tables
b. Delete primary key from sales_man table
c. Delete Foreign key and Check constraints from sales_order table
d. Add primary key in sales_man using ALTER TABLE
e. Add foreign key and CHECK constraints in sales_order table using ALTER
TABLE
create table sales_man(salesman_no number(10) ,s_name varchar(20) not null,place varchar(10), phone
number(10) unique, constraint pk_saleman primary key(salesman_no));

desc sales_man;
Data Leng Precisi Sca Primary Nulla Defa Comm
Table Column Type th on le Key ble ult ent

SALESMAN NUMBE
SALES_MAN - 10 0 1 - - -
_NO R
VARCHA
S_NAME 20 - - - - - -
R2
VARCHA
PLACE 10 - - - - -
R2
NUMBE
PHONE - 10 0 - - -
R

1-4

create table sales_order(order_no number(10),order_date date not null,salesman_no number(20), del_type


varchar(10),order_status varchar(20),constraint pk_sales_order primary key(order_no),constraint
fk_sales_order foreign key(salesman_no) references sales_man (salesman_no),constraint ck_sales_order
check (del_type='p' or del_type='f'),constraint ck_sales_order2 check (order_status='inprocess' or
order_status='fullfilled' or order_status='backorder' or order_status='cancelled'));
Data Leng Precisi Sca Primary Nulla Defa Comm
Table Column Type th on le Key ble ult ent

SALES_ORD NUMBE
ORDER_NO - 10 0 1 - - -
ER R
ORDER_DAT
DATE 7 - - - - - -
E
SALESMAN_ NUMBE
- 20 0 - - -
NO R
VARCH
DEL_TYPE 10 - - - - -
AR2
ORDER_STA VARCH
20 - - - - -
TUS AR2

1) insert into sales_man values(1,'arun','kerala',1234567890);

insert into sales_man values(2,'ajay','kerala',2134567890);


insert into sales_man values(3,'ajith','tamilnadu',2534567890);
insert into sales_man values(4,'aji','tamilnadu',2534767890);
insert into sales_man values(5,'jis','tamilnadu',2534762890);
select * from sales_man;
SALESMAN_NO S_NAME PLACE PHONE

1 arun kerala 1234567890


2 ajay kerala 2134567890
3 ajith tamilnadu 2534567890
4 aji tamilnadu 2534767890
5 jis tamilnadu 253476289

insert into sales_order values(11,'04/08/2021',1,'p','backorder');


insert into sales_order values(12,'06/08/2021',2,'f','fullfilled');
insert into sales_order values(13,'06/09/2021',3,'p','cancelled');
insert into sales_order values(14,'06/10/2021',4,'p','inprocess');
insert into sales_order values(5,'10/10/2021',5,'f','backorder');
select * from sales_order;
ORDER_NO ORDER_DATE SALESMAN_NO DEL_TYPE ORDER_STATUS

11 04/08/2021 1 p backorder
12 06/08/2021 2 y fullfilled
13 06/09/2021 3 p cancelled
14 06/10/2021 4 p inprocess
5 10/10/2021 5 p backorder

3 alter table sales_order drop constraint fk_sales_order;


Table altered.

alter table sales_order drop constraint ck_sales_order;


Table altered.

alter table sales_order drop constraint ck_sales_order2;


Table altered.

2. alter table sales_man drop constraint pk_saleman;


Table altered.

desc sales_man;
Data Leng Precisi Scal Primary Nullab Defa Comme
Table Column Type th on e Key le ult nt

SALES_M SALESMAN_
NUMBER - 10 0 - - -
AN NO
VARCHA
S_NAME 20 - - - - - -
R2
VARCHA
PLACE 10 - - - - -
R2
PHONE NUMBER - 10 0 -

4. alter table sales_man add constraint pk_saleman primary key(salesman_no);


Table altered.

desc sales_man;
Data Lengt Precisi Scal Primary Nullab Defau Comme
Table Column Type h on e Key le lt nt

SALES_M SALESMAN_
NUMBER - 10 0 1 - - -
AN NO
VARCHA
S_NAME 20 - - - - - -
R2
PLACE VARCHA 10 - - - - -
R2
PHONE NUMBER - 10 0 - -

5. alter table sales_man add constraint pk_sales_man foreign key(salesman_no) references sales_man
(salesman_no);

alter table sales_order add constraint ck_sales_order check (del_type='p' or del_type='f');


alter table sales_order add constraint ck_sales_order2 check (order_status='inprocess' or
order_status='fullfilled' or order_status='backorder' or order_status='cancelled');

dbms lab program 3: doctor details

Create a table Hospital with the fields


(doctorid,doctorname,department,qualification,experience).
Write the queries to perform the following.
h) Insert 5 records
i) Display the details of Doctors
j) Display the details of doctors who have the qualification ‘MD’
k) Display all doctors who have more than 5 years experience but do not have the
qualification ‘MD’
l) Display the doctors in ‘Skin’ department
m) update the experience of doctor with doctored=’D003’ to 5
n) Delete the doctor with DoctorID=’D005’

create table hospital(doctorid varchar(10),doctorname varchar(10),department


varchar(10),qualification varchar(10),experience number(5));

desc hospital;

Data Lengt Precisi Scal Primary Nullab Defau Comme


Table Column Type h on e Key le lt nt

HOSPIT VARCHA
DOCTORID 10 - - - - -
AL R2
DOCTORNAM VARCHA
10 - - - - -
E R2
DEPARTMEN VARCHA
10 - - - - -
T R2
QUALIFICATI VARCHA
10 - - - - -
ON R2
EXPERIENCE NUMBER - 5 0

insert into hospital values('D001', 'visakh','skin','md',5);


insert into hospital values('D002', 'anju','ortho','mbbs',6);
insert into hospital values('D003', 'akhil','skin','mbbs',4);
insert into hospital values('D004', 'binu','dental','md',7);
insert into hospital values('D005', 'jithu','surgen','md',4);

select * from hospital;


DOCTORID DOCTORNAME DEPARTMENT QUALIFICATION EXPERIENCE
D001 visakh skin md 5
D002 anju ortho mbbs 6
D003 akhil skin mbbs 4
D004 binu dental md 7
D005 jithu surgen md 4

select * from hospital where qualification ='md';


DOCTORID DOCTORNAME DEPARTMENT QUALIFICATION EXPERIENCE

D001 visakh skin md 5


D004 binu dental md 7
D005 jithu surgen md 4

select * from hospital where experience>5 and qualification != 'md';


DOCTORID DOCTORNAME DEPARTMENT QUALIFICATION EXPERIENCE

D002 anju ortho mbbs 6

select * from hospital where department='skin';


DOCTORID DOCTORNAME DEPARTMENT QUALIFICATION EXPERIENCE

D001 visakh skin md 5


D003 akhil skin mbbs 4

update hospital set experience =5 where doctorid= 'D003';


1 row(s) updated.

SELECT * FROM HOSPITAL;


DOCTORID DOCTORNAME DEPARTMENT QUALIFICATION EXPERIENCE

D001 visakh skin md 5


D002 anju ortho mbbs 6
D003 akhil skin mbbs 5
D004 binu dental md 7
D005 jithu surgen md 4

PROGRAM 4 bank details


Create the following tables

Bank_customer (accno primary key, cust_name, place)


Deposit (accno foreign key, deposit_no, damount)
Loan (accno foreign key loan_no, Lamount)
Write the following queries?

a) Display the details of the customers


b) Display the customers along with deposit amount who have only
deposit with the
bank
c) Display the customers along with loan amount who have only loan
with the bank
d) Display the customers they have both loan and deposit with the bank
e) Display the customer who have neither a loan nor a deposit with the
bank

create table bank_customer(acc_no int primary key, cust_name


varchar(10), place varchar(15));

desc bank_customer;
Data Leng Precisi Scal Primary Nullab Defa Comme
ble Column Type th on e Key le ult nt

BANK_CUSTO
ACC_NO NUMBER 22 - 0 1 - - -
MER
CUST_NA VARCHA
10 - - - - -
ME R2
VARCHA
PLACE 15 - - - - -
R2

create table deposit (acc_no int references bank_customer(acc_no),


deposit_no int,damount int);

desc deposit;
Data Lengt Precisio Scal Primary Nullabl Defaul Comme
Table Column Type h n e Key e t nt

DEPOSI NUMBE
ACC_NO 22 - 0 - - -
T R
DEPOSIT_N NUMBE
22 - 0 - - -
O R
NUMBE
DAMOUNT 22 - 0 - - -
R

create table loan(acc_no int references bank_customer(acc_no),


loan_no int, l_amount int);

desc loan;
Data Lengt Precisio Scal Primary Nullabl Defaul Commen
Table Column Type h n e Key e t t

LOA
ACC_NO NUMBER 22 - 0 - - -
N
LOAN_NO NUMBER 22 - 0 - - -
L_AMOUN
NUMBER 22 - 0 - -
T

insert into bank_customer values(12344,'Abin','Kannur');


insert into bank_customer values(12345,'Arun','Kozhikode');
insert into bank_customer values(12346,'Athira','Koyilandi');
insert into bank_customer values(12347,'Samuel','Manjeri');
insert into bank_customer values(12348,'Aparna','Aluva');
insert into bank_customer values(12349,'Kiran','Pattambi');

insert into deposit values(12345,100,25000);


insert into deposit values(12346,101,30000);
insert into deposit values(12347,102,300000);

insert into loan values(12345,200,70000);


insert into loan values(12348,201,65000);
insert into loan values(12349,202,35000);

select * from bank_customer;


ACC_NO CUST_NAME PLACE

12344 Abin Kannur


12345 Arun Kozhikode
12346 Athira Koyilandi
12347 Samuel Manjeri
12348 Aparna Aluva
12349 Kiran Pattamb

select * from deposit

ACC_NO DEPOSIT_NO DAMOUNT

12345 100 25000


12346 101 30000
12347 102 300000
select * from loan;

ACC_NO LOAN_NO L_AMOUNT

12345 200 70000


12348 201 65000
12349 202 35000

A). select * from bank_customer;


ACC_NO CUST_NAME PLACE

12344 Abin Kannur


12345 Arun Kozhikode
12346 Athira Koyilandi
12347 Samuel Manjeri
12348 Aparna Aluva
12349 Kiran Pattamb

B). select cust_name,damount from bank_customer,deposit where


bank_customer.acc_no=deposit.acc_no and
not exists(select * from loan where
loan.acc_no=bank_customer.acc_no);
CUST_NAME DAMOUNT

Athira 30000
Samuel 300000

C). select cust_name,l_amount from bank_customer,loan where


bank_customer.acc_no=loan.acc_no and not
exists(select * from deposit where
deposit.acc_no=bank_customer.acc_no);

CUST_NAME L_AMOUNT

Kiran 35000
Aparna 65000

D). select cust_name from bank_customer,deposit,loan where


bank_customer.acc_no=loan.acc_no and
bank_customer.acc_no=deposit.acc_no;
CUST_NAME

Arun

E). select cust_name from bank_customer where acc_no not in(select


acc_no from deposit union select acc_no
from loan);

CUST_NAME

Abin

5. eomployee details

Create a table employee with fields (EmpID, EName, Salary, Department, and
Age).
Insert some records. Write SQL queries using aggregate functions and group by
clause
I. Display the total number of employees.
J. Display the name and age of the oldest employee of each department.
K. Display the average age of employees of each department
L. Display departments and the average salaries
M. Display the lowest salary in employee table
N. Display the number of employees working in purchase department
O. Display the highest salary in sales department;
P. Display the difference between highest and lowest salary

create table employee(empid number(5),empname varchar(10),salary


number(10),department varchar(10),age number(5));
desc employee;
Data Lengt Precisi Scal Primary Nullab Defau Comme
able Column Type h on e Key le lt nt

EMPLOY
EMPID NUMBER - 5 0 - - -
EE
VARCHA
EMPNAME 10 - - - - -
R2
SALARY NUMBER - 10 0 - - -
DEPARTME VARCHA
10 - - - - -
NT R2
AGE NUMBER - 5 0 -

insert into employee values(1,'ajith',25000,'purchase',35);


insert into employee values(2,'bindh',17000,'sales',26);
insert into employee values(3,'nithin',21000,'purchase',23);
insert into employee values(4,'rohan',32000,'sales',42);
insert into employee values(5,'ajith',25000,'purchase',35);
insert into employee values(1,'anitha',22000,'packing',28);
select * from employee;
EMPID EMPNAME SALARY DEPARTMENT AGE

2 bindh 17000 sales 26

3 nithin 21000 purchase 23

1 ajith 25000 purchase 35

4 rohan 32000 sales 42

1 anitha 22000 packing 28

1 select count(*) from employee;

COUNT(*)

2 select empname,age, department from employee where age in(select max(age) from employee
group by department);

EMPNAME AGE DEPARTMENT

Ajith 35 purchase
Rohan 42 Sales
Anitha 28 packing

3 select department,avg(age) from employee group by department;

DEPARTMENT AVG(AGE)

Purchase 29
Sales 34
Packing 28

4. select department,avg(salary) from employee group by department;

DEPARTMENT AVG(SALARY)

Purchase 23000
Sales 24500
Packing 22000
5. select min(salary) from employee;
MIN(SALARY)
17000

6. select count(empid) from employee where department='purchase';

COUNT(EMPID)

7. select max(salary) from employee;


MAX(SALARY)

32000

8. select max(salary)-min(salary) from employee;

MAX(SALARY)-MIN(SALARY)

15000

Dbms program 6 product details

create table product(productcode number(5),productname varchar(10),category


varchar(10),quantity number(10),prince number(10),constraint pk_productcode
primary key(productcode));

desc product;

Data Lengt Precisi Scal Primary Nullab Defau Comme


Table Column Type h on e Key le lt nt

PRODU PRODUCTCO
NUMBER - 5 0 1 - - -
CT DE
PRODUCTNA VARCHA
10 - - - - -
ME R2
VARCHA
CATEGORY 10 - - - - -
R2
QUANTITY NUMBER - 10 0 - - -
PRINCE NUMBER - 10 0 - - -

insert into product values(101,'lux','bathsoap',100,25);


insert into product values(102,'colgate','toothpaste',50,20);
insert into product values(103,'arieal','washpowder',30,45);
insert into product values(104,'sunlight','washpowder',35,50);
insert into product values(105,'santhoor','bathsoap',30,20);
select * from product;
PRODUCTCODE PRODUCTNAME CATEGORY QUANTITY PRINCE

101 Lux bathsoap 100 25


102 Colgate toothpaste 50 20
103 Arieal washpowder 30 45
104 Sunlight washpowder 35 50
105 Santhoor bathsoap 30 20

1. select * from product order by productname desc;


PRODUCTCODE PRODUCTNAME CATEGORY QUANTITY PRINCE

104 Sunlight washpowder 35 50


105 Santhoor bathsoap 30 20
101 Lux bathsoap 100 25
102 Colgate toothpaste 50 20
103 Arieal washpowder 30 45

2. select productcode,productname from product where prince


between 20 and 50;
PRODUCTCODE PRODUCTNAME

101 Lux
102 Colgate
103 Arieal
104 Sunlight
105 Santhoor

3. select * from product where category='bathsoap' or category ='toothpaste' or category


='washpowder';

PRODUCTCODE PRODUCTNAME CATEGORY QUANTITY PRINCE

101 lux bathsoap 100 25


102 colgate toothpaste 50 20
103 arieal washpowder 30 45
104 sunlight washpowder 35 50
105 santhoor bathsoap 30 20
4. select * from product where quantity<100 or quantity>500;

PRODUCTCODE PRODUCTNAME CATEGORY QUANTITY PRINCE

102 colgate toothpaste 50 20


103 arieal washpowder 30 45
104 sunlight washpowder 35 50
105 santhoor bathsoap 30 20

5. select * from product where productname like 's%';


PRODUCTCODE PRODUCTNAME CATEGORY QUANTITY PRINCE

104 sunlight washpowder 35 50


105 santhoor bathsoap 30 20

6. select * from product where category ='toothpaste';

PRODUCTCODE PRODUCTNAME CATEGORY QUANTITY PRINCE

102 colgate toothpaste 50 20

7. select * from product where productname like '_u%' and category ='washpowder';

PRODUCTCODE PRODUCTNAME CATEGORY QUANTITY PRINCE

104 sunlight washpowder 35 50

Dbms PROGRAM 7 : company details

Consider the employee database given below. Give an expression in


SQL for each of the following
queries: EMPLOYEE (Employee-Name, City)
WORKS (Employee-Name, Company-Name, Salary)
COMPANY (Company-Name, City)
MANAGES (Employee-Name, Manager-Name)
A) Find the names of all employees who work in Infosys
B) Find the names and cities of residence of all employees who works
in Wipro
C) Find the names, and cities of all employees who work in Infosys and
earn more than Rs.
10,000.
D) Find the employees who live in the same cities as the companies for
which they work.
E) Find all employees who do not work in Wipro Corporation.
F) Find the company that has the most employees.

create table employee(emp_name varchar(20),city varchar(20));


desc employee;
Lengt Precisio Scal Primary Nullab Defau Comme
able Column Data Type h n e Key le lt nt

EMPLOYE EMP_NA VARCHA


20 - - - - -
E ME R2
VARCHA
CITY 20 - - - - -
R2

create table works(emp_name varchar(20),company_name


varchar(20),salary int);
desc works;

Data Lengt Precisi Scal Primary Nullabl Defau Comme


Table Column Type h on e Key e lt nt

WORK VARCHA
EMP_NAME 20 - - - - -
S R2
COMPANY_NA VARCHA
20 - - - - -
ME R2
SALARY NUMBER 22 - 0 - - -

create table company(company_name varchar(20),city varchar(20));


desc company;
Data Leng Precisi Scal Primary Nullab Defa Comm
Table Column Type th on e Key le ult ent

COMPAN COMPANY_N VARCHA


20 - - - - -
Y AME R2
VARCHA
CITY 20 - - - - -
R2

create table manages(emp_name varchar(20),manager_name


varchar(20));

desc manages;

Data Lengt Precisi Scal Primary Nullab Defau Comme


Table Column Type h on e Key le lt nt

MANAG VARCHA
EMP_NAME 20 - - - - -
ES R2
MANAGER_NA VARCHA
20 - - -
ME R2
insert into employee values('Ajith','Tirur');
insert into employee values('Alex','Tanur');
insert into employee values('Shahina','Manjeri');
insert into employee values('Sindhu','Calicut');
insert into employee values('Balan','Vadakara');

select * from employee;


EMP_NAME CITY

Shahina Manjeri
Ajith Tirur
Alex Tanur
Sindhu Calicut
Balan Vadakara

insert into works values('Ajith','Infosys',25000);


insert into works values('Alex','TCS',35000);
insert into works values('Shahina','Wipro',20000);
insert into works values('Sindhu','Infosys',10000);
insert into works values('Balan','Infosys',15000);

select * from works;

EMP_NAME COMPANY_NAME SALARY

Ajith Infosys 25000


Alex TCS 35000
Shahina Wipro 20000
Sindhu Infosys 10000
Balan Infosys 15000

insert into company values('Infosys','Calicut');


insert into company values('Wipro','Trivandrum');
insert into company values('TCS','Kochi');

select * from company;


COMPANY_NAME CITY

Infosys Calicut
Wipro Trivandrum
TCS Koc
insert into manages values('Ajith','Sachin');
insert into manages values('Alex','Sayooj');
insert into manages values('Shahina','Akhila');
insert into manages values('Sindhu','Kiran');
insert into manages values('Balan','Salman');

select * from manages;


EMP_NAME MANAGER_NAME

Ajith Sachin
Alex Sayooj
Shahina Akhila
Sindhu Kiran
Balan Salman

A). select emp_name from works where company_name='Infosys';

EMP_NAME

Ajith
Sindhu
Balan

B). select employee.emp_name,employee.city from employee,works


where
employee.emp_name=works.emp_name and
works.company_name='Wipro';

EMP_NAME CITY

Shahina Manjeri

C). select employee.emp_name,employee.city from employee,works


where
employee.emp_name=works.emp_name and
works.company_name='Infosys' and salary>10000;

EMP_NAME CITY

Ajith Tirur
Balan Vadakara

D). select employee.emp_name from employee,company where


employee.city=company.city;
EMP_NAME

Sindhu

E). select emp_name from works where company_name!='Wipro';


EMP_NAME

Ajith
Alex
Sindhu
Balan

F). select company_name,count(*) as no_of_employees from works


group by company_name having
count(distinct emp_name)>=all(select count(distinct emp_name) from
works group by company_name)

COMPANY_NAME NO_OF_EMPLOYEES

Infosys 3

PROGRAM 8 : area of a circle


Write a program code to calculate the area of a circle for a value of
radius varying from 3 to 7. Store the
radius and the corresponding value of calculated area in an empty table
named areas with field’s radius and
area.

Query:-
create table areas ( r number(2), area number(5));

declare
r number(5);
area number(14,2);
pi constant number (4,2):=3.14;
begin
r:=3;
while r<=7
loop
area:=pi*power(r,2);
insert into areas values(r,area );
r:=r+1;
end loop;
end;

select * from areas;

Output:-
R AREA

3 28
4 50
5 79
6 113
7 154

PROGRAM 9 electricity bill


Write a program block to calculate the electricity bill by accepting
cust_no and units_consumed.
Query:-
declare
cno number;
unit number;
amount number;
begin
cno:=:cno;
unit:=:unit;
if unit>0 and unit<=50 then
amount:=unit*1.5;
elsif unit>50 and unit<=100 then
amount:=unit*2;
elsif unit>100 and unit<=200 then
amount:=unit*2.8;
elsif unit>200 and unit<=300 then
amount:=unit*3.5;
else
amount:=unit*4.5;
end if;
dbms_output.put_line('billamount '||amount);
end;

Output:-

CNO 11

:UNIT 50

billamount 75

Statement processed.
PROGRAM 10 Fibonacci series
Create a procedure to print Fibonacci number up to a limit, limit is passed as
an argument

Query:-
declare
a number(3):=0;
b number(3):=1;
c number(3):=0;
n number(3);
procedure fibo(n number)
as begin
dbms_output.put_line('the Fibonacci series');
while(c<=n)
loop
dbms_output.put_line(c);
a:=b;
b:=c;
c:=a+b;
end loop;
end;
begin
n:=:n;
fibo(n);
end;

Output:-
5
N
the Fibonacci series
0
1
1
2
3
5

PROGRAM 11 prime number


Create a function to check whether a given number is prime or not
Query:-

declare
get_value number;
function checkprime(no in number)
return varchar is
ans varchar(25);
n number;
begin
if no = 2 then
ans:='given number is prime';
else
n:= no/2;
for i in 2..n
loop
if(mod(no,i)=0) then
ans:='given number is not prime';
exit;
else
ans:='given number is prime';
end if;
end loop;
end if;
return(ans);
end;
begin
get_value:=:get_value;
dbms_output.put_line(checkprime(get_value));
end;

7
GET_VALUE

given number is prime

Statement processed

PROGRAM 12 employee details


create a table emp_salary(empno,ename dept,salary);

Write a function to return the average salary of a particular department


by accepting department
name as argument.
Table structure:-
Attribute Datatype Constraint
empno int Primary key
ename varchar(25)
dept varchar(25)
salary int

create table emp_salary(empno varchar(10),ename varchar(20),dept varchar(20),salary number);


insert into emp_salary values('e001','anu','finaance',20000);
insert into emp_salary values('e002','vinu','finaance',25000);
insert into emp_salary values('e003','ajith','marketing',20000);
select * from emp_salary;
declare
dname varchar(20);
function avg_salary(dname varchar)
return number
is
avg_sal number;
begin
select avg(salary) into avg_sal from emp_salary group by dept having dept=dname;
return avg_sal;
end;
begin
dname:=:dname;
dbms_output.put_line('avgsaalry'|| avg_salary(dname));
end;

You might also like