You are on page 1of 23

2

Q 1: Consider the following relational schema

SAILORS (sid, sname, rating, date_of_birth) BOATS (bid,


bname, color)
RESERVES (sid, bid, date, time slot)

Write the following queries in SQL and relational algebra


a) Find sailors who’ve reserved at least one boat
b) Find names of sailors who’ve reserved a red or a orange boat in the
month of March.
c) Find names of sailors who’ve reserved a black and a orange
boat
d) Find sid of sailors who have not reserved a boat after Jan 2018.
e) Find sailors whose rating is greater than that of all the sailors
named
“John”
f) Find sailors who’ve reserved all boats
g) Find name and age of the oldest sailor(s)
h) Find the age of the youngest sailor for each rating with at least 2 such
sailors

Creating the tables


-- Creating table for sailors
create table sailors( sid
int primary key , sname
varchar(20), rating int,
date_of_birth date
);

-- creating table for boats create


table boats(
bid int primary key,
bname varchar(30),
color varchar(10)
);

-- creating table for realtion reservers


3

create table reserves(


sid int not null,
bid int not null, dates
date, timeslot int,
primary key(sid , bid , dates , timeslot),
foreign key (sid) references sailors(sid) on delete cascade, foreign key
(bid) references boats (bid) on delete cascade
);

Inserting data

-- inserting data in sailors table

insert into sailors values (1,'Jacky', 1, "1991-05-11"); insert into


sailors values (2,'John', 2, "1990-07-12"); insert into sailors
values (3,'James', 1, "1980-05-04"); insert into sailors values
(4,'Colombo', 5, "1996-08-08"); insert into sailors values
(5,'Parth', 6, "1989-12-09"); insert into sailors values (6,'Manasvi',
9, "1992-02-11"); insert into sailors values (7,'Apoorva', 8, "1994-
01-05"); insert into sailors values (8,'Shrey', 5, "1969-06-09");

-- inserting data into boats table :

insert into Boats values (101, 'The Wind Ranger', 'Black'); insert into
Boats values (102, 'Seas the Day', 'Green'); insert into Boats values
(103, 'Jolly Rancher', 'Black'); insert into Boats values (104,
'Buoyant Buoy', 'Blue'); insert into Boats values (105, 'Serendipity',
'DarkBlue'); insert into Boats values (106, 'Aqua Life', 'Black');
insert into Boats values (107, 'Mizu Boto', 'Orange'); insert into
Boats values (108, 'The Prawn Star', 'Black');

-- inserting data in reserve table

insert into reserves values (1, 108, '2018-11-10',9);


insert into reserves values (1, 103, '2017-10-16',10);
insert into reserves values (1, 104, '2017-09-10' ,11);

insert into reserves values (2, 101, '2019-10-12',13);


insert into reserves values (2, 102, '2018-09-10',16);
insert into reserves values (2, 107, '2018-11-12',18);
4

insert into reserves values (3, 102, '2019-03-01',18);


insert into reserves values (3, 104, '2019-03-09',13);
insert into reserves values (3, 105, '2021-08-07',14);

insert into reserves values (4, 106, '2021-08-09',12);


insert into reserves values (4, 107, '2011-03-19',16);
insert into reserves values (4, 103, '2017-03-19',19);

insert into reserves values (6, 108, '2021-08-09',12);


insert into reserves values (6, 107, '2011-03-19',16);
insert into reserves values (6, 104, '2017-03-19',19);

insert into reserves values (7, 103, '2018-04-01',18);


insert into reserves values (7, 103, '2018-04-09',17);
insert into reserves values (7, 103, '2021-08-07',14);

insert into reserves values (8, 108, '2020-03-09',12);


insert into reserves values (8, 106, '2018-03-19',16);
insert into reserves values (8, 104, '2019-03-19',19);

insert into reserves values (1, 102, '2019-12-19',18);


insert into reserves values (2, 107, '2018-11-15',17);
insert into reserves values (3, 104, '2019-12-13',14);

insert into reserves values (3, 101, '2019-11-19',19);


insert into reserves values (4, 102, '2019-12-19',18);
insert into reserves values (8, 102, '2020-10-11',19);
5

a) Find sailors who’ve reserved at least one boat

