0% found this document useful (0 votes)
5 views44 pages

Rdbms pgms-1

The document outlines several SQL programs for managing customer, sales, hospital, bank, employee, product, and company details. Each program includes the creation of tables, insertion of records, and various SQL queries to manipulate and retrieve data. The document provides a comprehensive guide for performing database operations using SQL commands.

Uploaded by

mohdshahal1111
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)
5 views44 pages

Rdbms pgms-1

The document outlines several SQL programs for managing customer, sales, hospital, bank, employee, product, and company details. Each program includes the creation of tables, insertion of records, and various SQL queries to manipulate and retrieve data. The document provides a comprehensive guide for performing database operations using SQL commands.

Uploaded by

mohdshahal1111
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/ 44

Program 1 : Customer Details

AIM
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

TABLE DESIGN

QUERIES
create table customer(cust_no varchar(5),cust_name varchar(15),age numeric,phone varchar(10));

a)

insert into customer values(1,'Raju',23,9495823456);


insert into customer values(2,'Zara',23,9447132324);
insert into customer values(3,'Adam',34,8089123456);
insert into customer values(4,'Sheena',23,7259123456);
insert into customer values(5,'Lisa',23,8891223344);
select *from customer;

b)
alter table customer add d_birth date;
\d customer;

c)
create table cust_phone as select cust_name,phone from customer;
select *from cust_phone;

cust_name | phone
+
Raju | 9495823456
Zara | 9447132324
Adam | 8089123456
Sheena | 7259123456
Lisa | 8891223344
(5 rows)

d)
alter table customer drop age;
\d customer;
e)ALTER TABLE customer ALTER COLUMN cust_name TYPE varchar(25);
\d customer;

f)
TRUNCATE customer;
select *from customer;

g)
ALTER TABLE customer RENAME TO cust;
\d cust;

h)
drop table cust;

Result:
SQL query successfully executed.
Program 2 : Sales Details
AIM
Create a table sales_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 val-
ues should be 'Inprocess','Fullfilled','Backorder', or '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

TABLE DESIGN
QUERIES
create table sales_man(salesman_no int primary key,s_name varchar(10) not null,place var-
char(10),phone numeric(10) unique);

create table sales_order(order_no int primary key,order_date date NOT NULL,order_status char(10) NOT
NULL check(order_status IN('Inprocess', 'Fullfilled', 'Cancelled', 'Backorder')), salesman_no int references
sales_man (salesman_no), del_type char(1) check (del_type='F' or del_type='P'));

a)
insert into sales_man values (101, 'ananthu', 'feroke', 8137036211);
insert into sales_man values (102, 'fariz', 'chungam', 8137036231);
insert into sales_man values( 103, 'sheena', 'chelari', 9997036231);
insert into sales_man values(104, 'asla', 'chelari', 9687036231);
insert into sales_man values(105, 'jithin', 'tanur', 9687035671);

select*from sales_man;

insert into sales_order values(1,'01-01-17', 'Inprocess',101,'F'),(2,'03-02-17', 'Fullfilled', 102, 'F'),

(3,'03-03-17', 'Fullfilled', 103, 'P'), (4, '03-03-17', 'Cancelled',104, 'F'), (5,'05-03-17', 'Backorder', 105, 'P');

select * from sales_order;


b)
ALTER TABLE sales_man DROP constraint sales_man_pkey cascade;
\d sales_man;

\d sales_order;

c)

ALTER TABLE sales_order DROP constraint sales_order_del_type_check;


ALTER TABLE sales_order DROP constraint sales_order_order_status_check;

\d sales_order;
d)
ALTER TABLE sales_man ADD primary key(salesman_no);

\d sales_man;

e)
ALTER TABLE sales_order ADD FOREIGN KEY (salesman_no) REFERENCES sales_man(salesman_no);
ALTER TABLE sales_order ADD CHECK (del_type = 'F' OR del_type = 'P');
ALTER TABLE sales_order ADD check(order_status IN( 'Inprocess', 'Fullfilled', 'Cancelled', 'Backorder'));

\d sales_order;
Result:
SQL query successfully executed.
Program 3 : Hospital Details
AIM
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’

TABLE DESIGN

QUERIES
create table hospital(doctorid char(4),doctorname varchar(10),department varchar(25),qualification
varchar(25),experience int);

