dbmsassignment8
dbmsassignment8
2. Select the names and the prices of all the products in the store.
3. Select the name of the products with a price less than or equal to $200.
4. Select all the products with a price between $60 and $120.
5. Select the name and price in cents (i.e., the price must be multiplied by 100).
7. Compute the average price of all products with manufacturer code equal to 2.
8. Compute the number of products with a price larger than or equal to $180.
9. Select the name and price of all products with a price larger than or equal to $180, and sort first
by price (in descending order), and then by name (in ascending order).
Select Name, Price From Products Where Price >= 180 Order By Price Desc, Name;
10. Select all the data from the products, including all the data for each product's manufacturer.
11. Select the product name, price, and manufacturer name of all the products.
Products.Manufacturer = Manufacturers.Code;
12. Select the average price of each manufacturer's products, showing only the manufacturer's code.
13. Select the average price of each manufacturer's products, showing the manufacturer's name.
14. Select the names of manufacturer whose products have an average price larger than or equal to
$150.
150;
Select Name, Price From Products Where Price = (Select Min(Price) From Products);
16. Select the name of each manufacturer along with the name and price of its most expensive
product.
Select A.Name, A.Price, F.Name From Products A, Manufacturers F Where A.Manufacturer = F.Code
17. Select the name of each manufacturer which have an average price above $145 and contain at
Insert Into Products( Code, Name , Price , Manufacturer) Values ( 11, 'Loudspeakers' , 70 , 2 );
21. Apply a 10% discount to all products with a price larger than or equal to $120.
Update Products Set Price = Price - (Price * 0.1) Where Price >= 120;
Employee Management
SHIKHA SHAH 2426035
4. Select all the data of employees whose last name is "Smith" or "Doe".
6. Select all the data of employees that work in department 37 or department 77.
7. Select all the data of employees whose last name begins with an "S".
9. Select the number of employees in each department (you only need to show the department
10. Select all the data of employees, including each employee's department's data.
11. Select the name and last name of each employee, along with the name and budget of the
employee's department.
12. Select the name and last name of employees working for departments with a budget greater
than $60,000.
SELECT Name, LastName FROM Employees WHERE Department IN (SELECT Code FROM
13. Select the departments with a budget larger than the average budget of all the departments.
14. Select the names of departments with more than two employees.
SELECT Name FROM Departments WHERE Code IN( SELECT Department FROM Employees
15. Select the name and last name of employees working for departments with second lowest
budget.
SELECT e.Name, e.LastName FROM Employees e WHERE e.Department = (SELECT Code FROM
(SELECT Code FROM Departments ORDER BY Budget ASC) WHERE ROWNUM = 2);
16. Add a new department called "Quality Assurance", with a budget of $40,000 and
departmental code 11. Add an employee called "Mary Moore" in that department, with SSN
847-21-9811.
18. Reassign all employees from the Research department (code 77) to the IT department (code
14).
19. Delete from the table all employees in the IT department (code 14).
20. Delete from the table all employees who work in departments with a budget greater than or
equal to $60,000.
DELETE FROM Employees WHERE Department IN(SELECT Code FROM Departments WHERE
The Warehouse
5. Select the warehouse code and the average value of the boxes in each warehouse.
6. Same as previous exercise, but select only those warehouses where the average value of the
SELECT Warehouse, AVG(Value)FROM Boxes GROUP BY Warehouse HAVING AVG(Value) > 150;
7. Select the code of each box, along with the name of the city the box is located in.
Boxes.Warehouse;
8. Select the warehouse codes, along with the number of boxes in each warehouse. Optionally,
take into account that some warehouses are empty (i.e., the box count should show up as
9. Select the codes of all warehouses that are saturated (a warehouse is saturated if the number
SELECT Code FROM Warehouses WHERE Capacity < (SELECT COUNT(*) FROM Boxes WHERE
Warehouse = Warehouses.Code);
SELECT Code FROM Boxes WHERE Warehouse IN(SELECT Code FROM Warehouses WHERE
Location = 'Chicago');
11. Create a new warehouse in New York with a capacity for 3 boxes.
12. Create a new box, with code "H5RT", containing "Papers" with a value of $200, and located in
warehouse 2.
14. Apply a 20% value reduction to boxes with a value larger than the average value of all the
boxes.
UPDATE Boxes SET value = value * 0.8 WHERE code IN (SELECT code FROM Boxes WHERE value
DELETE FROM Boxes WHERE Warehouse IN (SELECT W.Code FROM Warehouses W WHERE
Movie Theatres
4. Select all movie theaters that are not currently showing a movie.
5. Select all data from all movie theaters and, additionally, the data from the movie that is being
6. Select all data from all movies and, if that movie is being shown in a theater, show the data
7. Show the titles of movies not currently being shown in any theaters.
SELECT Title FROM Movies WHERE Code NOT IN(SELECT Movie FROM MovieTheaters WHERE
DELETE FROM MovieTheaters WHERE Movie IN (SELECT Code FROM Movies WHERE Rating =
'NC-17');
3. Obtain the average price of each piece (show only the piece code and the average price).
SELECT Name FROM Providers WHERE Code IN (SELECT Provider FROM Provides WHERE Piece =
1);
SELECT Name FROM Pieces WHERE Code IN (SELECT Piece FROM Provides WHERE Provider =
'HAL');
6. For each piece, find the most expensive offering of that piece and include the piece name,
provider name, and price (note that there could be two providers who supply the same piece
SHIKHA SHAH 2426035
SELECT Pieces.Name, Providers.Name, Price FROM Pieces INNER JOIN Provides ON Pieces.Code
= Piece INNER JOIN Providers ON Providers.Code = Provider WHERE Price = (SELECT MAX(Price)
7. Add an entry to the database to indicate that "Skellington Supplies" (code "TNBC") will
9. Update the database to reflect that "Susan Calvin Corp." (code "RBT") will not supply bolts
(code 4).
10. Update the database to reflect that "Susan Calvin Corp." (code "RBT") will not supply any
Scientists
1. List all the scientists' names, their projects' names, and the hours worked by that scientist on
SELECT S.Name, P.Name, P.Hours FROM Scientists S INNER JOIN AssignedTo A ON S.SSN =
A.Scientist INNER JOIN Projects P ON A.Project=P.Code ORDER BY P.Name ASC, S.Name ASC;
Planet Express
1. The first several exercises build on each other and illustrate the use of subqueries.Who
The Hospital
1.Obtain the names of all physicians that have performed a medical procedure they
SELECT Name FROM Physician WHERE EmployeeID IN (SELECT Physician FROM Undergoes U
WHERE NOT EXISTS (SELECT * FROM Trained_In WHERE Treatment = Procedure AND Physician
= U.Physician));
2. Same as the previous query, but include the following information in the results: Physician
name, name of procedure, date when the procedure was carried out, name of the patient the
P.EmployeeID );
3. Obtain the names of all physicians that have performed a medical procedure that they are
certified to perform, but such that the procedure was done at a date (Undergoes.Date) after
SELECT Name FROM Physician WHERE EmployeeID IN ( SELECT Physician FROM Undergoes U
4. Same as the previous query, but include the following information in the results: Physician
name, name of procedure, date when the procedure was carried out, name of the patient the
procedure was carried out on, and date when the certification expired.
5. Obtain the information for appointments where a patient met with a physician other than
his/her primary care physician. Show the following information: Patient name, physician
name, nurse name (if any), start and end time of appointment, examination room, and the
6. The Patient field in Undergoes is redundant, since we can obtain it from the Stay table. There
are no constraints in force to prevent inconsistencies between these two tables. More
specifically, the Undergoes table may include a row where the patient ID does not match the
one we would obtain from the Stay table through the Undergoes.Stay foreign key. Select all
SELECT * FROM Undergoes U WHERE Patient <> (SELECT Patient FROM Stay S WHERE U.Stay =
S.StayID);
7. Obtain the names of all the nurses who have ever been on call for room 123.
SELECT N.Name FROM Nurse N WHERE EmployeeID IN (SELECT OC.Nurse FROM On_Call OC,
R.ROOMNUMBER = 123);
8. The hospital has several examination rooms where appointments take place. Obtain the
SHIKHA SHAH 2426035
N.b. The solution below fails in MS SQL Server Management Studio, with the following message:
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or
LIKE
operator.
BY ExaminationRoom;
9. Obtain the names of all patients (also include, for each patient, the name of the patient's
primary care physician), such that \emph{all} the following are true:
• The patient has been prescribed some medication by his/her primary care physician.
• The patient has undergone a procedure with a cost larger that $5,000
• The patient has had at least two appointment where the nurse who prepped the appointment
• The patient's primary care physician is not the head of any department.
SELECT Pt.Name, PhPCP.Name FROM Patient Pt, Physician PhPCP WHERE Pt.PCP =
PhPCP.EmployeeID AND
EXISTS (SELECT * FROM Prescribes Pr WHERE Pr.Patient = Pt.SSN AND Pr.Physician = Pt.PCP) AND
EXISTS
(SELECT * FROM Undergoes U, Procedure Pr WHERE U.Procedure = Pr.Code AND U.Patient = Pt.SSN
AND
Pr.Cost > 5000) AND 2 <= (SELECT COUNT(A.AppointmentID) FROM Appointment A, Nurse N WHERE
A.PrepNurse = N.EmployeeID AND N.Registered = 1 AND A.Patient = Pt.SSN) AND NOT Pt.PCP IN
(SELECT