Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Database Management Systems Practical File: ROLL NUMBER - 2018UCO1615 NAME - Amogh Agarwal COE-2

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 20

DATABASE MANAGEMENT

SYSTEMS
PRACTICAL FILE

ROLL NUMBER - 2018UCO1615


NAME – Amogh Agarwal
COE-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)
CREATE TABLE sailors (
sid int PRIMARY KEY,
sname varchar(20) ,
rating int NOT NULL,
dob date
);

CREATE TABLE boats (


bid int PRIMARY KEY,
bname varchar(20) NOT NULL,
color varchar(20) NOT NULL
);
CREATE TABLE reserves (
sid int NOT NULL,
bid int NOT NULL,
rdate date,
FOREIGN KEY (sid) REFERENCES sailors(sid),
FOREIGN KEY (bid) REFERENCES boats(bid)
);
INSERT INTO sailors VALUES (102,'Kartik',9,'1997-05-21');
INSERT INTO sailors VALUES (103,'Rohan',5,'1999-06-15');
INSERT INTO sailors VALUES (104,'John',7,'1997-01-10');
INSERT INTO sailors VALUES (105,'Rahul',6,'2000-04-07');
INSERT INTO sailors VALUES (106,'Ram',8,'1998-09-15');
INSERT INTO sailors VALUES (107,'Daksh',8,'1997-12-25');
INSERT INTO sailors VALUES (108,'Jaivin',7,'1999-02-27');
INSERT INTO sailors VALUES (109,'Shubham',5,'1999-05-16');
INSERT INTO sailors VALUES (110,'Hitesh',6,'2000-11-30');
INSERT INTO boats VALUES (1,'B1','red');
INSERT INTO boats VALUES (2,'B2','white');
INSERT INTO boats VALUES (3,'B3','green');
INSERT INTO boats VALUES (4,'B4','black');
INSERT INTO boats VALUES (5,'B5','green');
INSERT INTO boats VALUES (6,'B6','red');
INSERT INTO boats VALUES (7,'B7','green');
INSERT INTO boats VALUES (8,'B8','pink');
INSERT INTO boats VALUES (9,'B9','blue');
INSERT INTO boats VALUES (10,'B10','orange');
INSERT INTO boats VALUES (11,'B11','red');
INSERT INTO boats VALUES (12,'B12','obrown');
INSERT INTO boats VALUES (13,'B13','blue');
INSERT INTO boats VALUES (14,'B14','black');
INSERT INTO boats VALUES (15,'B15','blue');
INSERT INTO boats VALUES (16,'B16','yellow');

INSERT INTO reserves VALUES (102,5,'2016-01-12');


INSERT INTO reserves VALUES (102,6,'2019-03-09');
INSERT INTO reserves VALUES (102,1,'2019-12-09');
INSERT INTO reserves VALUES (103,3,'2018-01-19');
INSERT INTO reserves VALUES (107,4,'2017-04-17');
INSERT INTO reserves VALUES (108,10,'2019-10-01');
INSERT INTO reserves VALUES (107,16,'2019-09-29');
INSERT INTO reserves VALUES (109,11,'2018-07-23');

Write the following queries in SQL and relational algebra


1) Find sailors who’ve reserved at least one boat
SQL QUERY
SELECT DISTINCT(sname)
FROM sailors JOIN reserves
ON sailors.sid = reserves.sid ;

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

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

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

6) Find sailors who’ve reserved all boats.

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:

CUSTOMER (cust_num, cust_lname , cust_fname, cust_balance);

PRDUCT (prod_num, prod_name, price)

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

CREATE TABLE CUSTOMER_1 (

Cust_Num int PRIMARY KEY,

Cust_Fname varchar(20),

Cust_Lname varchar(20),

Cust_Balance int

);

INSERT INTO CUSTOMER_1 VALUES (1,'Kartik','Gupta',200000);

INSERT INTO CUSTOMER_1 VALUES (2,'Ram','Kumar',350000);

INSERT INTO CUSTOMER_1 VALUES (3,'Gaurav','Singh',450000);

INSERT INTO CUSTOMER_1 VALUES (4,'Himanshu','Singh',55000);

INSERT INTO CUSTOMER_1 VALUES (5,'Kunal','Rohilla',34000);

INSERT INTO CUSTOMER_1 VALUES (6,'Shubham','Aggarwal',2200000);

INSERT INTO CUSTOMER_1 VALUES (7,'Jai','Anand',120000);

INSERT INTO CUSTOMER_1 VALUES (8,'Raj','Gupta',540000);

create table CUSTOMER_2 (

Cust_Num int PRIMARY KEY,

Cust_Fname varchar(20) NOT NULL,

Cust_Lname varchar(20) NOT NULL,


Cust_Balance int NOT NULL

);

insert into CUSTOMER_2 values(1,'Pulkit','Singh',8000);

insert into CUSTOMER_2 values(2,'Kunal','Rohilla',14000);

insert into CUSTOMER_2 values(3,'Sahil','Kumar',9000);

insert into CUSTOMER_2 values(4,'Rishabh','Chauhan',55000);

insert into CUSTOMER_2 values(5,'Deepanshu','Jindal',70000);

insert into CUSTOMER_2 values(6,'Rahul','Pathak',800000);

create table PRODUCT (

Prod_Num int PRIMARY KEY,

Prod_Name varchar(20),

Price int

);