a)
insert into hospital values('d001','miya','cardiologist','mbbs',5);
insert into hospital values('d002','john','orthologist','md',4);
insert into hospital values('d003','ramesh','skin','mbbs',3);
insert into hospital values('d004','madona','dentist','bds',6);
insert into hospital values('d005','manoj','optometry','md',1);
b)
select * from hospital;

c)
select * from hospital where qualification='md';

doctorid | doctorname | department | qualification | experience


----------+------------+-------------+---------------+------------
d002 | john | orthologist | md | 4
d005 | manoj | optometry | md | 1

d)
select doctorname from hospital where experience>5 and qualification!='md';

e)
select dotorname from hospital where department='skin';

f)
update hospital set experience=5 where doctorid='d003';
select * from hospital;
g)
delete from hospital where doctorid='d005';
select * from hospital;

Result:
SQL query successfully executed.
Program 4 : Bank details
AIM
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

TABLE DESIGN
QUERIES
create table Bank_customer(accno int primary key,cust_name varchar(25),place varchar(25)); create
table Deposit(accno int references Bank_customer(accno),deposit_no int, damount numeric); create
table loan(accno int references bank_customer(accno), loan_no int,lamount numeric);

insert into bank_customer values(101,'Ravi','clt');


insert into bank_customer values(102,'Adam','tvm');
insert into bank_customer values(103,'Aysha','mlprm');
insert into bank_customer values(104,'Lisa','knr');
insert into bank_customer values(105,'Shaju','klm');
insert into bank_customer values(106, 'Razeen','kch');
insert into bank_customer values(107,'Radha','tvm');
insert into bank_customer values(108,’Jose','knr');

insert into deposit values(101,15,400000);


insert into deposit values(102,13,75000);
insert into deposit values(105,12,55000);
insert into deposit values(108,16,750000);

insert into loan values(103,4,500000);


insert into loan values(104,2,200000);
insert into loan values(106,6,300000);
insert into loan values(108,8,600000);

select * from loan;


select * from deposit;

a)
select * from bank_customer;

Accno cust_name place

101 Ravi clt


102 Adam tvm
103 Aysha mlprm
104 Lisa knr
105 Shaju klm
106 Razeen kch
107 Radha tvm
108 jose knr

b)
select b.accno,cust_name,damount from bank_customer b join deposit d on b.accno=d.accno where b.accno
not in(select accno from loan);

Accno cust_name damount

101 Ravi 400000


102 Adam 75000
105 Shaju 55000

c)
select b.accno,cust_name,lamount from bank_customer b join loan l on b.accno=l.accno where b.accno not
in(select accno from deposit);
d)
select b.cust_name,l.lamount,d.damount from bank_customer b,loan l,deposit d where b.accno=d. accno AND
b.accno=l.accno);

select b.cust_name,lamount ,damount from bank_customer b join loan l on b.accno=l.accno join deposit d and
b.accno=d.accno where b.accno in((select accno from loan) intersect (select accno from deposit));

cust_name lamount damount

Jose 600000 750000

e)
select * from bank_customer where accno not in((select accno from loan)union(select accno from deposit));

Accno cust_name place

107 Radha tvm

Result:
SQL query successfully executed.
Program 5 :Employee 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
A.​ Display the total number of employees.
B.​ Display the name and age of the oldest employee of each department.
C.​ Display departments and the average salaries
D.​ Display the lowest salary in employee table
E.​ Display the highest salary in sales department;
F.​ Display the difference between highest and lowest salary

TABLE DESIGN

QUERIES
create table employee(empid int PRIMARY KEY,ename varchar(10),salary numeric, department var- char(20) ,
age int);

Insert into employee values(101, 'Adam', 20000, 'Purchase', 25), (102, 'Lisa', 15000, 'Sales', 45), (103,
'Arun', 18000, 'Sales', 34), (104, 'Aysha',25000, 'Purchase', 25), (105, 'Sheeja', 30000, 'Finance', 36),
(106, 'Sagar', 28000, 'Finance', 42);
select * from employee;

empid enmae salary department age

101 Adam 20000 Purchase 25


102 Lisa 15000 Sales 45
103 Arun 18000 Sales 34
104 Aysha 25000 Prchase 25
105 Sheeja 30000 Finance 36
106 Sagar 28000 Finance 42

a)
select count(empid)from employee;

b)
select ename,department from employee a where age in(select max(age) from employee b group by department
having a.department=b.department);

