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

dbmsassignment8

The document contains SQL queries for managing data related to products, employees, warehouses, movie theaters, pieces and providers, scientists, and hospital procedures. It includes selection, insertion, updating, and deletion operations across various tables, demonstrating how to retrieve and manipulate data effectively. The queries cover a wide range of functionalities, from calculating averages and counts to filtering results based on specific conditions.

Uploaded by

bipos26556
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

dbmsassignment8

The document contains SQL queries for managing data related to products, employees, warehouses, movie theaters, pieces and providers, scientists, and hospital procedures. It includes selection, insertion, updating, and deletion operations across various tables, demonstrating how to retrieve and manipulate data effectively. The queries cover a wide range of functionalities, from calculating averages and counts to filtering results based on specific conditions.

Uploaded by

bipos26556
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SHIKHA SHAH 2426035

The Computer Store

1. Select the names of all the products in the store.

Select Name From Products;

2. Select the names and the prices of all the products in the store.

Select Name, Price From Products;

3. Select the name of the products with a price less than or equal to $200.

Select Name From Products Where Price <= 200;

4. Select all the products with a price between $60 and $120.

Select * From Products Where Price Between 60 And 120;

5. Select the name and price in cents (i.e., the price must be multiplied by 100).

Select Name, Price * 100 From Products;

6. Compute the average price of all the products.

Select Avg(Price) From Products;

7. Compute the average price of all products with manufacturer code equal to 2.

Select Avg(Price) From Products Where Manufacturer=2;

8. Compute the number of products with a price larger than or equal to $180.

Select Count(*) From Products Where Price >= 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.

Select * From Products, Manufacturers Where Products.Manufacturer = Manufacturers.Code;

11. Select the product name, price, and manufacturer name of all the products.

Select Products.Name, Price, Manufacturers.Name From Products, Manufacturers Where

Products.Manufacturer = Manufacturers.Code;

12. Select the average price of each manufacturer's products, showing only the manufacturer's code.

Select Avg(Price), Manufacturer From Products Group By Manufacturer;

13. Select the average price of each manufacturer's products, showing the manufacturer's name.

Select Avg(Price), Manufacturers.Name From Products, Manufacturers Where

Products.Manufacturer = Manufacturers.Code Group By Manufacturers.Name;


SHIKHA SHAH 2426035

14. Select the names of manufacturer whose products have an average price larger than or equal to

$150.

Select Avg(Price), Manufacturers.Name From Products, Manufacturers Where

Products.Manufacturer = Manufacturers.Code Group By Manufacturers.Name Having Avg(Price) >=

150;

15. Select the name and price of the cheapest product.

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

And A.Price = (Select Max(A.Price) From Products A Where A.Manufacturer = F.Code);

17. Select the name of each manufacturer which have an average price above $145 and contain at

least 2 different products.

Select M.Name, Avg(P.Price) As P_Price, Count(P.Manufacturer)As M_Count From Manufacturers

M, Products P Where P.Manufacturer = M.Code Group By M.Name , P.Manufacturer Having

Avg(P.Price) >= 150 And Count(P.Manufacturer) >= 2;

18. Add a new product: Loudspeakers, $70, manufacturer 2.

Insert Into Products( Code, Name , Price , Manufacturer) Values ( 11, 'Loudspeakers' , 70 , 2 );

19. Update the name of product 8 to "Laser Printer".

Update Products Set Name = 'Laser Printer' Where Code = 8;

20. Apply a 10% discount to all products.

Update Products Set Price = Price - (Price * 0.1);

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

1. Select the last name of all employees.

SELECT LastName FROM Employees;

2. Select the last name of all employees, without duplicates.

SELECT DISTINCT LastName FROM Employees;

3. Select all the data of employees whose last name is "Smith".

SELECT * FROM Employees WHERE LastName = 'Smith';

4. Select all the data of employees whose last name is "Smith" or "Doe".

SELECT * FROM Employees WHERE LastName = 'Smith' OR LastName = 'Doe';

5. Select all the data of employees that work in department 14.

SELECT * FROM Employees WHERE Department = 14;

6. Select all the data of employees that work in department 37 or department 77.

SELECT * FROM Employees WHERE Department = 37 OR Department = 77;

7. Select all the data of employees whose last name begins with an "S".

SELECT * FROM Employees WHERE LastName LIKE 'S%';

8. Select the sum of all the departments' budgets.

SELECT SUM(Budget) FROM Departments;

9. Select the number of employees in each department (you only need to show the department

code and the number of employees).

SELECT Department, COUNT(*) FROM Employees GROUP BY Department;

10. Select all the data of employees, including each employee's department's data.

SELECT * FROM Employees E INNER JOIN Departments D ON E.Department = D.Code;

11. Select the name and last name of each employee, along with the name and budget of the

employee's department.

SELECT Employees.Name, LastName, Departments.Name AS DepartmentsName, Budget FROM

Employees INNER JOIN Departments ON Employees.Department = Departments.Code;

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

Departments WHERE Budget > 60000);

13. Select the departments with a budget larger than the average budget of all the departments.

