You are on page 1of 17

Consider the following relational schema

SAILORS (sid, sname, rating, dob)

BOATS (bid, bname, color)

RESERVES (sid, bid, date)

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 green boat in the month of March.

c) Find names of sailors who’ve reserved a red and a green boat

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

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

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

TABLES
create table sailors( sid int primary key,
sname varchar(256),
rating int,
dob date);

create table boats(bid int primary key,


bname varchar(256),
color varchar(256)
);
create table reserves( sid int not null,
bid int not null,
ddate date not null,
foreign key (sid) references sailors(sid),
foreign key (bid) references boats (bid));

VALUES
insert into sailors values(100,’rahul’,98,’2000-05-23’);
insert into sailors values(101,’ravi’,98,’1995-05-23’);
insert into sailors values(102,’vikas’,32,’1990-05-23’);
insert into sailors values(103,’aditya’,32,’1996-05-23’);
insert into sailors values(104,’ajay’,40,’1994-10-25’);

RESERVES
insert into reserves values (100,1000,’2020-03-25’);
insert into reserves values (101,1001,’2019-04-25’);
insert into reserves values (100,1001,’2019-04-30’);
insert into reserves values (103,1002,’2020-04-30’);
insert into reserves values (103,1003,’2020-06-30’);
insert into reserves values (101,1000,’2020-05-24’);
insert into reserves values (102,1003,’2019-05-12’);
insert into reserves values (100,1002,’2019-06-13’);
insert into reserves values (100,1003,’2019-09-13’);
BOATS
insert into boats values (1000,’alpha’,’red’);
insert into boats values (1001,’bravo’,’green’);
insert into boats values (1002,’charlie’,’black’);
insert into boats values (1003,’delta’,’white’);

a) SELECT DISTINCT sname FROM SAILORS S, RESERVES R


WHERE R.sid=S.sid;

b) SELECT sname FROM SAILORS S ,BOATS B ,RESERVES R

WHERE R.sid=S.sid AND B.bid=R.bid AND (B.color=’RED’

OR B.color=’green) AND ddate LIKE ‘%-03-%’ ;


c) SELECT sname FROM SAILORS S ,BOATS B ,RESERVES R

WHERE R.sid=S.sid AND B.bid=R.bid AND B.color=’red’

INTERSECTS

SELECT sname FROM SAILORS S ,BOATS B ,RESERVES R

WHERE R.sid=S.sid AND B.bid=R.bid AND B.color=’green’;

d) SELECT S.sid FROM SAILORS S

WHERE S.sid NOT IN

( SELECT R.sid FROM RESERVES R WHERE ddate>’2020-01-01’) ;


e) SELECT * FROM SAILORS S WHERE S.rating>

(SELECT S1.rating FROM SAILORS S1 WHERE S1.sname=’ajay’) ;

f) SELECT S.sname FROM SAILORS S WHERE NOT EXISTS (SELECT * FROM BOATS B WHERE
NOT EXISTS ( SELECT * FROM RESERVES R WHERE R.sid=S.sid AND R.bid=B.bid);
g) SELECT S.sname , TIMESTAMPDIFF(YEAR,dob,"2020-10-29") as age from SAILORS S where

S.dob IN

(SELECT MIN(S1.dob) FROM SAILORS S1);

h) SELECT RATING,min(TIMESTAMPDIFF(YEAR,dob,"2020-09-17")) as youngestage FROM

SAILOR GROUP BY RATING having COUNT(*)>1;

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
CUSTOMER TABLE
Create table customer (cust_num int primary key,cust_lname varchar(256) ,cust_fname
varchar(256) not null,cust_balance int default 0);
insert into customer values(1,’shastri’,’ravi’,0);
insert into customer values(2,’goyal’,’vikas’,1000);
insert into customer values(3,’shastri’,’ravi’,2000);
insert into customer values(4,’kumar’,’akshat’,3000);
insert into customer values(5,’agaarwal’,’rohit’,4000);
insert into customer values(6,’singh’,’aman’,5000);
insert into customer values(7,’kumar’,’ankur’,6000);
insert into customer values(8,’jain’,’yash’,0);

INVOICE TABLE
create table invoice(
inv_num int primary key,
prod_num int not null,
cust_num int not null,
inv_date date not null,
unit_sold int not null,
inv_amount int not null,
foreign key (prod_num) references product (prod_num),
foreign key (cust_num) references customer (cust_num),
check(unit_sold>0));
INSERTING VALUES
insert into invoice values (1,3,1,'2020-05-26',2,10000);
insert into invoice values (2,1,7,'2020-06-06',4,10000);