enmae department

Adam Purchase
Lisa Sales
Aysha Prchase
Sagar Finance

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

Purchase 22500.000000000000
Finance 29000.000000000000
Sales 16500.000000000000
(3 rows)
d)
select min(salary) as min_salary from employee;

e)
select max(salary)from employee where department='Sales';

f)
select max(salary) - min(salary) as sal_difference from employee;

Result:
SQL query successfully executed.
Program 6 : Product details
AIM
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'

TABLE DESIGN

QUERIES
create table product(product_code int primary key, product_name varchar(20),category varchar(20), quantity
int,price numeric(10,2));

insert into product values(1,'colgate','paste',10,100); insert


into product values(2,'close up','paste',9,90); insert into
product values(3,'nirma','bath soap',10,600);
insert into product values(4,'sunlight','washing powder',10,700); insert
into product values(5,'toy','car',1,200);
insert into product values(6,'toy','bike',3,300); insert
into product values(7,'lux','bath soap',1,20);
insert into product values(8,'lux','bath liquid',600,2000); insert
into product values(9,'nirma','bath liquid',300,1000);
select * from product;

a)
select * from product order by product_name desc;

b)
select product_code,product_name from product where price between 20 and 50;
C)
select product_name,price from product where category in ('bath soap','paste','washing powder');

product_name price

colgate 100.00
close up 90.00
nirma 600.00
sunlight 700.00
lux 20.00
(5 rows)

d)
select * from product where quantity<100 or quantity>500;

e)
select product_name from product where product_name like 's%';

f)
select product_name from product where category != 'paste';
g)
select product_name from product where product_name like '_u%' and category='washing powder';

Result:
SQL query successfully executed.
Program 7 : Company details

AIM
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.
TABLE DESIGN
QUERIES

create table employee(empname varchar(10) primary key,city varchar(10));

create table company(company_name varchar(10) primary key,city varchar(10));

create table works(empname varchar(10) primary key references employee(empname),cname var-


char(10) references company(company_name),salary int);

create table manages(empname varchar(10) references employee(empname),manager_name


varchar(10)​ references employee(empname),primary key(empname,manager_name));

insert into employee values('swathi','kzkd');


insert into employee values('vishnu','tvm');
insert into employee values('shreya','usa');
insert into employee values('adam','dubai');
insert into employee values('sajid','malappuram');

insert into company values('infosys','tvm');


insert into company values('chandrika','trissur');
insert into company values('wipro','kochi');
insert into company values('tata','mumbai');
insert into company values('bajaj','delhi');

insert into works values('swathi','infosys',10000);


insert into works values('vishnu','infosys',15000);
insert into works values('shreya','wipro',71502);
insert into works values('adam','infosys',8000);
insert into works values('sajid','wipro',18000);

insert into manages values('swathi','adam');


insert into manages values('vishnu','adam');
insert into manages values('sajid','shreya');
select * from employee;

select * from company;

select * from manages;

select * from works;


a)
select empname​ from works where cname='infosys';

b)
select employee.empname,employee.city from employee,works where employee.empname =
works.empname and works.cname = 'wipro';

c)
select employee.empname,city from employee,works where employee.empname= works.empname and
cname='infosys' and salary>10000;

d)
select employee.empname from employee,works,company where employee.empname = works.empname
and employee.city​ = company.city and works.cname​ =​ company. company_name;

e)
select empname from works where cname!='wipro';
f)
select cname from works group by cname order by count(*) desc limit 1;

Result:
SQL query successfully executed.
Program 8 :Supplier details
AIM
Create table supplier(supcode,sname,city)
Create table product (pcode,pname)
Create table supl_product(supcode,pcode,qty)
A.​ Get all pairs of supplier numbers such that the two suppliers are located in the same city.
B.​ Get supplier names for suppliers who supply product P2.
C.​ Get product numbers supplied by more than one supplier.
D.​ Get supplier numbers for suppliers who are located in the same city as supplier S1.
E.​ Get supplier names for suppliers who supply part P1.
F.​ Get the number of Suppliers, who are supplying at least one product.
G.​ For each product supplied, get the pcode. and the total quantity supplied for that part.

TABLE DESIGN
Table Name : Supplier

Name Type Constraints Description

supcode Char(3) Primary key Supplier number

sname varchar(10) Supplier name

city Varchar(10) City

