0% found this document useful (0 votes)
37 views

DBM Sfile

The document contains questions related to SQL queries on relational database schemas involving tables like SAILORS, BOATS, RESERVES, CUSTOMERS, PRODUCTS, INVOICES, DEPARTMENTS, JOBS, and EMPLOYEES. The questions ask to write queries to find names of sailors who reserved boats of certain colors, customers who made no purchases, number of products sold per date, highest paid employee, total salary budget per department, and more. Triggers and views are also created to automatically update balances and restrict employee age.

Uploaded by

Saneh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

DBM Sfile

The document contains questions related to SQL queries on relational database schemas involving tables like SAILORS, BOATS, RESERVES, CUSTOMERS, PRODUCTS, INVOICES, DEPARTMENTS, JOBS, and EMPLOYEES. The questions ask to write queries to find names of sailors who reserved boats of certain colors, customers who made no purchases, number of products sold per date, highest paid employee, total salary budget per department, and more. Triggers and views are also created to automatically update balances and restrict employee age.

Uploaded by

Saneh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1

DATABASE MANAGEMENT SYSTEMS

Practical File
COCSC05

Submitted By:
Name: Saneh
Rollno.: 2022UCS1545

NETAJI SUBHASH UNIVERSITY OF TECHNOLOGY


DWARKA SECTOR - 3, DELHI
PIN: 110078
2

QUES 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
CREATE DATABASE QUESTION1;
USE QUESTION1;
CREATE TABLE SAILOR(
SID INT PRIMARY KEY ,
sname varchar(50),
rating int,
date_of_birth date
);
INSERT INTO SAILOR VALUES
(1, 'John',3,'2000-05-10'),
(2, 'Jane',1, '2001-08-18'),
(3, 'Bob', 2,'1999-11-25'),
(4, 'Alice', 2, '2002-03-30'),
(5, 'David',4,'2001-07-15');
CREATE TABLE BOATS(
BID INT PRIMARY KEY ,
bname varchar(50),
color VARCHAR(10));
INSERT INTO BOATS VALUES
(1, 'Jon','red'),
(2, 'Jne', 'green'),
(3, 'Job','black'),
(4, 'Aice', 'blue'),
(5, 'Did','red');
CREATE TABLE RESERVES(
SID INT NOT NULL,
3

BID INT NOT NULL,


date date,
timeslot DATETIME,
PRIMARY KEY(date ,timeslot,SID,BID),
FOREIGN KEY (SID) REFERENCES SAILOR(SID),
FOREIGN KEY (BID) REFERENCES BOATS(BID));
INSERT INTO RESERVES VALUES
(1, 2, '2008-11-09 ' ,'2008-03-09 15:45:21'),
(1, 2, '2008-11-09 ' ,'2008-11-09 15:45:21'),
(2,1 , '2008-11-11' , '2008-11-11 13:23:44'),
(3, 1, '2008-11-11 ' ,'2008-11-11 11:12:01'),
(4, 3, '2008-10-29' , '2008-10-29 14:56:59'),
(5,4, '2008-10-09 ' ,'2008-10-09 14:56:59');

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


SELECT DISTINCT S.sid, S.sname
FROM SAILORS S
JOIN RESERVES R ON S.sid = R.sid;

b) Find names of sailors who’ve reserved a red or a green boat in the month of March.
SELECT DISTINCT S.sname
FROM SAILORS S
JOIN RESERVES R ON S.sid = R.sid
JOIN BOATS B ON R.bid = B.bid
WHERE (B.color = 'red' OR B.color = 'green') AND MONTH(R.date) = 3;

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


SELECT DISTINCT S.sname
FROM SAILORS S
JOIN RESERVES R1 ON S.sid = R1.sid
JOIN BOATS B1 ON R1.bid = B1.bid AND B1.color = 'red'
4

JOIN RESERVES R2 ON S.sid = R2.sid


JOIN BOATS B2 ON R2.bid = B2.bid AND B2.color = 'green';

d) Find sid of sailors who have not reserved a boat after Jan 2018.
SELECT DISTINCT S.sid
FROM SAILORS S
WHERE S.sid NOT IN (SELECT DISTINCT R.sid FROM RESERVES R WHERE R.date > '2018-01-31');

e) Find sailors whose rating is greater than that of all the sailors named “John”
SELECT DISTINCT S.sid, S.sname, S.rating
FROM SAILORS S
WHERE S.rating > ALL (SELECT rating FROM SAILORS WHERE sname = 'John');

