Docsity

SQL Database Design and Querying Exercises, Exercises of Database Management Systems (DBMS)

Partial preview of the text

Download SQL Database Design and Querying Exercises and more Exercises Database Management Systems (DBMS) in PDF only on Docsity!

INSERT INTO PERSON (driver_id, name, address) VALUES (1, 'John Smith', '123 Main St'), (2, 'Jane Doe', '456 Elm St'), (3, 'Michael Lee', '789 Oak St'), (4, 'Emily Brown', '567 Pine St'), (5, 'David Wang', '890 Maple St'), (6, 'Sarah Chen', '234 Birch St'), (7, 'Lisa Garcia', '345 Cedar St'), (8, 'Kevin Kim', '678 Walnut St'), (9, 'Laura Adams', '901 Spruce St'), (10, 'Brian Hall', '123 Oak St'); INSERT INTO CAR (regno, model, year) VALUES ('ABC123', 'Sedan', 2018), ('XYZ456', 'SUV', 2020), ('DEF789', 'Hatchback', 2019), ('JKL012', 'Truck', 2017), ('MNO345', 'Sedan', 2022), ('PQR678', 'Coupe', 2021), ('STU901', 'SUV', 2015), ('VWX234', 'Hatchback', 2023), ('YZA567', 'Sedan', 2016), ('BCD890', 'Truck', 2018); INSERT INTO OWNS (driver_id, regno) VALUES

(1, 'ABC123'), (2, 'XYZ456'), (3, 'DEF789'), (4, 'JKL012'), (5, 'MNO345'), (6, 'PQR678'), (7, 'STU901'), (8, 'VWX234'), (9, 'YZA567'), (10, 'BCD890'); INSERT INTO PARTICIPATED (driver_id, regno, report_number, damage_amount) VALUES (1, 'ABC123', 101, 1500), (2, 'XYZ456', 102, 2000), (3, 'DEF789', 103, 3000), (4, 'JKL012', 104, 1000), (5, 'MNO345', 105, 2500), (6, 'PQR678', 106, 1800), (7, 'STU901', 107, 1200), (8, 'VWX234', 108, 2200), (9, 'YZA567', 109, 2800), (10, 'BCD890', 110, 1700); INSERT INTO ACCIDENT (report_number, accd_date, location) VALUES (101, '2023-01-15', 'Intersection A'), (102, '2022-05-20', 'Highway B'), (103, '2023-03-10', 'Street C'), (104, '2021-09-05', 'Intersection D'), (105, '2022-11-30', 'Parking Lot E'), (106, '2023-07-12', 'Highway F'),

Questions :

  1. Retrieve the names of drivers who participated in accidents with a damage amount between 1000 and 2000:
  2. Get the registration numbers and models of cars that are either SUVs or Sedans.
  3. Display the accident locations that are not 'Intersection' or 'Highway'.
  4. Retrieve the drivers who own cars that are more than 5 years old.
  5. Retrieve the registration numbers of cars owned by drivers named 'John Smith' or 'Jane Doe'.
  6. Count the number of accidents at each location from the ACCIDENT table, ordered by location.
  7. Display the average damage amount per accident, considering only those with damage greater than 1000.
  8. Retrieve the car models and their counts, ordering by the number of occurrences in ascending order.
  9. List the drivers who participated in more than 2 accidents, ordered by their names.
  10. Display the accident locations and their total damage, considering only those with total damage exceeding 5000, ordered by total damage in descending order:
  11. Retrieve the drivers' names and the maximum damage amount they caused in accidents, ordered by maximum damage in descending order.
  12. List the car registration numbers and their average damage amounts, considering only those with an average damage greater than 1500, ordered by average damage in descending order. Create a Table Train with the following attributes with its constraints: In this relation, the "train" table has the following attributes:
  13. train_id: A unique identifier for each train (assumed to be a numeric value).
  14. train_name: The name or identifier of the train (up to 50 characters).
  15. departure_station: The station where the train departs from (up to 50 characters).
  16. arrival_station: The station where the train arrives at (up to 50 characters).
  17. departure_time: The timestamp of the departure time.
  18. arrival_time: The timestamp of the arrival time.
  19. duration: The duration of the train journey (represented as an interval of days to seconds).
  20. available_seats: The number of available seats on the train.(number up to 3 digits)
  21. ticket_price: The price of a train ticket (with 10 digits in total and up to 2 decimal places).

Values to be entered:

(1, 'Express 1', 'Station A', 'Station Z', TIMESTAMP '2023-08-17 08:00:00',

TIMESTAMP '2023-08-17 16:00:00', INTERVAL '8' HOUR, 150, 50.00);

(2, 'Local 42', 'Station B', 'Station Y', TIMESTAMP '2023-08-17 09:30:00',

TIMESTAMP '2023-08-17 11:30:00', INTERVAL '2' HOUR, 120, 20.50);

(3, 'Rapid 7', 'Station C', 'Station X', TIMESTAMP '2023-08-17 11:00:00',

TIMESTAMP '2023-08-17 14:00:00', INTERVAL '3' HOUR, 200, 35.75);

