Project File
Project File
System
PROJECT
FILE
2)Find names of sailors who have reserverd a red or 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 IN ('Red', 'Green') AND MONTH(R.date) = 3;
3)Find the Name of Sailors who have reserved a red and a green boat
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' AND S.sid IN (
SELECT S2.sid
FROM SAILORS S2
JOIN RESERVES R2 ON S2.sid = R2.sid
JOIN BOATS B2 ON R2.bid = B2.bid
WHERE B2.color = 'Green'
);
4)Find sid of sailors who have not reseverd a boat after jan 2018
SELECT DISTINCT S.sid
FROM SAILORS S
WHERE NOT EXISTS (
SELECT 1
FROM RESERVES R
WHERE S.sid = R.sid AND R.date > '2018-01-31'
);
5)Find Sailors whose rating is greater than of all the sailors named “john”
SELECT *
FROM SAILORS
WHERE rating > (
SELECT MAX(rating)
FROM SAILORS
WHERE sname = 'John'
);
1) find the names of the customer who have purchased no item. set default value of
cust_balance as 0 for such customer
SELECT cust_fname, cust_lname
FROM CUSTOMERS
WHERE cust_num NOT IN (SELECT cust_num FROM
INVOICE);
2) Write the trigger to update the cust_balance in customer table when a new
invoice record is entered for the customer
DELIMITER //
CREATE TRIGGER update_cust_balance
AFTER INSERT ON INVOICE
FOR EACH ROW
BEGIN
UPDATE CUSTOMERS
SET cust_balance = cust_balance + NEW.inv_amount
WHERE cust_num = NEW.cust_num;
END;
//
DELIMITER ;
3) Find the customers who have purchased more than 3 units of a product on a day
SELECT DISTINCT C.cust_fname, C.cust_lname
FROM CUSTOMERS C
JOIN INVOICE I ON C.cust_num = I.cust_num
WHERE I.unit_sold > 3;
4) write a query to illustrate left outer ,right outer and full outer join
-- Left outer join
SELECT *
FROM CUSTOMERS
LEFT JOIN INVOICE ON CUSTOMERS.cust_num =
INVOICE.cust_num;
6) Write a trigger to ensure that no employee of age less than 25 can be inserted in
the database
DELIMITER //
CREATE TRIGGER check_employee_age
BEFORE INSERT ON EMPLOYEE
FOR EACH ROW
BEGIN
IF DATEDIFF(CURDATE(), NEW.DOB) < (25 * 365)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Employee must be at least
25 years old';
END IF;
END;
//
DELIMITER ;