insert into PRODUCT values(1,'Mixer',1000);

insert into PRODUCT values(2,'Chair',2500);

insert into PRODUCT values(3,'TV',45000);

insert into PRODUCT values(4,'Mobile',30000);

insert into PRODUCT values(5,'Mouse',1000);

insert into PRODUCT values(6,'Heater',5300);

insert into PRODUCT values(7,'Tubelight',200);

insert into PRODUCT values(8,'Air Conditioner',64000);

create table INVOICE (

Inv_Num int PRIMARY KEY,

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);

insert into INVOICE values(2, 102, 4, 6, '2019-02-28', 8000);

insert into INVOICE values(3, 103, 2, 8, '2019-04-23', 12000);

insert into INVOICE values(4, 104, 1, 5, '2019-03-22', 1000);

insert into INVOICE values(5, 105, 9, 7, '2019-01-08', 5000);

insert into INVOICE values(6, 106, 6, 2, '2019-10-20', 3100);

insert into INVOICE values(7, 105, 14, 1, '2019-02-27', 5000);

insert into INVOICE values(8, 1006, 4, 3, '2019-02-28', 3100);

Write the following queries in SQL and relational algebra

1) Write query to generate a combined list of all customers including


duplicate ones

SQL QUERY
SELECT CUST_LNAME, CUST_FNAME FROM CUSTOMER UNION SELECT CUST_LNAME,
CUST_FNAME FROM CUSTOMER_2;

RELATIONAL ALGEBRA

2) Write query to show duplicate records


SQL QUERY
SELECT C.CUST_LNAME, C.CUST_FNAME FROM CUSTOMER_1 AS C, CUSTOMER_2 AS C2
WHERE C.CUST_LNAME=C2.CUST_LNAME AND C.CUST_FNAME=C2.CUST_FNAME
Relational Algebra
3) Write a query that will generate records which are unique to
CUSTOMER_2

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

6) Assuming CUSTOMER_1 contains CUST_AGE attribute, write queries to


update values in this attribute

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;

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)

CREATE TABLE DEPARTMENT (

Department_ID int PRIMARY KEY,

Name varchar(20) NOT NULL,

Location_ID INT
);

INSERT INTO DEPARTMENT VALUES (1,'Finance',302);

INSERT INTO DEPARTMENT VALUES (2,'Security',706);

INSERT INTO DEPARTMENT VALUES (3,'Human Resources',890);

INSERT INTO DEPARTMENT VALUES (4,'IT',6509);

INSERT INTO DEPARTMENT VALUES (5,'Electronic Dept',651);

INSERT INTO DEPARTMENT VALUES (6,'Software Dept',471);

INSERT INTO DEPARTMENT VALUES (7,'Hardware Dept',491);

CREATE TABLE JOB (

Job_ID int PRIMARY KEY,

function varchar(100)

);

INSERT INTO JOB VALUES(501,'Manager of Department');

INSERT INTO JOB VALUES(502,'Works for Security team');

INSERT INTO JOB VALUES(503,'Works for Human Resources team');

INSERT INTO JOB VALUES(504,'works for IT team');

INSERT INTO JOB VALUES(505,'works for Finance team');

INSERT INTO JOB VALUES(506,'works for Electrical Dept team');

INSERT INTO JOB VALUES(507,'works for Hardware team');

INSERT INTO JOB VALUES(508,'works for Software team');

CREATE TABLE EMPLOYEE (

Employee_ID int,

Name varchar(255),

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(1001,'Kartik','1999-03-14',502,801,'2015-03-18',100000,2);

INSERT INTO EMPLOYEE VALUES(1001,'Himanshu','1997-05-16',502,801,'2016-10-10',50000,2);

INSERT INTO EMPLOYEE VALUES(1001,'Rahul','1999-03-26',504,801,'2015-03-05',60000,4);

INSERT INTO EMPLOYEE VALUES(1001,'Raj','1997-09-28',505,801,'2018-10-11',50000,1);

INSERT INTO EMPLOYEE VALUES(1001,'Chetan','1996-11-17',505,801,'2014-05-28',75000,1);

INSERT INTO EMPLOYEE VALUES(1001,'Sahil','1998-07-23',508,801,'2017-12-15',87000,7);

INSERT INTO EMPLOYEE VALUES(1001,'Shiva','1997-12-05',502,801,'2019-10-18',1000,2);

Write the following queries in SQL and relational algebra

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

SQL QUERY
SELECT COUNT(Employee_ID)
FROM Employee
WHERE hire_date> '2015-03-01' And hire_date<'2015-04-01';

RELATIONAL ALGEBRA

2) Display the Nth highest salary drawing employee details.


SQL QUERY
SELECT *
FROM employee
GROUP BY salary
ORDER BY salary DESC LIMIT 1;

RELATIONAL ALGEBRA

3) Find the budget (total salary) of each department


SQL QUERY
SELECT SUM(SALARY),DEPARTMENT_ID
FROM EMPLOYEE
GROUP BY DEPARTMENT_ID;

RELATIONAL ALGEBRA

4) Find the department with maximum budget.


SQL QUERY
SELECT SUM(SALARY),DEPARTMENT_ID
FROM EMPLOYEE
GROUP BY DEPARTMENT_ID
Order by sum(salary) desc limit 1;

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;

You might also like