DBM Sfile
DBM Sfile
Practical File
COCSC05
Submitted By:
Name: Saneh
Rollno.: 2022UCS1545
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;
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');
h) Find the age of the youngest sailor for each rating with at least 2 such sailors
5
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
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;
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;
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);
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
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;