Lab 16 02
Lab 16 02
Lab 16 02
4. Table
employee_name VARCHAR(255),
department_id INT,
job_id VARCHAR(50),
salary DECIMAL(10, 2)
);
SELECT COUNT(*)
FROM employees
SELECT MIN(salary)
FROM employees
FROM employees;
d. List the minimum and maximum salaries for each job type:
FROM employees
GROUP BY job_id;
SELECT employee_name
FROM employees
FROM employees
5. Tables
CREATE TABLE sailors (
sid INT PRIMARY KEY,
sname VARCHAR(255),
age INT,
rating INT
);
);
reservation_date DATE,
PRIMARY KEY (sid, bid),
FOREIGN KEY (sid) REFERENCES sailors(sid),
FOREIGN KEY (bid) REFERENCES boats(bid)
);
a. Find the sids, names of sailors who have reserved all boats called “INTERLAKE”:
SELECT *
FROM boats b
WHERE b.bname = 'INTERLAKE'
AND NOT EXISTS (
SELECT *
FROM reservations r
WHERE r.bid = b.bid
AND r.sid = s.sid
)
);
b. Find the sname, bid, and reservation date for each reservation:
c. Find the ages of sailors whose name begins and ends with B and has at least 3 characters:
SELECT s.age
FROM sailors s
WHERE s.sname LIKE 'B%B' AND LENGTH(s.sname) >= 3;
d. List in alphabetic order all sailors who have reserved a red boat:
SELECT s.sname
FROM sailors s
JOIN reservations r ON s.sid = r.sid
JOIN boats b ON r.bid = b.bid
WHERE b.color = 'red'
e. Find the age of the youngest sailor for each rating level:
GROUP BY rating;
6. Tables
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
ProductType VARCHAR(255),
Price DECIMAL(10, 2),
OrderDate DATE,
Vendor VARCHAR(255),
VendorType VARCHAR(50)
);
a. List the Vendors who have delivered products within 6 months from order date:
SELECT Vendor
FROM Orders
WHERE DATEDIFF(MONTH, OrderDate, GETDATE()) <= 6;
b. Display the Vendor details who have supplied both Assembled and Subparts:
SELECT Vendor
FROM Orders
GROUP BY Vendor
c. Display the Sub parts by grouping the Vendor type (Local or NonLocal):
SELECT *
FROM Vendors
ORDER BY Vendor ASC;
e. Display the Sub part which costs more than any of the Assembled parts:
SELECT ProductType
FROM Orders
WHERE Price > ALL (SELECT Price FROM Orders WHERE ProductType = 'Assembled');
SELECT MAX(Price)
FROM Orders
WHERE ProductType = 'Assembled'
AND Price NOT IN (SELECT MAX(Price) FROM Orders WHERE ProductType = 'Assembled');
WK 4
a. PL/SQL program to swap two numbers:
DECLARE
temp NUMBER;
BEGIN
temp := num1;
num1 := num2;
num2 := temp;
END;
DECLARE
num1 NUMBER := 10;
num2 NUMBER := 20;
num3 NUMBER := 15;
max_num NUMBER;
BEGIN
IF num1 >= num2 AND num1 >= num3 THEN
max_num := num1;
ELSIF num2 >= num1 AND num2 >= num3 THEN
max_num := num2;
ELSE
max_num := num3;
END IF;
2.
a. PL/SQL program to find the total and average of 6 subjects and display the grade:
DECLARE
subject1 NUMBER := 80;
subject2 NUMBER := 75;
subject3 NUMBER := 85;
subject4 NUMBER := 90;
BEGIN
total := subject1 + subject2 + subject3 + subject4 + subject5 + subject6;
average := total / 6;
grade := 'A';
ELSIF average >= 80 THEN
grade := 'B';
ELSIF average >= 70 THEN
grade := 'C';
ELSIF average >= 60 THEN
grade := 'D';
ELSE
grade := 'F';
END IF;
END;
DECLARE