-
Notifications
You must be signed in to change notification settings - Fork 0
Dev #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Dev #1
Changes from all commits
86717d9
92f8b6f
8d87d0f
eb1ecf4
ee4da43
421b85d
395fb0d
ddaa590
88f6440
3fc9ce9
5ee4cdc
e8ddca9
2083598
c9d31cd
e50cba8
d495ff8
92660d1
64639f1
77c62bc
98deffa
aef1255
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
|
|
||
|
|
||
|
|
||
| use RestaurantReservation ; | ||
|
|
||
| -- 1. ReservationId in Orders table | ||
| CREATE INDEX idx_orders_reservationid ON Orders(ReservationId); | ||
|
|
||
| -- 2. RestaurantId in Reservations table | ||
| CREATE INDEX idx_reservations_restaurantid ON Reservations(RestaurantId); | ||
|
|
||
| -- 3. OrderId in OrderItems table | ||
| CREATE INDEX idx_orderitems_orderid ON OrderItems(OrderId); | ||
|
|
||
| -- 4. ItemId in OrderItems table | ||
| CREATE INDEX idx_orderitems_itemid ON OrderItems(ItemId); | ||
|
|
||
| -- 5. EmployeeId in Orders table | ||
| CREATE INDEX idx_orders_employeeid ON Orders(EmployeeId); | ||
|
|
||
| -- 6. ReservationDate in Reservations table | ||
| CREATE INDEX idx_reservations_reservationdate ON Reservations(ReservationDate); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
|
|
||
| -- **Database Function - Calculate Employees Salary**: | ||
| -- **Function Name**: **`fn_CalculateEmployeeSalary`** | ||
| -- **Purpose**: Compute the salary for a given employee. | ||
| -- **Parameter**: `EmployeeId` | ||
| -- **Implementation**: Salary is defined as: # number of orders made by specific employee * employee rank. | ||
| -- Employee�s rank based on position: Position = `VIPOrdersWaiter` = 5, `StandardWaiter` = 4, `AssistantWaiter` = 3. | ||
| -- **Return**: salary for the `EmployeeId`. | ||
|
|
||
| CREATE FUNCTION fn_CalculateEmployeeSalary(@EmployeeId INT) | ||
| RETURNS INT as | ||
| begin | ||
| DECLARE @cnt INT=(select count(*) from Orders where EmployeeId=@EmployeeId); | ||
| DECLARE @pos NVARCHAR(100) = (SELECT Position FROM Employees WHERE EmployeeId = @EmployeeId); | ||
| DECLARE @rank INT = CASE | ||
| WHEN @pos = 'VIPOrdersWaiter' THEN 5 | ||
| WHEN @pos = 'StandardWaiter' THEN 4 | ||
| WHEN @pos = 'AssistantWaiter' THEN 3 | ||
| ELSE 2 END; | ||
| RETURN ISNULL(@cnt,0) * @rank; | ||
| end; | ||
| SELECT dbo.fn_CalculateEmployeeSalary(2) AS SalaryForEmployee2; | ||
|
|
||
|
|
||
|
|
||
|
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, remove additional spaces |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
|
|
||
| -- **Database Function - Calculate Restaurant Revenue**: | ||
| -- - **Function Name**: **`fn_CalculateRevenue`** | ||
| -- - **Purpose**: Compute revenue made by a specific restaurant. | ||
| -- - **Parameter**: `RestaurantId` | ||
| -- - **Return**: total revenue amount for the `RestaurantId` . | ||
|
|
||
| create function fn_CalculateRevenue(@RestaurantId INT) | ||
| RETURNS DECIMAL(18,2) as | ||
| begin | ||
| return( | ||
| select ISNULL(SUM(O.TotalAmount),0) | ||
| from Orders O | ||
| JOIN Reservations r ON o.ReservationId = r.ReservationId | ||
| WHERE r.RestaurantId = @RestaurantId | ||
| ); | ||
| end; | ||
|
|
||
| SELECT dbo.fn_CalculateRevenue(3) AS RevenueForRestaurant1; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
|
|
||
| -- 13. **Stored Procedure - Borrowed Books Report**: | ||
| -- **Procedure Name**: **`sp_ResrvedTablesReport`** | ||
| -- **Purpose**: Generate a report of tables reserved within a specified date range. | ||
| -- **Parameters**: **`StartDate`**, **`EndDate`** | ||
| -- **Implementation**: Retrieve all tables reserved within the given range, with details like reservation date, party size and restaurant details. | ||
| -- **Return**: Tabulated report of reserved tables | ||
|
|
||
| -- GET NEW ID FOR INSERT NEW ORDER | ||
| CREATE FUNCTION dbo.fn_GetNextOrderId() | ||
| RETURNS INT | ||
| AS | ||
| BEGIN | ||
| DECLARE @NextId INT; | ||
|
|
||
| SELECT @NextId = ISNULL(MAX(OrderId), 0) + 1 | ||
| FROM Orders; | ||
|
|
||
| RETURN @NextId; | ||
| END; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
|
|
||
| use RestaurantReservation ; | ||
| -- List all reservations for a specific customer | ||
|
|
||
| DECLARE @CustomerId INT = 1; | ||
| SELECT * | ||
| FROM Reservations | ||
| WHERE CustomerId=@CustomerId; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
|
|
||
| use RestaurantReservation ; | ||
| -- List of Managers: Retrieve all employees holding Manager position. | ||
|
|
||
| SELECT * | ||
| FROM Employees | ||
| WHERE Position = 'Manager'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
|
|
||
| use RestaurantReservation ; | ||
| --List of Orders and Menu Items: Lists the orders placed on a specific given reservation along with the associated menu items. | ||
|
|
||
| DECLARE @ReservationId INT = 1; | ||
| SELECT * | ||
| FROM Orders O | ||
| JOIN OrderItems OI | ||
| ON O.OrderId=OI.OrderId | ||
| JOIN MenuItems MI | ||
| ON MI.ItemId=OI.ItemId | ||
| WHERE O.ReservationId = @ReservationId; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
|
|
||
| use RestaurantReservation ; | ||
| --List of Ordered Menu Items: Lists the menu items ordered by a specific reservation. | ||
|
|
||
| DECLARE @ReservationId INT = 1; | ||
| SELECT MI.* | ||
| FROM Orders O | ||
| JOIN OrderItems OI | ||
| ON O.OrderId = OI.OrderId | ||
| JOIN MenuItems MI | ||
| ON OI.ItemId = MI.ItemId | ||
| WHERE o.ReservationId = @ReservationId; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
|
|
||
| use RestaurantReservation ; | ||
| --Calculate Average Order Amount: Calculate the average order amount made through a specific employee. | ||
|
|
||
| DECLARE @EmployeeId INT =7; | ||
|
|
||
| SELECT emp.EmployeeId,emp.FirstName,emp.LastName, AVG(TotalAmount) AS AvgAmount | ||
| FROM Orders o | ||
| join Employees emp | ||
| on o.EmployeeId=emp.EmployeeId | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add space before and after = |
||
| WHERE o.EmployeeId = @EmployeeId | ||
| group by emp.EmployeeId,emp.FirstName,emp.LastName; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
|
|
||
| --Reservation�s Order with CTEs: Identify reservations which have 2 or more orders using CTEs. | ||
|
|
||
|
|
||
| WITH OrdersPerReservation AS ( | ||
| SELECT ReservationId, COUNT(*) AS OrdersCount | ||
| FROM Orders | ||
| WHERE ReservationId IS NOT NULL | ||
| GROUP BY ReservationId | ||
| ) | ||
| SELECT * | ||
| FROM Reservations R | ||
| JOIN OrdersPerReservation OPR | ||
| ON R.ReservationId=OPR.ReservationId | ||
| WHERE OPR.OrdersCount>=2; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
|
|
||
| --Restaurant Popularity using Aggregation: Rank restaurants by the reservation frequency. | ||
|
|
||
|
|
||
|
|
||
| SELECT r.RestaurantId, r.Name, | ||
| COUNT(res.ReservationId) AS ReservationCount | ||
| FROM Restaurants r | ||
| LEFT JOIN Reservations res ON r.RestaurantId = res.RestaurantId | ||
| GROUP BY r.RestaurantId, r.Name | ||
| ORDER BY ReservationCount DESC; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
|
|
||
| --Popular Menu Item Analysis using Joins and Window Functions: Identify the most popular menu item for each restaurant for a given month. | ||
| DECLARE @Year INT = 2025; | ||
| DECLARE @Month INT = 5; | ||
|
|
||
|
|
||
| ;WITH ItemsInMonth AS ( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove extra comma |
||
| SELECT mi.RestaurantId, mi.ItemId, mi.Name, | ||
| SUM(oi.Quantity) AS QtySold | ||
| FROM OrderItems oi | ||
| JOIN Orders o ON oi.OrderId = o.OrderId | ||
| JOIN MenuItems mi ON oi.ItemId = mi.ItemId | ||
| WHERE YEAR(o.OrderDate) = @Year AND MONTH(o.OrderDate) = @Month | ||
| GROUP BY mi.RestaurantId, mi.ItemId, mi.Name | ||
| ),Ranked AS ( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. separating queries by adding one line between each two, would make it easier to understand. |
||
| SELECT RestaurantId, ItemId, Name, QtySold, | ||
| ROW_NUMBER() OVER (PARTITION BY RestaurantId ORDER BY QtySold DESC) AS rn | ||
| FROM ItemsInMonth | ||
| )SELECT RestaurantId, ItemId, Name, QtySold | ||
| FROM Ranked | ||
| WHERE rn = 1 | ||
| ORDER BY RestaurantId; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
|
|
||
|
|
||
|
|
||
| -- **Stored Procedure - Add New Order**: | ||
| -- **Procedure Name**: **`sp_AddNewOrder`** | ||
| -- **Purpose**: Streamline the process of adding a new order. | ||
| -- **Parameters**: **`ReservationId`**, **`EmployeeId`**, **`OrderDate`**, and **`TotalAmount`**. | ||
| -- **Implementation**: Check if the specified reservation and employee exist, if not, return an error message, if existing, add new order. | ||
| -- **Return**: The new **`OrderId`** or an error message. | ||
|
|
||
| IF OBJECT_ID('dbo.sp_AddNewOrder','P') IS NOT NULL | ||
| DROP PROCEDURE dbo.sp_AddNewOrder; | ||
| GO | ||
|
|
||
| CREATE PROCEDURE dbo.sp_AddNewOrder | ||
| @ReservationId INT, | ||
| @EmployeeId INT, | ||
| @OrderDate DATETIME, | ||
| @TotalAmount DECIMAL(12,2) | ||
| AS | ||
| BEGIN | ||
| SET NOCOUNT ON; | ||
|
|
||
|
|
||
| IF NOT EXISTS (SELECT 1 FROM Reservations WHERE ReservationId = @ReservationId) | ||
| BEGIN | ||
| RAISERROR('Reservation not found (ReservationId=%d).',16,1,@ReservationId); | ||
| RETURN -1; | ||
| END | ||
|
|
||
|
|
||
| IF NOT EXISTS (SELECT 1 FROM Employees WHERE EmployeeId = @EmployeeId) | ||
| BEGIN | ||
| RAISERROR('Employee not found (EmployeeId=%d).',16,1,@EmployeeId); | ||
| RETURN -2; | ||
| END | ||
|
|
||
|
|
||
| DELETE FROM Orders | ||
| WHERE ReservationId = @ReservationId; | ||
|
|
||
|
|
||
| INSERT INTO Orders (OrderId, ReservationId, EmployeeId, OrderDate, TotalAmount) | ||
| VALUES (dbo.fn_GetNextOrderId(), @ReservationId, @EmployeeId, @OrderDate, @TotalAmount); | ||
|
|
||
|
|
||
| SELECT MAX(OrderId) AS NewOrderId FROM Orders; | ||
| END; | ||
| GO | ||
|
|
||
|
|
||
| EXEC dbo.sp_AddNewOrder | ||
| @ReservationId = 1, | ||
| @EmployeeId = 2000, | ||
| @OrderDate = '2025-09-29 14:35', | ||
| @TotalAmount = 50.00; | ||
|
|
||
| SELECT MAX(OrderId) AS NewOrderId FROM Orders; | ||
| select * from Orders; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
|
|
||
|
|
||
| --15. **SQL Stored Procedure with Temp Table**: | ||
| -- Design a stored procedure that retrieves all tables which have future reservations. | ||
| -- Store these tables in a temporary table, then join this temp table with the **`Restaurants`** | ||
| -- table to list out the specific information about the associated restaurants. | ||
| use RestaurantReservation ; | ||
|
|
||
| CREATE PROCEDURE dbo.sp_FutureReservedTables | ||
| AS | ||
| BEGIN | ||
| SET NOCOUNT ON; | ||
|
|
||
|
|
||
| IF OBJECT_ID('tempdb..#FutureTables') IS NOT NULL | ||
| DROP TABLE #FutureTables; | ||
|
|
||
|
|
||
| SELECT DISTINCT | ||
| t.TableId, | ||
| t.RestaurantId, | ||
| t.Capacity | ||
| INTO #FutureTables | ||
| FROM Tables t | ||
| JOIN Reservations r ON t.TableId = r.TableId | ||
| WHERE r.ReservationDate > GETDATE(); | ||
|
|
||
|
|
||
| SELECT | ||
| ft.TableId, | ||
| ft.RestaurantId, | ||
| ft.Capacity, | ||
| res.Name AS RestaurantName, | ||
| res.Address AS RestaurantAddress, | ||
| res.PhoneNumber AS RestaurantPhone | ||
| FROM #FutureTables ft | ||
| LEFT JOIN Restaurants res ON ft.RestaurantId = res.RestaurantId | ||
| ORDER BY res.Name, ft.TableId; | ||
|
|
||
| END; | ||
|
|
||
| EXEC dbo.sp_FutureReservedTables; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
|
|
||
| -- 13. **Stored Procedure - Borrowed Books Report**: | ||
| -- **Procedure Name**: **`sp_ResrvedTablesReport`** | ||
| -- **Purpose**: Generate a report of tables reserved within a specified date range. | ||
| -- **Parameters**: **`StartDate`**, **`EndDate`** | ||
| -- **Implementation**: Retrieve all tables reserved within the given range, with details like reservation date, party size and restaurant details. | ||
| -- **Return**: Tabulated report of reserved tables | ||
|
|
||
| CREATE PROCEDURE sp_ResrvedTablesReport | ||
| @StartDate DATETIME, | ||
| @EndDate DATETIME | ||
| AS | ||
| begin | ||
| SELECT r.ReservationId, r.ReservationDate, r.PartySize, | ||
| rest.RestaurantId, rest.Name AS RestaurantName, rest.Address, rest.PhoneNumber, | ||
| r.TableId, | ||
| r.CustomerId | ||
| FROM Reservations r | ||
| JOIN Restaurants rest ON r.RestaurantId = rest.RestaurantId | ||
| WHERE r.ReservationDate BETWEEN @StartDate AND @EndDate | ||
|
|
||
| end; | ||
| EXEC dbo.sp_ResrvedTablesReport '2025-01-01','2025-02-28'; | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
|
|
||
|
|
||
| --16. **Trigger Implementation** | ||
| -- Design a trigger to log an entry into a separate **`AuditLog`** table whenever a table get reserved. | ||
| --The **`AuditLog`** should capture `ResturantId`, `TableId`, `ReservationDate` and **`ChangeDate`**. | ||
|
|
||
| use RestaurantReservation ; | ||
|
|
||
| CREATE TABLE dbo.AuditLog ( | ||
| AuditId INT IDENTITY(1,1) PRIMARY KEY, | ||
| RestaurantId INT, | ||
| TableId INT, | ||
| ReservationDate DATETIME, | ||
| ChangeDate DATETIME DEFAULT GETDATE() | ||
| ); | ||
|
|
||
| CREATE TRIGGER dbo.trg_LogReservation | ||
| ON dbo.Reservations | ||
| AFTER INSERT | ||
| AS | ||
| BEGIN | ||
| INSERT INTO dbo.AuditLog (RestaurantId, TableId, ReservationDate) | ||
| SELECT RestaurantId, TableId, ReservationDate | ||
| FROM inserted; | ||
| END; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
|
|
||
| --Retrieve Employees details with Views: Use a view to list all employees information including their restaurants details | ||
|
|
||
|
|
||
| CREATE VIEW EmployeesFull | ||
| AS | ||
| SELECT e.EmployeeId, e.FirstName, e.LastName, e.Position, e.RestaurantId, | ||
| r.Name AS RestaurantName, r.Address AS RestaurantAddress, r.PhoneNumber AS RestaurantPhone | ||
| FROM Employees e | ||
| JOIN Restaurants r ON e.RestaurantId = r.RestaurantId; | ||
|
|
||
| SELECT * FROM EmployeesFull; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
|
|
||
| --Retrieve Reservations Report with Views: Use a view to list all reservations information including restaurants and customers information. | ||
|
|
||
|
|
||
| CREATE VIEW ReservationsReport AS | ||
| SELECT r.ReservationId, r.ReservationDate, r.PartySize, r.TableId, | ||
| c.CustomerId, c.FirstName AS CustomerFirstName, c.LastName AS CustomerLastName, c.Email, c.PhoneNumber AS CustomerPhone, | ||
| res.RestaurantId, res.Name AS RestaurantName, res.Address AS RestaurantAddress, res.PhoneNumber AS RestaurantPhone | ||
|
|
||
| FROM Reservations r | ||
| JOIN Customers c ON r.CustomerId = c.CustomerId | ||
| JOIN Restaurants res ON r.RestaurantId = res.RestaurantId; | ||
|
|
||
| SELECT * FROM ReservationsReport; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for these leading spaces