SELECT * FROM Departments WHERE Budget > (SELECT AVG(Budget)FROM Departments);


SHIKHA SHAH 2426035

14. Select the names of departments with more than two employees.

SELECT Name FROM Departments WHERE Code IN( SELECT Department FROM Employees

GROUP BY Department HAVING COUNT(*) > 2);

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.

INSERT INTO Departments VALUES ( 11 , 'Quality Assurance' , 40000);

INSERT INTO Employees VALUES ( '847219811' , 'Mary' , 'Moore' , 11);

17. Reduce the budget of all departments by 10%.

UPDATE Departments SET Budget = Budget * 0.9;

18. Reassign all employees from the Research department (code 77) to the IT department (code

14).

UPDATE Employees SET Department = 14 WHERE Department = 77;

19. Delete from the table all employees in the IT department (code 14).

DELETE FROM Employees WHERE Department = 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

Budget >= 60000);

21. Delete from the table all employees.

DELETE FROM Employees;

The Warehouse

1. Select all warehouses.


SHIKHA SHAH 2426035

SELECT * FROM Warehouses;

2. Select all boxes with a value larger than $150.

SELECT * FROM Boxes WHERE Value > 150;

3. Select all distinct contents in all the boxes.

SELECT DISTINCT Contents FROM Boxes;

4. Select the average value of all the boxes.

SELECT AVG(Value) FROM Boxes;

5. Select the warehouse code and the average value of the boxes in each warehouse.

SELECT Warehouse, AVG(Value) FROM Boxes GROUP BY Warehouse;

6. Same as previous exercise, but select only those warehouses where the average value of the

boxes is greater than 150.

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.

SELECT Boxes.Code, Location FROM Warehouses INNER JOIN Boxes ON Warehouses.Code =

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

zero, instead of omitting the warehouse from the result).

SELECT Warehouses.Code, COUNT(Boxes.Code) FROM Warehouses LEFT JOIN Boxes ON

Warehouses.Code = Boxes.Warehouse GROUP BY Warehouses.Code;

9. Select the codes of all warehouses that are saturated (a warehouse is saturated if the number

of boxes in it is larger than the warehouse's capacity).

SELECT Code FROM Warehouses WHERE Capacity < (SELECT COUNT(*) FROM Boxes WHERE

Warehouse = Warehouses.Code);

10. Select the codes of all the boxes located in Chicago.

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.

INSERT INTO Warehouses (Code,Location,Capacity) VALUES (6,'New York',3);


SHIKHA SHAH 2426035

12. Create a new box, with code "H5RT", containing "Papers" with a value of $200, and located in

warehouse 2.

INSERT INTO Boxes VALUES('H5RT','Papers',200,2);

13. Reduce the value of all boxes by 15%.

UPDATE Boxes SET Value = Value * 0.85;

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

> (SELECT AVG(value) FROM Boxes));

15. Remove all boxes with a value lower than $100.

DELETE FROM Boxes WHERE Value < 100;

16. Remove all boxes from saturated warehouses.

DELETE FROM Boxes WHERE Warehouse IN (SELECT W.Code FROM Warehouses W WHERE

W.Capacity <= (SELECT COUNT(*) FROM Boxes B WHERE B.Warehouse = W.Code));

Movie Theatres

1. Select the title of all movies.

SELECT Title FROM Movies;

2. Show all the distinct ratings in the database.

SELECT DISTINCT Rating FROM Movies;

3. Show all unrated movies.

SELECT * FROM Movies WHERE Rating IS NULL;

4. Select all movie theaters that are not currently showing a movie.

SELECT * FROM MovieTheaters WHERE Movie IS NULL;

5. Select all data from all movie theaters and, additionally, the data from the movie that is being

shown in the theater (if one is being shown).

SELECT * FROM MovieTheaters JOIN Movies ON MovieTheaters.Movie = Movies.Code;

6. Select all data from all movies and, if that movie is being shown in a theater, show the data

from the theater.


SHIKHA SHAH 2426035

SELECT * FROM MovieTheaters RIGHT JOIN Movies ON MovieTheaters.Movie = Movies.Code;

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

Movie IS NOT NULL);

8. Add the unrated movie "One, Two, Three".

INSERT INTO Movies(Code,Title,Rating) VALUES(10,'One, Two, Three',NULL);

9. Set the rating of all unrated movies to "G".

UPDATE Movies SET Rating='G' WHERE Rating IS NULL;

10. Remove movie theaters projecting movies rated "NC-17".

DELETE FROM MovieTheaters WHERE Movie IN (SELECT Code FROM Movies WHERE Rating =

'NC-17');

Pieces And Providers

1. Select the name of all the pieces.

SELECT Name FROM Pieces;

2. Select all the provider’s data.

SELECT * FROM Providers;

3. Obtain the average price of each piece (show only the piece code and the average price).

SELECT Piece, AVG(Price) FROM Provides GROUP BY Piece;

4. Obtain the names of all providers who supply piece 1.

SELECT Name FROM Providers WHERE Code IN (SELECT Provider FROM Provides WHERE Piece =

1);