Table Name : Product

Name Type Constraints Description

pcode Char(3) Primary key product number

pname varchar(10) product name


Table Name : Supl_product

Name Type Constraints Description

supcode Char(3) Foreign key – supplier (supcode) Supplier number

pcode char(3) Foreign key – product(pcode) Product number

qty int Quantity

QUERIES
create table supplier(supcode char(3) primary key,sname varchar(10), city varchar(10));

create table product(pcode char(3) primary key, pname varchar(10));

Create table supl_product(supcode char(3) references supplier(supcode),pcode char(3) references


product(pcode),qty int);

insert into supplier values ('s1','raju','calicut'), ('s2','sheela','thrissur'), ('s3','aysha','kochi'),


('s4','anees','tirur'), ('s5','lisa','calicut'),('s6','zara','tirur');

select * from supplier;


insert into product values('p1','soap'), ('p2','rice'), ('p3','salt'), ('p4','sugar'), ('p5','wheat'), ('p6','colgate'),
('p7','chilly');

select * from product;

insert into supl_product values('s1','p1',34), ('s1','p2',20), ('s2','p2',20), ('s6','p4',5), ('s3','p3',10),


('s2','p7',10), ('s3','p1',12);

select * from supl_product;

a)
select a.supcode,b.supcode,b.city from supplier a,supplier b where a.city=b.city and a.supcode<b.supcode;

b)
select sname from supplier where supcode in(select supcode from supl_product where pcode=’p2’);

sname
raju
c)
select pcode from supl_product group by pcode having count(pcode)>1;

d)
select supcode from supplier where city=(select city from supplier where supcode='s1');

e)
select sname from supplier where supcode in(select supcode from supl_product where pcode='p1');

f)
select count(distinct supcode) from supl_product;

g)
select pcode,sum(qty) from supl_product group by pcode;

pcode | sum
+
p3 | 10
p4 | 5
p7 | 10
p2 | 40
p1​ | 46
(5 rows)

Result:
SQL query successfully executed.
Program 9 :Student Grade

AIM

Create table exam_result(rollno, avg_score, Grade) insert 10 records. Assign null values to the field grade.
Write Program block to update the grade field by using the following condition.
avg_score between 90 and 100 - A
avg_score 75 -89 - B
avg_score 60- 74 - C
avg_score 50 -59 - D
avg_score below 50 – E

TABLE DESIGN

Table Name : exam_result


Name Type Description
Rollno Integer Roll Number
avg_score numeric(5,2) Average Mark
Grade char(1) Grade

QUERIES
Create table exam_result(rollno integer,avg_score numeric(5,2), grade char(1));

insert into exam_result values (4, 67), (3, 35), (2, 91), (6, 45), (11, 86),(7,95), (8, 75), (9, 68), (5, 55), (12,
97), (13, 30);
select * from exam_result;

rollno avg_score grade

4 67
3 35
2 91
6 45
11 86
7 95
8 75
9 68
5 55
12 97
13 30

do $$
declare rec
record;
grd char(1);
begin
for rec in select * from exam_result
loop
if rec.avg_score between 90 and 100​ then
grd:= 'A';
elsif rec.avg_score between 75 and 89 then
grd= 'B';
elsif rec.avg_score between 60 and 74 then
grd:= 'C';
elsif rec.avg_score between 50 and 59 then
grd:= 'D';
else
grd:= 'E';
end if;
update exam_result set grade=grd where rollno=rec.rollno;
end loop;
End $$;
Select * from exam_result;

rollno avg_score grade

4 67 C
3 35 E
2 91 A
6 45 E
11 86 B
7 95 A
8 75 B
9 68 C
5 55 D
12 97 A
13 30 E

Result:
SQL query successfully executed.
Program 10 : Area of a Circle
AIM
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.

TABLE DESIGN

QUERIES
do $$
declare
r integer;
ar numeric(10,2);
begin
create table areas(radius integer,area numeric(5,2));
r:=3;
for r in 3..7 loop

ar:=3.14*r*r;
insert into areas values(r,ar);
end loop;
end $$;
select * from areas;

Result:
SQL query successfully executed.
Program 11 : Electricity Bill
AIM
Write a program block to calculate the electricity bill by accepting cust_no and units_consumed.