select distinct sailors.sname , sailors.sid from


sailors join reserves
on sailors.sid = reserves.sid;

b) Find names of sailors who’ve reserved a Red or a orange boat in the month of
March.

select sailors.sname from sailorswhere


sailors.sid in
( select reserves.sid from reserves join boats
on boats.bid = reserves.bid where dates like '%-03-%' and (color = 'Red' or
color = 'Orange')
);
6

c) Find names of sailors who’ve reserved a black and a orange boat

select sailors.sname from sailors where sailors.sid in (


select reserves.sid from reserves join boats
on reserves.bid = boats.bid where boats.color = 'Black' and reserves.sid in
(
select reserves.sid from reserves join boats
where boats.bid = reserves.bid and color = 'Orange'
));

d) Find sid of sailors who have not reserved a boat after Jan 2018.

select sid from sailors where


sid not in
(select distinct sid from reserves where dates > '2018-01-31');
7

e) Find sailors whose rating is greater than that of all the sailors named “John”.

select sailors.sname , sailors.sid , sailors.rating from sailors where


sailors.rating >
(select sailors.rating from sailors where sailors.sname = 'John');

f) Find sailors who’ve reserved all boats

select sailors.sname , sailors.sid , derived.counts


from sailors join (select sid , count(distinct reserves.bid) as counts from reserves
group by reserves.sid) as derived
on derived.sid = sailors.sid where counts = (select count(bid) from boats);

g) Find name and age of the oldest sailor

select sname , floor(datediff(current_date() , date_of_birth)/365) as age from


sailors
where date_of_birth in
(select min(date_of_birth) from sailors);
8

h) Find the age of the youngest sailor for each rating with at least 2 such sailors

select min(floor(datediff(current_date() , date_of_birth)/365)) as age , rating from sailors


group by rating having count(*) > 1;

RELATIONAL ALGEBRA
9
1

Q2. Consider the following relational schema:

CUSTOMER (cust_num, cust_lname , cust_fname, cust_balance); PRODUCT


(prod_num, prod_name, price)
INVOICE (inv_num, prod_num, cust_num, inv_date ,unit_sold, inv_amount);

Write SQL queries and relational algebraic expression for the following

a) Find the names of the customer who have purchased no item.


Set default
value of Cust_balance as 0 for such customers.
b) Write the trigger to update the CUST_BALANCE in the CUSTOMER
table when a new invoice record is entered for the customer.
c) Find the customers who have purchased more than three units of a
product on a day.
d) Write a query to illustrate Left Outer, Right Outer and Full Outer Join.
e) Count number of products sold on each date.
f) As soon as customer balance becomes greater than Rs. 100,000, copy the
customer_num in new table called ”GOLD_CUSTOMER”
g) Add a new attribute CUST_DOB in customer table
1

Creation of Table & Insertion


of Data :
create DATABASE QUESTION_2; use
QUESTION_2;

create table customer (


cust_num int primary key,
cust_fname varchar(20),
cust_lname varchar(20),
cust_balance int
);

create table product (


prod_num int primary key,
prod_name varchar(30), price int
);

create table invoice(


invoice_num int, prod_num
int , cust_num int not null,
invoice_date date,
unit_sold int,
invoice_amount int,
primary key (invoice_num ,prod_num , cust_num , invoice_date),
foreign key (cust_num) references customer(cust_num) on delete cascade, foreign key
(prod_num) references product(prod_num) on delete cascade
);

insert into customer value(203 , 'Daniel' , 'Radchiffe' , 5000);insert into customer value(321 , 'Emma' , 'Watson' , 8000);
insert into customer value(69 , 'Shrey' , 'Aggrawal' , 40000); insert
into customer value(4 , 'Rupert' , 'Grint' , 10000); insert into
customer value(56 , 'Alan' , 'Rickman' , 9000); insert into customer
value(439 , 'Tom' , 'Felton' , 12000); insert into customer value(908 ,
'Evanna' , 'Lynch' , 20000); insert into customer value(108 , 'Bonnie' ,
'Wright' , 50000); insert into customer value(72 , 'Loren' , 'Allred' ,
5000); insert into customer value(11 , 'Sasha' ,'Sloan' , 130000);
10

