Database Management Systems Practical File: ROLL NUMBER - 2018UCO1615 NAME - Amogh Agarwal COE-2
Database Management Systems Practical File: ROLL NUMBER - 2018UCO1615 NAME - Amogh Agarwal COE-2
Database Management Systems Practical File: ROLL NUMBER - 2018UCO1615 NAME - Amogh Agarwal COE-2
SYSTEMS
PRACTICAL FILE
RELATIONAL ALGEBRA
2) Find names of sailors who’ve reserved a red or a green boat in the month of March.
SQL QUERY
SELECT DISTINCT(sname)
FROM sailors JOIN reserves
ON sailors.sid = reserves.sid
JOIN boats ON boats.bid = reserve.bid
WHERE rdate LIKE ‘%-03-%’ AND (color = ‘red’ or color = ‘green’) ;
RELATIONAL ALGEBRA
SQL QUERY
SELECT sailors.sname
FROM
(SELECT DISTINCT(reserves.sid)
FROM boats JOIN reserves
ON boats.bid = reserves.bid
WHERE color = 'red' and reserves.sid
IN
(SELECT DISTINCT(reserves.sid)
FROM boats JOIN reserves
ON boats.bid = reserves.bid
WHERE color = 'green'))
AS boats JOIN sailors
WHERE boats.sid = sailors.sid;
RELATIONAL ALGEBRA
4) Find sid of sailors who have not reserved a boat after Jan 2018.
SQL QUERY
SELECT DISTINCT(sailors.sid)
FROM sailors JOIN reserves
ON sailors.sid = reserves.sid
WHERE rdate < ‘2018-01-01’;
RELATIONAL ALGEBRA
5) Find sailors whose rating is greater than that of all the sailors named “John”.
SQL QUERY
SELECT *
FROM sailors
WHERE rating >
(SELECT rating from sailors WHERE sname = ‘John’);
RELATIONAL ALGEBRA
SQL QUERY
SELECT S.sname
FROM sailors S
WHERE NOT EXISTS (SELECT B.bid
FROM boats B SELECT S.sname
FROM sailors S
WHERE NOT EXISTS (SELECT B.bid
FROM boats B
WHERE NOT EXISTS(SELECT R.bid
FROM reserves R
WHERE R.bid = B.bid
AND R.sid = S.sid));
WHERE NOT EXISTS(SELECT R.bid
FROM reserves R
WHERE R.bid = B.bid
AND R.sid = S.sid));
7) Find name and age of the oldest sailor(s).
SQL QUERY
SELECT sname ,
FLOOR(DATEDIFF(CURDATE(),date_of_birth)/365.25) AS AGE
FROM sailors WHERE FLOOR(DATEDIFF(CURDATE(),date_of_birth)/365.25) >= ALL (SELECT
FLOOR(DATEDIFF(CURDATE(),date_of_birth)/365.25) AS AGE
FROM sailors);
RELATIONAL ALGEBRA
8) Find the age of the youngest sailor for each rating with at least 2 such sailors.
SQL QUERY
SELECT MIN(FLOOR(DATEDIFF(CURDATE(),date_of_birth)/365.25)) AS AGE , rating FROM
sailors
GROUP BY rating HAVING COUNT(*) > 1;
RELATIONAL ALGEBRA
Q 2: Consider the following relational schema:
Cust_Fname varchar(20),
Cust_Lname varchar(20),
Cust_Balance int
);
);
Prod_Name varchar(20),
Price int
);
Prod_Num int,
Unit_Sold int ,
Cust_Num int ,
Inv_Date date,
Inv_Amount int
);
insert into INVOICE values(1, 101, 1, 3, '2019-07-21', 10000);
SQL QUERY
SELECT CUST_LNAME, CUST_FNAME FROM CUSTOMER UNION SELECT CUST_LNAME,
CUST_FNAME FROM CUSTOMER_2;
RELATIONAL ALGEBRA
SQL QUERY
SELECT C2.CUST_LNAME, C2.CUST_FNAME FROM CUSTOMER_2 C2 WHERE
CONCAT(C2.CUST_LNAME,C2.CUST_FNAME) NOT IN (SELECT
CONCAT(C1.CUST_LNAME,C1.CUST_FNAME) FROM CUSTOMER_1 C1);
RELATIONAL ALGEBRA
4) Write query that will show invoice number, avg invoice amount and their
difference
SQL QUERY
SELECT INV_NUM, INV_AMOUNT, (SELECT AVG(INV_AMOUNT) FROM INVOICE) AS AVG_INV,
(INV_AMOUNT-(SELECT AVG(INV_AMOUNT) FROM INVOICE)) AS DIFF FROm INVOICE GROUP
BY INV_NUM, INV_AMOUNT;
RELATIONAL ALGEBRA
5) Modify CUSTOMER_1 table to include two new attributes:
CUST_DOB and CUST_AGE
Customer 1 was born on 15 march 1969
And customer 2 on 22 December 1977
SQL QUERY
alter table CUSTOMER_1 add column cust_dob date;
alter table CUSTOMER_1 add column cust_age int;
update CUSTOMER_1 set cust_dob = "1969-03-15" where cust_num = 1;
update CUSTOMER_1 set cust_dob = "1977-12-22" where cust_num = 2;
RELATIONAL ALGEBRA
SQL QUERY
update CUSTOMER_1 set cust_age = datediff(curdate(),cust_dob)/365;
RELATIONAL ALGEBRA
7) Find all products with price greater or equal to average product price
SQL QUERY
select * from product where price>=(select avg(price) from product);
Relational Algebra
8) List all products whose total quantity sold is greater than the average
quantity sold
SQL Query
select * from invoice group by prod_num having sum(unit_sold)>(select avg(unit_sold) from
invoice );
Relational Algebra
9) Produce list of product code , price, average price, and the difference
between product price and average price
SQL Query
Select prod_num , price ,(select avg(price) from product) as "average", (price-(select
avg(price) from product)) as "product price-average" from product;
Location_ID INT
);
function varchar(100)
);
Employee_ID int,
Name varchar(255),
dob date,
Job_ID int,
Manager_ID int,
Hire_Date date,
Salary int,
department_ID INT,
);
SQL QUERY
SELECT COUNT(Employee_ID)
FROM Employee
WHERE hire_date> '2015-03-01' And hire_date<'2015-04-01';
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
5) Create a view to show number of employees working in Delhi and update it automatically
when the database is modified.
SQL QUERY
CREATE VIEW DELHI_POPULATION ASSELECT COUNT(EMPLOYEE_ID)FROM
EMPLOYEE,DEPARTMENTWHERE LOCATION_id=10;
RELATIONAL ALGEBRA
6) Write a trigger to ensure that no employee of age less than 25 can be inserted in the
database.
SQL QUERY
delimiter $$
CREATE TRIGGER Check_age BEFORE INSERT ON employee
FOR EACH ROW
BEGIN
IF NEW.dob > 1993-01-01 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ERROR:
AGE MUST BE ATLEAST 25 YEARS!';
END IF;
END;