PRODUCT
create table product (prod_num int primary key,prod_name varchar(256) not null, price int
not null);
insert into product values(1,'phone',2500);
insert into product values(2,'radio',500);
insert into product values(3,'camera',5000);
insert into product values(4,'TV',3000);
a) SELECT concat (cust_fname,” “, cust_lname) as name FROM CUSTOMER
WHERE cust_balance=0;

b) CREATE trigger update_customer


BEFORE INSERT ON invoice
FOR each row
UPDATE customer c
SET c.cust_balance=c.cust_balance+new.inv_amount
WHERE cust_num=new.cust_num;

insert into invoice values(1,3,1,2020-05-23,2,10000);


insert into invoice values(2,1,7,2020-06-23,4,10000);
c) SELECT cust_num , concat(cust_fname,” “,cust_lname) as name FROM customer
WHERE cust_num IN (SELECT cust_num from invoice GROUP by cust_num , inv_date,
. proud_num having sum(unit_sold)>3);

d) LEFT JOIN
SELECT concat(cust_fname," ",cust_lname) as NAME,
i.inv_amount FROM customer c
LEFT JOIN invoice i
ON c.cust_num=i.cust_num;
RIGHT JOIN
SELECT concat(cust_fname," ",cust_lname) as NAME,
i.inv_amount FROM customer c
RIGHT JOIN invoice i
ON c.cust_num=i.cust_num;
FULL JOIN

SELECT concat(cust_fname," ",cust_lname) as NAME,


i.inv_amount FROM customer c
LEFT JOIN invoice i
ON i.cust_num=c.cust_num
UNION
SELECT concat(cust_fname," ",cust_lname) as name,
i.inv_amount FROM customer c
RIGHT JOIN invoice i
ON i.cust_num=c.cust_num;
e) SELECT inv_date , sum(unit_sold) as totalsaleInaDAy
FROM invoice
GROUP by inv_date;

f) CREATE trigger in_gold


-> after update on customer
-> FOR each row
-> INSERT into GOLD_CUSTOMER
-> (SELECT cust_num,cust_lnmae,cust_fname
-> FROM customer
-> WHERE cust_num=new.cust_num
-> and cust_balance>100000
-> and cust_num not in (SELECT cust_num from gold_customer));

G) ALTER table customer


-> add column dob date;

Q 3: Consider the following relational schema


DEPARTMENT(Department_ID, Name, Location_ID)
JOB (Job_ID , Function )
EMPLOYEE (Employee_ID, name, DOB, Job_ID , Manager_ID, Hire_Date, Salary,
department_id)
Answer the following queries using SQL and relational algebra:
a) Write a query to count number of employees who joined in March 2019
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.
DEPARTMENT
CREATE TABLE DEPARTMENT(
Department ID int PRIMARY KEY,
name varchar(256) NOT NULL,
Location ID int);
insert into department values (1,'security',100);
insert into department values (2,'HR',100);
insert into department values (3,'software',102);
insert into department values (4,'finance',103);
insert into department values (5,'hardware',100);

JOB
create table job (Job_ID int primary key,Function varchar(256));
insert into job values(1000,'Works In Hardware');
insert into job values(1001,'Works In Security');
insert into job values(1002,’Works in Software');
insert into job values(1003,'Part Of HR');
insert into job values(1004,'Works In finanace team');

EMPLOYEE
create table employee(Employee_ID int,
Name varchar(256),
dob date,
Job_ID int,
Manager_ID int ,
Hire_Date date,
Salary int,
department_ID int,
Foreign key(Job_ID) references job(Job_ID),
Foreign key(Department_ID) references department (Department_ID));
insert into employee values (10,’ravi’,’2000-05-24’,1000,500,2000-09-23,10000,5);
insert into employee values (11,’aman’,’2005-05-24’,1003,500,2005-09-23,20000,2);
insert into employee values (12,’rohit’,’1990-05-24’,1002,501,2008-09-23,100000,3);
insert into employee values (13,’ram’,’1995-05-24’,1001,502,1990-09-30,40000,1);
insert into employee values (14,’yash’,’1981-09-24’,1004,502,2019-03-30,50000,4);
insert into employee values (15,’yash’,’1985-05-24’,1004,503,1981-10-24,60000,4);

a) SELECT count(Employee_ID) FROM employee


WHERE Hire_Date>'2019-03-01' and Hire_Date<'2019-04-01';

b) Select * from employee


order by salary desc
limit 1;
c) SELECT sum(salary) ,department_id
FROM employee group by department_id;

d) SELECT sum(salary) ,department_id


FROM employee group by department_id
ORDER by sum(salary) desc limit 1;

E) CREATE view del_population as select


COUNT(Employee_ID) from Employee, department
WHERE Location_ID=100;

You might also like