insert into product value(2, 'Razer Blade Stealth 14', 130000); insert into
product value(3, 'Macbook Pro 14 (2021)', 200000); insert into product
value(4, 'HP Spectre X360', 170000);
insert into product value(5, 'Apple Microfiber Cloth', 2000);

insert into invoice value(1,'1' ,69 ,'2021-11-23' ,2 ,30);


insert into invoice value(2,'4' ,4 ,'2021-12-13' ,1 ,1300);
insert into invoice value(3,'2' ,321 ,'2015-10-23' ,1 ,40000);
insert into invoice value(4,'5' ,439 ,'2011-01-23' ,3 ,1400);
insert into invoice value(5,'1' ,69 ,'2021-05-21' ,2 ,1500);
insert into invoice value(6,'2' ,321 ,'2010-12-13' ,12 ,100);
insert into invoice value(7,'5' ,56 ,'2018-09-23' ,10 ,5000);
insert into invoice value(8,'3' ,439 ,'2019-10-22' ,2 ,1500);
insert into invoice value(9,'2' ,108 ,'2020-01-18' ,12 ,150);
insert into invoice value(10,'4' ,4 ,'2021-11-23' ,5 ,6000);
insert into invoice value(11,'3' ,321 ,'2017-10-13' ,2 ,75000);
insert into invoice value(12,'3' ,439 ,'2020-11-23' ,1 ,8000);
insert into invoice value(13,'2' ,908 ,'2020-11-24' ,1 ,4200);
insert into invoice value(14,'1' ,69 ,'2020-11-1' ,1 ,600);
11

a) Find the names of the customer who have


purchased no item. Set default value of
Cust_balance as 0 for such customers.
select CONCAT(cust_fname," ",cust_lname) as name from
customer where cust_balance = 0;

b) Write the trigger to update the


CUST_BALANCE in the CUSTOMER
table when a new invoice record is
entered for the customer.

delimiter $$
create trigger check_age before insert on invoice for
each row
Update customer c
Set c.cust_balance = c.cust_balance + new.invoice_amount Where
c.cust_num = new.cust_num;
Select cust_fname ,cust_lname , cust_balance from customer;
12

c) Find the customers who have


purchased more than three units of a
product on a day.

select cust_num, cust_fname from


customer where cust_num in(select
cust_num from invoice
group by cust_num,invoice_date,prod_num having
sum(unit_sold)>3);
13

D) Write a query to illustrate Left Outer, Right Outer


and Full Outer Join.

i) Left Join
select customer.cust_num ,cust_fname , cust_lname , invoice_amount ,invoice_date from customer
left join invoice
on customer.cust_num = invoice.cust_num;
14

ii) Right Join

select customer.cust_num ,cust_fname , cust_lname , invoice_amount ,invoice_date from customer


right join invoice
on customer.cust_num = invoice.cust_num;
15

iii) Full Outer Join


(select customer.cust_num ,cust_fname , cust_lname , invoice_amount ,invoice_date from
customer left join invoice
on customer.cust_num = invoice.cust_num) union
(select customer.cust_num ,cust_fname , cust_lname , invoice_amount ,invoice_date from
customer right join invoice
on customer.cust_num = invoice.cust_num);
16

e) Count number of products sold on each date.

select invoice_date , sum(unit_sold) from


invoice group by invoice_date order by
invoice_date asc;

F) As soon as customer balance becomes greater than Rs. 100,000, copy the customer_num in
new table called ”GOLD_CUSTOMER”

select cust_num from


(select * from customer where cust_balance > 10000) as
gold_customer;
17

g) Add a new attribute CUST_DOB in customer table

alter table customer add cust_dob date; select *


from customer
18

Q3)
DEPARTMENT(Department_ID, Name, Location_ID) JOB (Job_ID ,
Function )
EMPLOYEE (Employee_ID, name, DOB, Job_ID , Manager_ID, Hire_Date, Salary,
department_id)

a) Write a query to count number of employees who joined in March 2015


b) Display the Nth highest salary drawing employee details.
c) Find the budget (total salary) of each department.
d) Find the department with maximum budget.
e) Create a view to show number of employees working in Delhi and
update it automatically when the
database is modified.
f) Write a trigger to ensure that no employee of age less than 25 can be
inserted in the database