(4, 'Express 5', 'Station D', 'Station W', TIMESTAMP '2023-08-17 13:45:00',

TIMESTAMP '2023-08-17 17:45:00', INTERVAL '4' HOUR, 100, 45.25);

(5, 'Local 23', 'Station E', 'Station V', TIMESTAMP '2023-08-17 16:20:00',

TIMESTAMP '2023-08-17 18:20:00', INTERVAL '2' HOUR, 180, 18.90);

(6, 'Rapid 9', 'Station F', 'Station U', TIMESTAMP '2023-08-17 18:30:00',

TIMESTAMP '2023-08-17 21:00:00', INTERVAL '2' HOUR + INTERVAL '30'

MINUTE, 80, 28.50);

(7, 'Express 3', 'Station G', 'Station T', TIMESTAMP '2023-08-17 20:15:00',

TIMESTAMP '2023-08-18 02:15:00', INTERVAL '6' HOUR, 50, 60.75);

(8, 'Local 11', 'Station H', 'Station S', TIMESTAMP '2023-08-18 08:45:00',

TIMESTAMP '2023-08-18 10:45:00', INTERVAL '2' HOUR, 160, 15.25);

(9, 'Rapid 2', 'Station I', 'Station R', TIMESTAMP '2023-08-18 11:30:00',

TIMESTAMP '2023-08-18 14:30:00', INTERVAL '3' HOUR, 220, 30.00);

(10, 'Express 8', 'Station J', 'Station Q', TIMESTAMP '2023-08-18 14:00:00',

TIMESTAMP '2023-08-18 20:00:00', INTERVAL '6' HOUR, 120, 55.50);

USE THE WHERE CLAUSE TO FILTER DATA BASED ON SPECIFIC CONDITIONS:

  1. Create a Table Train
  2. Insert into table

HAVING clause is used in SQL to filter the results of aggregate functions applied to grouped data. The HAVING clause works in combination with the GROUP BY clause and is used to filter aggregated data based on conditions involving the result of aggregate functions.

  1. Filtering Grouped Data by Aggregate Value: Write a query to retrieve the departure stations and the count of trains departing from each station, but only for stations with more than two departing trains.
  2. Filtering Groups by Aggregate Calculation: Write a query to calculate the average ticket price for each arrival station, but only includes stations with an average ticket price higher than $30.00.
  3. Filtering Groups with Multiple Conditions: Write a query to retrieve the maximum available seats for each departure station, but only includes stations where the maximum seats are more than 150 and the minimum ticket price is less than $40.00.
  4. Filtering Groups Based on Aggregate Calculation Order: Write a query to retrieve the train names and their latest departure times, but only for trains that have a departure time later than 6:00 PM.
  5. Using HAVING with COUNT and GROUP BY: Write a query to retrieve the departure stations and the count of trains departing from each station, but only includes stations where the number of departing trains is between 2 and 5.

The ORDER BY clause in SQL is used to sort the result set of a query based on one or more columns.

  1. Sorting by a Single Column: Write a query to retrieve train names, departure times, and arrival times, and sorts the result set in ascending order based on the departure times.
  2. Sorting by Multiple Columns: Write a query to retrieve train names, departure times, and arrival times and sorts the result set primarily by departure times in ascending order and then by arrival times in descending order.
  3. Sorting with NULL Values: Write a query to retrieve train names and arrival times and sorts the result set in ascending order based on arrival times. NULL values for arrival times will appear last in the sorted result.
  4. Sorting with Aggregate Functions: Write a query to retrieve the average ticket price for each departure station, groups the result by station, and then sorts the stations by average price in descending order.
  5. Sorting with Expression: Write a query to calculate the travel duration in hours between departure and arrival times, and then sorts the result set in descending order based on this calculated duration. HOTEL MENU APPLICATION Table: Menu Items ItemI D ItemName Price 1 Burger 5. 2 Pizza 8. 3 Pasta 7. 4 Salad 3. 5 Soda 1. 6 Chicken 6. 7 Fries 2. 8 Ice Cream 4. 9 Hot Dog 4. 10 Sandwich 6. 11 Milkshake 4. 12 Nachos 5. 13 Smoothie 4. 14 Fried Rice 7. 15 Tacos 6. 16 Chicken BBQ 9. 17 Sushi 10. 18 Wings 8.

Outpu t Ques Query 6: Find menu items with a total quantity ordered greater than or equal to 5. Query Outpu t Ques Query 7: Retrieve a combined list of customer names from the Orders table and Item names from the MenuItems table. Query Outpu t Ques Query 8: Combine the orders placed on two different dates. Query Outpu t Ques Query 9: Find menu items that were ordered on both '2023-09-25' and '2023-09-26'. Query Outpu t Ques Query 10: Find customers who placed orders on both '2023-09-27' and '2023-09-28'. Query Outpu t Ques Query 11: Retrieve a list of orders with their corresponding menu item details. Query Outpu t Ques Query 12: Find orders placed by customer 'Alice' and their associated menu items. Query Outpu t Ques Query 13: Retrieve all orders and menu items, including those without matches in either table. Query Outpu t Ques Query 14: Find menu items with no orders and orders with no associated menu items. Query Outpu t Ques Query 15: Retrieve a list of orders with their corresponding menu item details using a natural join. Query Outpu t Ques Query 16: Find orders placed by customers where the customer’s name matches the menu item name. Query Outpu

