









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










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 :
USE THE WHERE CLAUSE TO FILTER DATA BASED ON SPECIFIC CONDITIONS:
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.
The ORDER BY clause in SQL is used to sort the result set of a query based on one or more columns.
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
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):