CS281-Computers and Data Organization
Homework 2
Bilkent University
Fall 2023-2024
1 Details About Submission
Please include your answers in a separate document with your name, surname,
section, and ID included. You should type your answers on the computer. In
the end, you should upload a single PDF file on Moodle.
2 Database
The following relational schema is given for a car rental system:
Brand(BrandID, BrandName)
Company(CompanyID, CompanyName, City)
Car(Plate, Model, Year, Color, Price, Type, BrandID, CompanyID)
Foreign Key (BrandID) →Brand (BrandID)
Foreign Key (CompanyID) →Company (CompanyID)
Customer(SSN, Age, Gender, Name, Surname)
Rental(RentalID, SSN, Plate, PickUpDate, ReturnDate)
Foreign Key (Plate) →Cars (Plate)
Foreign Key (SSN) →Customer (SSN)
The Brand table stores car brands, such as ’Honda’ and ’BMW,’ among
others. The Company table contains information about rental companies.
The Cars table contains information about individual cars, including models
(e.g., ’A5,’ ’Civic,’ ’Fiesta’), year of manufacture (numeric format, e.g., 2023 or
2010), color options (e.g., ’Red,’ ’White,’ ’Black’), prices (e.g., 1200, 900), and
types (e.g., ’Sedan,’ ’SUV’). The Customer table stores information related to
customers. The Rental table stores information about car rentals. For example,
an entity could be as follows: The customer with SSN 123456789 rented the car
with the plate ’00-AB-1234,’ picked up the car from the rental company on 13
October 2023, and returned the car to the company on 18 October 2023.
1
3 Questions
Write SQL queries based on the given information for the following questions.
(10 points for each question.)
1. Based on the current year, list cars that are more than 10 years old and
are either ’Black,’ ’White,’ or ’Red’ in color.
SELECT *
FROM Cars
WHERE [Link] < 2013
AND [Link] IN (’Black’, ’White’, ’Red’)
2. List all the cars which have not been rented this year (2023).
SELECT *
FROM Cars
WHERE [Link] NOT IN (SELECT DISTINCT Plate
FROM Rental
WHERE ReturnDate >= ’2023-01-01’)
3. List the average age of customers by gender
SELECT Gender, AVG(Age)
FROM Customer
GROUP BY [Link]
4. Find the name, surname, and age of the customers who rented a car on
19 October 2023.
SELECT Name, Surname, Age
FROM Rental, Customer
WHERE [Link] = [Link]
AND (PickUpDate <= ’2023-10-19’ AND ReturnDate >= ’2023-10-19’)
5. Find the age of the youngest and oldest customers who have never rented
an SUV car.
SELECT MIN(age), MAX(age)
FROM Cars, Rental, Customer
WHERE [Link] = [Link]
AND [Link] = [Link]
AND [Link] NOT IN (SELECT Distinct [Link]
From Rental, Cars
Where [Link] = [Link]
AND [Link] = ’SUV’)
6. List the number of cars by cities of their companies.
SELECT City, Count(*)
FROM Cars, Company
WHERE [Link] = [Link]
GROUP BY [Link]
2
7. Find the plates, years, models, and types of cars matching the brand of
the most expensive car.
SELECT Plate, Year, Model, Type
FROM Cars
WHERE [Link] IN (Select BrandID
From Cars
Where [Link] = (SELECT max(Price)
From Cars))
8. Find the plates of ’Red’ cars that have been rented more than once.
SELECT Plate
FROM Cars, Rental
WHERE [Link] = ’Red’
AND [Link] = [Link]
GROUP BY [Link]
HAVING count(*) > 1
9. List the plates, colors, and years of SUV cars from ’BestCars’ company
and Sedan cars from ’RentCar’ company.
SELECT Plate, Color, Year
FROM Cars, Company
WHERE [Link] = [Link]
AND [Link] = ’SUV’
AND [Link] = ’BestCars’
UNION
SELECT Plate, Color, Year
FROM Cars, Company
WHERE [Link] = [Link]
AND [Link] = ’Sedan’
AND [Link] = ’RentCar’
10. Make a 10% discount on the prices of ’BestCars’ company’s cars.
UPDATE Cars
SET Price = Price * 0.9
WHERE [Link] IN (SELECT CompanyID
FROM Company
WHERE CompanyName = ’BestCars’)
By doing this homework, you agree that you would fol-
low Bilkent University’s policy on plagiarism, and you ac-
cept that all the solutions belong individually to you. You
also accept that in case of an act of plagiarism, you would
3
not get any points from this homework, and disciplinary
action will be taken.