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

Homework 2 Database

Uploaded by

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

Homework 2 Database

Uploaded by

altin.daka2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Homework 2 Database

DATABASE CREATION:
CREATE DATABASE HW2DB

GO

--

USE HW2DB

GO

--

CREATE TABLE CLIENT (

CNumber INT PRIMARY KEY,

CName VARCHAR(255),

CPhone VARCHAR(20)

);

-- Insert sample records

INSERT INTO CLIENT (CNumber, CName, CPhone) VALUES

(1, 'John Doe', '123-456-7890'),

(2, 'Jane Smith', '234-567-8901'),

(3, 'Alice Johnson', '345-678-9012'),

(4, 'Bob Brown', '456-789-0123'),

(5, 'Carol White', '567-890-1234');

GO

--
CREATE TABLE EMPLOYEE (

ENumber INT PRIMARY KEY,

EName VARCHAR(255),

EPhone VARCHAR(20)

);

-- Insert sample records

INSERT INTO EMPLOYEE (ENumber, EName, EPhone) VALUES

(1, 'Sam Blue', '678-901-2345'),

(2, 'Lucy Green', '789-012-3456'),

(3, 'Ron Black', '890-123-4567'),

(4, 'Nina Teal', '901-234-5678'),

(5, 'Oscar Gray', '012-345-6789');

GO

--

CREATE TABLE CAR (

CarNumber INT PRIMARY KEY,

CarModel VARCHAR(255)

);

-- Insert sample records

INSERT INTO CAR (CarNumber, CarModel) VALUES

(1, 'Toyota Corolla'),

(2, 'Honda Civic'),

(3, 'Ford Focus'),


(4, 'Nissan Sentra'),

(5, 'Chevrolet Cruze');

GO

--

CREATE TABLE OFFICE (

ONumber INT PRIMARY KEY,

Location VARCHAR(255)

);

-- Insert sample records

INSERT INTO OFFICE (ONumber, Location) VALUES

(1, 'New York'),

(2, 'Los Angeles'),

(3, 'Chicago'),

(4, 'Houston'),

(5, 'Phoenix');

GO

--

CREATE TABLE RENT_CONTRACT (

RentID INT PRIMARY KEY,

StartDate DATE,

EndDate DATE,

Price DECIMAL(10,2),

ONumber INT,

CarNumber INT,
ENumber INT,

FOREIGN KEY (ONumber) REFERENCES OFFICE(ONumber),

FOREIGN KEY (CarNumber) REFERENCES CAR(CarNumber),

FOREIGN KEY (ENumber) REFERENCES EMPLOYEE(ENumber)

);

-- Insert sample records

INSERT INTO RENT_CONTRACT (RentID, StartDate, EndDate, Price, ONumber, CarNumber, ENumber)
VALUES

(1, '2024-01-01', '2024-01-15', 300.00, 1, 1, 1),

(2, '2024-02-01', '2024-02-15', 450.00, 2, 2, 2),

(3, '2024-03-01', '2024-03-15', 375.00, 3, 3, 3),

(4, '2024-04-01', '2024-04-15', 200.00, 4, 4, 4),

(5, '2024-05-01', '2024-05-15', 220.00, 5, 5, 5);

GO

--
1. The first query | List of All Employees and the Offices They Work In

Selects Enumber, Ename, EPhone, Location by first joining the Employee E


table with Rent_contract and after that joining the Office table with
Rent_contract therefore being able to print the location aswell.

2. The second query | Current Rentals and Their Car Models


Here we use CAST(GETDATE() AS DATE) since SQL Server does not recognize
Current_Date but it gets date with GETDATE() but with only this function it
returns and other things which are not necessary therefore we need to use
CAST() with AS DATE to only take the DATE and not the time for example.

3. Third Query | Clients and Their Total Expenditure on Rentals

We join Client and Rent_Contract ON CNumber and RentID.

4. Fourth Query | Most Popular Car Model Rented


Instead of LIMIT we use in SQL Server TOP.

5. Fifth Query | Employees and Their Most Expensive Rentals

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.

You might also like