Homework 2 Database
Homework 2 Database
DATABASE CREATION:
CREATE DATABASE HW2DB
GO
--
USE HW2DB
GO
--
CName VARCHAR(255),
CPhone VARCHAR(20)
);
GO
--
CREATE TABLE EMPLOYEE (
EName VARCHAR(255),
EPhone VARCHAR(20)
);
GO
--
CarModel VARCHAR(255)
);
GO
--
Location VARCHAR(255)
);
(3, 'Chicago'),
(4, 'Houston'),
(5, 'Phoenix');
GO
--
StartDate DATE,
EndDate DATE,
Price DECIMAL(10,2),
ONumber INT,
CarNumber INT,
ENumber INT,
);
INSERT INTO RENT_CONTRACT (RentID, StartDate, EndDate, Price, ONumber, CarNumber, ENumber)
VALUES
GO
--
1. The first query | List of All Employees and the Offices They Work In
The query joins EMPLOYEE, RENT_CONTRACT, and CAR tables to bring together details of
the employees, the rental contracts they have managed, and the cars involved in those
contracts.
It groups the results by employee name and phone, along with the contract details (car
model, start date, and end date), allowing for the aggregation of rental prices.
The MAX(RC.Price) function is used to find the highest rental price for each group
(combination of employee and contract details). The HAVING clause is crucial as it filters the
grouped data to include only those entries where the rental price equals the maximum price
found for that group.