Creating the tables


create table department ( department_id
int primary key, department_name
varchar(20), location_id int
);

create table job ( job_id


int primary key,
functions varchar(30)
);

create table employee( employee_id


int primary key, employee_name
varchar(30) , date_of_birth date,
job_id int not null,
hire_date date,
manager_id int , salary
int, department_id int,
foreign key (job_id) references job(job_id) ,
foreign key (department_id) references department(department_id)
);
19

Inserting data
insert into department value(01 , 'Research' , 'Ghaziabad'); insert into
department value(02 , 'Accounting' , 'Delhi'); insert into department
value(03 , 'Development' , 'Gurgaon');
insert into department value(04 , 'Human resource management' , 'Gurgaon'); insert into
department value(05 , 'Sales' , 'Faridabad');
insert into department value(06 , 'Marketing' , 'Delhi'); insert into
department value(07 , 'Designing' , 'Sonipat'); insert into department
value(08 , 'Operations' , 'Faridabad');

insert into job value(101 , 'Designer'); insert into


job value(102 , 'Operator'); insert into job
value(103 , 'Developer'); insert into job value(104
, 'Accountant'); insert into job value(105 , 'Sales
head'); insert into job value(106 , 'Researcher');
insert into job value(107 , 'Marketing Specialist'); insert into
job value(108 , 'Manager');

-- emp_id , name , dob , job_id ,hire_date , manager , salary , departement_id insert into
employee value(1001 , 'Rachel Shroff' ,'1995-09-08' , 105,
'2018-03-06' ,800 , 50000 , 05);
insert into employee value(1002 , 'Nikita Adani' ,'1990-05-12' , 107 , '2013-09-
24', 815 , 100000, 06);
insert into employee value(1003 , 'Arshpreet Singh' ,'1992-05-15' , 102 , '2017-11-03'
,813 , 45000, 08);
insert into employee value(1004 , 'Shreyas Srivastava' ,'1991-10-22' , 106 , '2009-12-18'
,807 , 75000 , 01);
insert into employee value(1005 , 'Amit Trivedi' ,'1988-10-12' , 108 , '2017-03-
21' ,809 , 82000 , 04);
insert into employee value(1006 , 'Sunil Kumar' ,'1989-10-15' , 103 , '2016-04-
15' ,808 , 65000 , 03);
insert into employee value(1007 , 'Dinesh Singh' ,'1986-01-21' , 101 , '2014-03-22'
,813 , 90000 , 07);
insert into employee value(1008 , 'Vinod Kumar' ,'1990-01-12' , 104 , '2011-07-01'
,802 , 55000 , 02);
insert into employee value(1009 , 'Vishwajeet Jindal' ,'1988-10-08' , 103 , '2016-09-15'
,808 , 60000 , 03);
insert into employee value(1010 , 'Arundhati Roy' ,'1992-10-26' , 107 , '2015-08-14'
,815 , 120000 , 06);
11

a) Write a query to count number of employees who joined in March 2015


select count(employee_id) from (select
employee_id
from employee
where hire_date like '%-03-%') as emp;

b) Display the Nth highest salary drawing employee details.


select * from employee where
employee.salary not in (select
employee.salary
from employee cross join employee
as temp on employee.salary < temp.salary
);

b) Find the budget (total salary) of each department.

select employee.department_id , sum(employee.salary) from


employee group by department_id;
20

d) Find the department with maximum budget.


select department_id , sum(salary) as budget from
employee group by department_id order by
sum(salary) desc;

e) Create a view to show number of employees working in Delhi and update it


automatically when the database is modified.

create view emp_delhi as select count(distinct employee_id) from (select *


from employee natural join department
where location = 'Delhi') as temp; select *

from b.emp_delhi;

f) Write a trigger to ensure that no employee of age less than 25 can be inserted in the
database
delimiter $$
create trigger verify_age before insert on employee for each
row
begin
if new.dob > '1995-01-01' then signal
sqlstate '45000'
set message_text = 'error:
age must be atleast 25 years!'; end if;
end;
21

RELATIONAL ALGEBRA

You might also like