5. Select the name of pieces provided by provider with code "HAL".

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

at the most expensive price).

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)

FROM Provides WHERE Piece = Pieces.Code);

7. Add an entry to the database to indicate that "Skellington Supplies" (code "TNBC") will

provide sprockets (code "1") for 7 cents each.

INSERT INTO Provides VALUES (1, 'TNBC', 7);

8. Increase all prices by one cent.

UPDATE Provides SET Price = Price + 1;

9. Update the database to reflect that "Susan Calvin Corp." (code "RBT") will not supply bolts

(code 4).

DELETE FROM Provides WHERE Provider = 'RBT' AND Piece = 4;

10. Update the database to reflect that "Susan Calvin Corp." (code "RBT") will not supply any

pieces (the provider should still remain in the database).

DELETE FROM Provides WHERE Provider = 'RBT';

Scientists

1. List all the scientists' names, their projects' names, and the hours worked by that scientist on

each project, in alphabetical order of project name, then scientist name.

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

received a 1.5kg package?

SELECT Client.Name FROM Client WHERE Client.AccountNumber IN (SELECT Package.Recipient

FROM Package WHERE Package.Weight = 1.5);


SHIKHA SHAH 2426035

2. What is the total weight of all the packages that he sent?

SELECT p.weight FROM Client c JOIN Package p ON c.AccountNumber = p.Sender WHERE

c.Name = 'Al Gore''s Head';

3. Which pilots transported those packages?

SELECT Employee.Name FROM Employee JOIN Shipment ON Shipment.Manager =

Employee.EmployeeID JOIN Package ON Package.Shipment = Shipment.ShipmentID WHERE

Shipment.ShipmentID IN ( SELECT p.Shipment FROM Client c JOIN Package p ON

c.AccountNumber = p.Sender WHERE c.AccountNumber = (SELECT Client.AccountNumber

FROM Client JOIN Package ON Client.AccountNumber = Package.Recipient WHERE

Package.Weight = 1.5))GROUP BY Employee.Name;

The Hospital

1.Obtain the names of all physicians that have performed a medical procedure they

have never been certified to perform.

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

procedure was carried out on.

SELECT P.Name AS Physician, Pr.Name AS Procedure, U.DATEUNDERGOES AS Procedure_Date,

Pt.Name AS Patient FROM Physician P JOIN Undergoes U ON U.PHYSICIAN = P.EmployeeID JOIN

Patient Pt ON U.PATIENT = Pt.SSN JOIN Procedure Pr ON U.PROCEDURE = Pr.Code WHERE NOT

EXISTS (SELECT * FROM Trained_In T WHERE T.Treatment = U.PROCEDURE AND T.PHYSICIAN =

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

the physician's certification expired (Trained_In.CertificationExpires).

SELECT Name FROM Physician WHERE EmployeeID IN ( SELECT Physician FROM Undergoes U

WHERE U.DATEUNDERGOES > (SELECT CertificationExpires FROM Trained_In T WHERE


SHIKHA SHAH 2426035

T.Physician = U.PHYSICIAN AND T.Treatment = U.PROCEDURE));

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.

SELECT P.Name AS Physician, Pr.Name AS Procedure, U.DATEUNDERGOES AS ProcedureDate,

Pt.Name AS Patient, T.CertificationExpires FROM Physician P JOIN Undergoes U ON U.Physician

= P.EmployeeID JOIN Patient Pt ON U.Patient = Pt.SSN JOIN Procedure Pr ON U.Procedure =

Pr.Code JOIN Trained_In T ON T.Physician = P.EmployeeID AND T.Treatment = U.Procedure

WHERE U.DATEUNDERGOES > T.CertificationExpires;

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

name of the patient's primary care physician.

SELECT Pt.Name AS Patient, Ph.Name AS Physician, N.Name AS Nurse, A.STARTO AS "Start",

A.ENDO AS "End", A.EXAMINATIONROOM, PhPCP.Name AS PCP FROM Patient Pt JOIN

Appointment A ON A.PATIENT = Pt.SSN JOIN Physician Ph ON A.PHYSICIAN = Ph.EmployeeID

JOIN Physician PhPCP ON Pt.PCP = PhPCP.EmployeeID LEFT JOIN Nurse N ON A.PREPNURSE =

N.EmployeeID WHERE A.PHYSICIAN <> Pt.PCP;

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

rows from Undergoes that exhibit this inconsistency.

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,

Room R WHERE OC.BlockFloor = R.BlockFloor AND OC.BlockCode = R.BlockCode AND

R.ROOMNUMBER = 123);

8. The hospital has several examination rooms where appointments take place. Obtain the
SHIKHA SHAH 2426035

number of appointments that have taken place in each examination room.

N.b. The solution below fails in MS SQL Server Management Studio, with the following message:

Msg 306, Level 16, State 2, Line 473

The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or
LIKE

operator.

SELECT ExaminationRoom, COUNT(AppointmentID) AS "Number" FROM Appointment GROUP

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

was a registered nurse.

• 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

Head FROM Department);

You might also like