f) Find sailors who’ve reserved all boats


SELECT DISTINCT S.sid, 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.sid = S.sid AND R.bid = B.bid)
);
g) Find name and age of the oldest sailor(s)
SELECT sname, DATEDIFF(CURDATE(), date_of_birth) AS age
FROM SAILORS
WHERE DATEDIFF(CURDATE(), date_of_birth) = (SELECT MAX(DATEDIFF(CURDATE(),
date_of_birth)) FROM SAILORS);

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

SELECT rating, MIN(DATEDIFF(CURDATE(), date_of_birth) / 365) AS youngest_age


FROM SAILORS
GROUP BY rating
HAVING COUNT(*) >= 2;

QUES 2 : 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);
CREATE DATABASE CUSTOMERDATABASE;
USE CUSTOMERDATABASE;
CREATE TABLE CUSTOMER (
cust_num INT PRIMARY KEY,
cust_lname VARCHAR(255),
cust_fname VARCHAR(255),
cust_balance DECIMAL(10, 2)
);
CREATE TABLE PRODUCT (
prod_num INT PRIMARY KEY,
prod_name VARCHAR(255),
price DECIMAL(10, 2)
);
CREATE TABLE INVOICE (
inv_num INT PRIMARY KEY,
prod_num INT,
cust_num INT,
inv_date DATE,
unit_sold INT,
inv_amount DECIMAL(10, 2),
FOREIGN KEY (prod_num) REFERENCES PRODUCT(prod_num),
FOREIGN KEY (cust_num) REFERENCES CUSTOMER(cust_num)
);
6

INSERT INTO CUSTOMER (cust_num, cust_lname, cust_fname, cust_balance) VALUES


(3, 'Taylor', 'Daniel', 650.50),
(4, 'Jones', 'Emma', 850.75),
(5, 'Davis', 'Michael', 550.30),
(6, 'Anderson', 'Sophia', 900.00),
(7, 'Taylor', 'Daniel', 650.50),
(8, 'Brown', 'Olivia', 1200.75);
INSERT INTO PRODUCT (prod_num, prod_name, price) VALUES
(104, 'Doodad D', 9.99),
(105, 'Contraption E', 49.99),
(106, 'Whatchamacallit F', 34.75),
(107, 'Gizmo G', 24.50),
(108, 'Widget H', 14.99);
INSERT INTO INVOICE (inv_num, prod_num, cust_num, inv_date, unit_sold, inv_amount)
VALUES
(1004, 104, 4, '2023-04-05', 5, 49.95),
(1005, 105, 5, '2023-05-12', 2, 99.98),
(1006, 106, 6, '2023-06-20', 3, 104.25),
(1007, 107, 7, '2023-07-15', 1, 24.50),
(1008, 108, 8, '2023-08-08', 4, 59.96);

Write SQL queries 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.
SELECT cust_fname, cust_lname, COALESCE(cust_balance, 0) AS cust_balance
FROM CUSTOMER
WHERE cust_num NOT IN (SELECT DISTINCT cust_num FROM INVOICE);

b) Write the trigger to update the CUST_BALANCE in the CUSTOMER table when a new invoice
record is entered for the customer.
CREATE TRIGGER update_cust_balance
AFTER INSERT ON INVOICE
FOR EACH ROW
BEGIN
UPDATE CUSTOMER
SET cust_balance = cust_balance + NEW.inv_amount
7

WHERE cust_num = NEW.cust_num;


END;

c) Find the customers who have purchased more than three units of a product on a day.
SELECT DISTINCT cust_num, cust_fname, cust_lname
FROM INVOICE
WHERE unit_sold > 3;

d) Write a query to illustrate Left Outer, Right Outer and Full Outer Join.
-- Left Outer Join
SELECT *
FROM CUSTOMER
LEFT OUTER JOIN INVOICE ON CUSTOMER.cust_num = INVOICE.cust_num;

-- Right Outer Join


SELECT *
FROM CUSTOMER
RIGHT OUTER JOIN INVOICE ON CUSTOMER.cust_num = INVOICE.cust_num;

-- Full Outer Join


SELECT *
FROM CUSTOMER
FULL OUTER JOIN INVOICE ON CUSTOMER.cust_num = INVOICE.cust_num;

e) Count number of products sold on each date.