t Ques Query 17: Retrieve all orders and their associated menu item details, including orders with no menu item matches. Query Outpu t Ques Query 18: Find menu items and any orders associated with them, including menu items without orders. Query Outpu t Ques Query 19: Retrieve all menu items and any orders associated with them, including menu items without orders. Query Outpu t Ques Query 20: Find orders and their associated menu item details, including orders with no menu item matches. Query Outpu t

CATERING SERVICES APPLICATION

Table 1: Customers This table stores information about the customers who are using the catering services. Field Name Data Type Description CustomerID INT Unique customer identifier FirstName VARCHAR First name of the customer LastName VARCHAR Last name of the customer Email VARCHAR Email address of the customer Phone VARCHAR Phone number of the customer Address VARCHAR Customer's address Table 2: Events This table contains details about the events for which catering services are being provided. Field Name Data Type Description EventID INT Unique event identifier EventName VARCHAR Name of the event EventDate DATE Date of the event EventType VARCHAR Type of the event (e.g., wedding, corporate, birthday) CustomerI D INT Foreign key referencing the Customers table to associate the event with a customer Table 3: MenuItems This table holds information about the menu items offered by the catering service.

Create table: T1 CREATE TABLE Customers ( CustomerID INT PRIMARY KEY, FirstName VARCHAR(255), LastName VARCHAR(255), Email VARCHAR(255), Phone VARCHAR(20), Address VARCHAR(255) ); -- Inserting 5 customer records INSERT INTO Customers (CustomerID, FirstName, LastName, Email, Phone, Address) VALUES (1, 'John', 'Doe', '[email protected]', '555-123-4567', '123 Main St'), (2, 'Jane', 'Smith', '[email protected]', '555-987-6543', '456 Elm St'), (3, 'Michael', 'Johnson', '[email protected]', '555-789-1234', '789 Oak St'), (4, 'Sarah', 'Williams', '[email protected]', '555-456-7890', '101 Pine St'), (5, 'David', 'Brown', '[email protected]', '555-321-6540', '222 Cedar St'); T2 CREATE TABLE Events ( EventID INT PRIMARY KEY, EventName VARCHAR(255), EventDate DATE, EventType VARCHAR(255), CustomerID INT, FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ); -- Inserting 5 event records INSERT INTO Events (EventID, EventName, EventDate, EventType, CustomerID) VALUES (1, 'Wedding', '2023-06-15', 'Wedding Ceremony', 1), (2, 'Corporate Meeting', '2023-09-10', 'Business Conference', 2), (3, 'Birthday Party', '2023-08-20', 'Birthday Celebration', 3), (4, 'Anniversary', '2023-07-05', 'Anniversary Party', 4), (5, 'Company Picnic', '2023-07-30', 'Corporate Picnic', 5); T3 CREATE TABLE MenuItems ( MenuItemID INT PRIMARY KEY, ItemName VARCHAR(255), Description VARCHAR(255), Price NUMBER(10, 2), EventID INT, FOREIGN KEY (EventID) REFERENCES Events(EventID) ); -- Inserting 5 menu item records INSERT INTO MenuItems (MenuItemID, ItemName, Description, Price, EventID) VALUES (1, 'Chicken Alfredo', 'Creamy pasta with grilled chicken', 15.99, 1), (2, 'Vegetable Stir-Fry', 'Fresh vegetables in soy sauce', 12.49, 2), (3, 'Chocolate Cake', 'Rich chocolate cake with frosting', 7.99, 3), (4, 'Shrimp Cocktail', 'Chilled shrimp with cocktail sauce', 19.99, 4), (5, 'BBQ Ribs', 'Slow-cooked BBQ ribs with coleslaw', 14.99, 5); Q1 (^) Inner Join: Retrieve the names of customers and the names of events they have organized: Q2 (^) Left Join

Q3 (^) Right Join Retrieve all events and the names of their corresponding customers (including events with no customers): Q4 (^) Full Outer Join Retrieve all customers and events, showing events without customers and customers without events: Q5 Self-Join Find customers who have organized events on the same date: Q6 (^) Cross Join Create a list of all possible combinations of customers and menu items for events: Q7 Self-Join with Aggregation Find customers who have organized more than one event: Q8 Join with Conditional Filtering Retrieve events organized by a specific customer (e.g., John Doe): Q9 (^) Self-Join with Recursive Hierarchical Data Find the hierarchy of events organized by customers for a given event (e.g., a corporate conference): Q10 Join with Subquery Retrieve the menu items and their prices for a specific event (e.g., Wedding):