Rdbms pgms-1
Rdbms pgms-1
AIM
Create a table customer (cust_no varchar (5), cust_name varchar (15), age number, phone varchar (10))
TABLE DESIGN
QUERIES
create table customer(cust_no varchar(5),cust_name varchar(15),age numeric,phone varchar(10));
a)
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)).
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;
(3,'03-03-17', 'Fullfilled', 103, 'P'), (4, '03-03-17', 'Cancelled',104, 'F'), (5,'05-03-17', 'Backorder', 105, 'P');
\d sales_order;
c)
\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).
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';
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)
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);
a)
select * from bank_customer;
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);
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));
e)
select * from bank_customer where accno not in((select accno from loan)union(select accno from deposit));
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;
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));
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
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
QUERIES
create table supplier(supcode char(3) primary key,sname varchar(10), city varchar(10));
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
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;
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;
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 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 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
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 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));
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);
Result:
SQL query successfully executed.