PROGRAM CODE
create or replace function electricity_bill(c int, u int) returns text as $$
declare
rate int;
amt int;
begin
if u<=100 then
rate:=3;
elsif u<=250 then
rate:=4;
elsif u<=500 then
rate:=5; else rate:=6; end if;
amt:=rate*u;
return 'Customer No​ : ' || c || E'\nUnits Consumed : ' || u || E'\nBill Amount​ : ' || amt;
end;
$$ language plpgsql;

select electricity_bill(121,200);
Result:
SQL query successfully executed.

Program 12 : Fibonacci Numbers upto a Limit


AIM
Create a procedure to print Fibonacci number up to a limit, limit is passed as an argument

PROGRAM CODE
create or replace function fibonacci(n int) returns setof int as $$

declare
a int:=1;
b int:=0;
c int:=0;
begin
loop
exit when n < c;
return next c;
c:=a+b;
a:=b;
b:=c;
end loop;
end;
$$ language plpgsql;

select fibonacci(8);
Result:
SQL query successfully executed.

Program 13 : Check Prime or Not


AIM
Create a function to check whether a given number is prime or not

PROGRAM CODE
CREATE FUNCTION check_prime(n int) returns varchar(25) AS $$
DECLARE
i int;
BEGIN
if​ n<2 then
return n || '​ is not a prime number ';
end if;
for i in 2..n/2
loop
if mod(n,i)=0 then
return n || '​ is not a prime number ';
end if;
end loop;
return n || '​ is a prime number ';
end;
$$ language plpgsql;

Select check_prime(1);
Select check_prime(7);
lbsmdc=# Select check_prime(1);
check_prime

1 is not a prime number


(1 row)

lbsmdc=# Select check_prime(7);


check_prime

7 is a prime number
(1 row)

Result:
SQL query successfully executed.
Program 14​ Display Average Salary of a Department
AIM
Create a table emp_salary(empno,ename,dept,salary)
Write a function to return the average salary of a particular department by accepting departmentname as
argument.

TABLE DESIGN

QUERIES
Create table emp_salary(empno int, ename varchar(15), dept varchar(15), salary int);
Insert into emp_salary values(101, 'Adam', 'Production', 20000), (102, 'Lisa', 'Marketing', 15000), (103,
'Arun', 'Marketing', 18000), (104, 'Aysha', 'Production', 25000), (105, 'Sheeja', 'Finance', 30000), (106,
'Sagar', 'Finance', 28000);

Select * from emp_salary;


create function avg_salary(dept_name varchar(10)) returns numeric(10,2) as $$
declare
avg_sal​ numeric(10,2);
begin
select avg(salary) into avg_sal from emp_salary group by dept having dept=dept_name;
return avg_sal;
end;
$$ language plpgsql;

Select avg_salary('Production');

Select avg_salary('Finance');

Result:
SQL query successfully executed.
Program 15.​ Total and average mark using Trigger Before Insert
AIM
Create a table Student (regno, sname, sub1, sub2, sub3, sub4, sub5, mark_total,avg_mark)
Create a BEFORE INSERT trigger to calculate total mark and average mark and update the corresponding
columns.

TABLE DESIGN
Table Name : student
Name Type Description
Regno char(5) Register number
Sname varchar(15) Name of the student
sub1 numeric(3) Subject 1
sub2 numeric(3) Subject 2
sub3 numeric(3) Subject 3
sub4 numeric(3) Subject 4
sub5 numeric(3) Subject 5
mark_total numeric(3) Total mark
avg_mark numeric(5,2) Average mark

QUERIES
Create table student(regno char(5), sname varchar(15), sub1 numeric(3), sub2 numeric(3), sub3 numer-
ic(3), sub4 numeric(3), sub5 numeric(3),​ mark_total numeric(3), avg_mark numeric(5,2));

create or replace function fun() returns trigger as $$


declare
Begin

new.mark_total=new.sub1+new.sub2+new.sub3+new.sub4+new.sub5;
new.avg_mark=new.mark_total/5.0;
return new;
end;
$$ language plpgsql;

create trigger trig before insert on student for each row execute procedure fun();

insert into student values ('s101','adam',23,45,67,23,45), ('s102', ' sheena', 96,97,89,95,67), ('s103',
'bobby', 67,52,83,91,34), ('s104', 'radha', 34,54,23,12,25), ('s105', 'zara', 86,76,82,85,34);

select * from student;

Result:
SQL query successfully executed.

You might also like