SELECT inv_date, COUNT(DISTINCT prod_num) AS num_products_sold
FROM INVOICE
GROUP BY inv_date;

f) As soon as customer balance becomes greater than Rs. 100,000, copy the customer_num in
new table called ”GOLD_CUSTOMER”
INSERT INTO GOLD_CUSTOMER (cust_num)
SELECT cust_num
FROM CUSTOMER
WHERE cust_balance > 100000;

g) Add a new attribute CUST_DOB in customer table


ALTER TABLE CUSTOMER
ADD COLUMN CUST_DOB DATE;

QUES.3: Consider the following relational schema


DEPARTMENT(Department_ID, Name, Location_ID)
JOB (Job_ID , Function )
8

EMPLOYEE (Employee_ID, name, DOB, Job_ID , Manager_ID, Hire_Date, Salary,


department_id)

Answer the following queries using SQL:


CREATE DATABASE EMPLOYEE;
USE EMPLOYEE;
CREATE TABLE DEPARTMENT (
Department_ID INT PRIMARY KEY,
Name VARCHAR(255),
Location_ID INT
);

-- Creating JOB Table


CREATE TABLE JOB (
Job_ID INT PRIMARY KEY,
FunctionOfemployee VARCHAR(255)
);
-- Creating EMPLOYEE Table
CREATE TABLE EMPLOYEE (
Employee_ID INT PRIMARY KEY,
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 (Manager_ID) REFERENCES EMPLOYEE(Employee_ID),
FOREIGN KEY (Department_ID) REFERENCES DEPARTMENT(Department_ID)
);

INSERT INTO DEPARTMENT (Department_ID, Name, Location_ID)


VALUES
(1, 'Sales', 101),
(2, 'Marketing', 102),
(3, 'IT', 103),
(4, 'HR', 104),
(5, 'Finance', 105);

INSERT INTO JOB (Job_ID, FunctionOfemployee)


VALUES
(101, 'Sales Representative'),
(102, 'Marketing Specialist'),
9

(103, 'Software Engineer'),


(104, 'HR Manager'),
(105, 'Financial Analyst');

INSERT INTO EMPLOYEE (Employee_ID, Name, DOB, Job_ID, Manager_ID, Hire_Date, Salary,
Department_ID)
VALUES
(1, 'John Doe', '1990-05-15', 101, NULL, '2022-01-01', 50000, 1),
(2, 'Jane Smith', '1985-08-20', 102, 1, '2022-02-15', 60000, 2),
(3, 'Bob Johnson', '1995-03-10', 103, NULL, '2022-03-20', 70000, 3),
(4, 'Emily Brown', '1988-11-25', 104, 2, '2022-04-05', 75000, 4),
(5, 'Emily Brown', '1988-10-25', 104, 2, '2015-03-05', 75000, 4),
(6, 'Michael Davis', '1992-06-15', 105, 1, '2022-05-12', 65000, 5);

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

SELECT COUNT(*) AS employee_count


FROM EMPLOYEE
WHERE MONTH(Hire_Date) = 3 AND YEAR(Hire_Date) = 2015;

b) Display the Nth highest salary drawing employee details.


SELECT *
FROM EMPLOYEE
ORDER BY Salary DESC
LIMIT 1 OFFSET N-1;
c) Find the budget (total salary) of each department.
SELECT department_id, SUM(Salary) AS department_budget
FROM EMPLOYEE
GROUP BY department_id;

d) Find the department with maximum budget.


SELECT department_id
FROM (
SELECT department_id, SUM(Salary) AS department_budget
FROM EMPLOYEE
GROUP BY department_id
ORDER BY department_budget DESC
LIMIT 1
);

e) Create a view to show number of employees working in Delhi and update it automatically
when the database is modified.
CREATE VIEW EmployeesInDelhi AS
10

SELECT department_id, COUNT(*) AS num_employees


FROM EMPLOYEE
JOIN DEPARTMENT ON EMPLOYEE.department_id = DEPARTMENT.Department_ID
WHERE DEPARTMENT.Location_ID = 'Delhi'
GROUP BY department_id;

f) Write a trigger to ensure that no employee of age less than 25 can be inserted in the
database.
CREATE TRIGGER check_employee_age
BEFORE INSERT ON EMPLOYEE
FOR EACH ROW
BEGIN
IF (DATEDIFF(CURDATE(), NEW.DOB) / 365) < 25 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Employee age must be 25 or older';
END IF;
END;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy