0% found this document useful (0 votes)
319 views28 pages

SQL Queries for Auto Rentals Lab 6

The document describes tables in a SQL database for an auto rental company, including tables for customers, rentals, and rental costs. It provides the structure and sample data for each table. It then lists 13 questions for writing and executing SQL queries against the database tables to return requested data. The questions cover basic queries selecting columns, filtering on conditions, joining tables, aggregating results, and performing calculations.

Uploaded by

sana fiaz
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
319 views28 pages

SQL Queries for Auto Rentals Lab 6

The document describes tables in a SQL database for an auto rental company, including tables for customers, rentals, and rental costs. It provides the structure and sample data for each table. It then lists 13 questions for writing and executing SQL queries against the database tables to return requested data. The questions cover basic queries selecting columns, filtering on conditions, joining tables, aggregating results, and performing calculations.

Uploaded by

sana fiaz
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Introduction and Table Setup
  • Table Creation Scripts
  • Deliverables and Task Descriptions
  • Query Tasks

CSIS 325: Lab 6 SQL Queries

In this lab, you will be working with the following tables in SQL Server. To create and populate these
tables in SQL Server, run the queries that are listed below these tables.

CUSTOMER

CID CName Age Resid_City BirthPlace


1 BLACK 40 ERIE TAMPA
2 GREEN 25 CARY ERIE
3 JONES 30 HEMET TAMPA
4 MARTIN 35 HEMET TAMPA
5 SIMON 22 ERIE ERIE
6 VERNON 60 CARY CARY
7 WILSON 25 DENVER AUSTIN
In the CUSTOMER table, CName is the primary key.

RENTALS
Rtn CID Make Date_Out Pickup Date_returned Return_city
1 1 FORD 10-Oct-2010 CARY 12-Oct-2010 CARY
2 1 GM 01-Nov-2009 TAMPA 05-Nov-2009 CARY
3 1 FORD 01-Jan-2009 ERIE 10-Jan-2009 ERIE
4 2 NISSAN 07-Nov-2010 TAMPA
5 3 FORD 01-Oct-2010 CARY 31-Oct-2010 ERIE
6 3 GM 01-Aug-2009 ERIE 05-Aug-2009 ERIE
7 4 FORD 01-Aug-2010 CARY 12-Aug-2010 ERIE
8 5 GM 01-Sep-2010 ERIE

In the table RENTALS, Rtn is the primary key and represents the rental number. CID is a foreign key in
the RENTALS table and refers to the CID in CUSTOMER; Pickup is the city where the car was picked up;
and Date_Out is the date in which the car was rented out. Return_city is the city where the car was
returned. Date_returned is the date in which the vehicle was returned. If the car has not yet been
returned, Date_returned and Return_city are null.

RENTCOST
MAKE COST
FORD 30
GM 40
NISSAN 30
TOYOTA 20
VOLVO 50
The RENTCOST table stores the rates per day of each vehicle. The primary key of this table is MAKE,
and it is a foreign key in the RENTALS table.
create database AutoRentals
go

use AutoRentals
go

create table Customer


(CID integer,
CName varchar(20),
Age integer,
Resid_City varchar(20),
BirthPlace varchar(20),
Constraint PK_Customer Primary Key (CID))

insert Customer
select 1, 'Black', 40, 'Erie', 'Tampa'

insert Customer
select 2, 'Green', 25, 'Cary', 'Erie'

insert Customer
select 3, 'Jones', 30, 'Hemet', 'Tampa'

insert Customer
select 4, 'Martin', 35, 'Hemet', 'Tampa'

insert Customer
select 5, 'Simon', 22, 'Erie', 'Erie'

insert Customer
select 6, 'Vernon', 60, 'Cary', 'Cary'

insert Customer
select 7, 'Wilson', 25, 'Denver', 'Austin'

create table Rentcost


(Make varchar(20),
Cost float,
constraint PK_Rentcost Primary Key (Make))

insert Rentcost
select 'Ford', 30

insert Rentcost
select 'GM', 40

insert Rentcost
select 'Nissan', 30

insert Rentcost
select 'Toyota', 20

insert Rentcost
select 'Volvo', 50

Create table Rentals


(Rtn integer,
CID integer,
Make varchar(20),
Date_Out smalldatetime,
Pickup varchar(20),
Date_returned smalldatetime,
Return_city varchar(20),
Constraint PK_Rentals Primary Key (Rtn),
Constraint FK_CustomerRentals Foreign Key (CID) References Customer,
Constraint FK_RentCostRentals Foreign Key (Make) References Rentcost)

insert Rentals
select 1, 1, 'Ford', '10/10/2010', 'Cary', '10/12/2010', 'Cary'

insert Rentals
select 2, 1, 'GM', '11/1/2009', 'Tampa', '11/5/2009', 'Cary'

insert Rentals
select 3, 1, 'Ford', '1/1/2009', 'Erie', '1/10/2009', 'Erie'

insert Rentals
select 4, 2, 'Nissan', '11/7/2010', 'Tampa', null, null

insert Rentals
select 5, 3, 'Ford', '10/1/2010', 'Cary', '10/31/2010', 'Erie'

insert Rentals
select 6, 3, 'GM', '8/1/2009', 'Erie', '8/5/2009', 'Erie'

insert Rentals
select 7, 4, 'Ford', '8/1/2010', 'Cary', '8/12/2010', 'Erie'

insert Rentals
select 8, 5, 'GM', '9/1/2010', 'Erie', null, null

Deliverables:
Write queries that will satisfy the following requirements. To receive credit for this assignment, all
queries must be executed in SQL Server and be displayed appropriately. For each question:

 TYPE the SQL query below the instructions.


 Take a screenshot of your query that was executed in SQL server. Be sure to capture the
query as well as the results in your screen shot.
 Paste the screenshot below the typed query .

The first question has been done for you.


1. Write and execute a query that will return the name and age of all customers.

You should have 7 rows in your result:

Answer:

select CName, Age


from customer This is the query typed out.

This is the query when executed in


SQL Server. Note the capture displays
the query, the results, and the
number of rows returned.
2. Write and execute a query that will display the name and resid_city of all customers that were born
in Tampa.

You should have 3 rows in your result:

Answer:
SELECT CName, Resid_City FROM Customer
WHERE BirthPlace = 'Tampa'

3. Write and execute a query that will show the customer id and name of customers between the ages
of 25 (inclusive) and 40 (inclusive). Order your results by Cname in descending order. Display the
CName column with the heading “Customer Name”  note the capitalizing and the space between
words. (Note that you will need to use a column alias for this using the keyword “AS.”)

Your output should look like this:


Answer:

SELECT CID, CName AS [Customer Name]


FROM [Link]
WHERE Age BETWEEN '25' and '40'
ORDER by CName DESC

4. Write and execute a query that will list any customers whose names begin with the letter “G”.

Your query should return 1 row like this:

Answer:
SELECT cName
FROM [Link]
WHERE CName LIKE 'G%'
5. Write and execute a query that will display a unique list of all the automobile Makes that have ever
been rented. Order your results by Make.

You should have 3 rows in your resultset.

Answer:
SELECT DISTINCT Make FROM [Link]
6. Write and execute a query that will display all of the customers who have ever rented an automobile.
Include the customer name, make, pickup location in your results. Order your list in ascending order by
pickup.

Your results should include 8 rows:

Answer:
SELECT [Link],
[Link],
[Link]
FROM [Link] c
INNER JOIN [Link] r
ON [Link] = [Link]
ORDER BY [Link]
7. Write and execute a query that will return the names of customers who rented a Ford or GM.

You should have 4 rows in your resultset.

Answer:
SELECT DISTINCT [Link]
FROM [Link] c
INNER JOIN [Link] r
ON [Link] = [Link]
WHERE [Link] IN ('Ford', 'GM')
8. Write and execute a query that will display the customer names, ages, makes, and daily cost of each
automobile that they rented. Order your results by cost.

You should have 8 rows in your answer:

Answer:
SELECT [Link], [Link], [Link], [Link]
FROM [Link] c
INNER JOIN [Link] r
ON [Link] = [Link]
INNER JOIN [Link] rc
ON [Link] = [Link]
ORDER BY [Link]
9. Write and execute a query that will return the unique list of birth places of everyone who has ever
rented a Ford.

You should have one row in your result: Tampa

Answer:
SELECT DISTINCT [Link]
FROM [Link] c
INNER JOIN [Link] r
ON [Link] = [Link]
AND [Link] = 'Ford'
10. Write a query that will return the Names and ages of customers who have rented any automobile
during 2009. Make sure that each customer is listed only once in your output.

You should have two rows in your resultset:

Answer:
SELECT DISTINCT [Link], [Link]
FROM [Link] c
INNER JOIN [Link] r
ON [Link] = [Link]
WHERE YEAR(date_returned) = '2009'
11. Write and execute a query that will determine the total number of days that Black rented a GM on
November 1, 2009.

You should have one cell: 4

Answer:
SELECT DATEDIFF(day, r.Date_Out, r.Date_returned) as [Total Days]
FROM [Link] c
INNER JOIN [Link] r
ON [Link] = [Link]
WHERE r.Date_Out = '2009-11-01 00:00:00'
12. Write and execute a query that will determine the total cost of the automobile rented by Black on
November 1, 2009.

You should have one cell: 160

Answer:
SELECT SUM([Link]) FROM [Link] c
INNER JOIN [Link] r ON [Link] = [Link]
INNER JOIN [Link] rc ON [Link] = [Link]
WHERE [Link] = 'Black'
AND r.Date_Out = '11/1/2009'
13. Write and execute a query that will determine the Total cost of all the automobiles that have ever
been rented.

You should have one cell: 1880

Answer:
SELECT SUM(DATEDIFF(day, r.Date_Out, r.Date_returned) * [Link]) as [Total Cost]
FROM [Link] rc
inner join [Link] r ON [Link] = [Link]
inner join [Link] c on [Link] = [Link]
14. Write and execute a query that will determine the average number of days that automobiles are
rented. Show your result broken out by makes. Do not include an automobile if it has not yet been
returned.

You should have two rows: Ford and GM with average days rented 13 and 4, respectively.

Answer:
SELECT make, AVG(DATEDIFF(DD, Date_Out, Date_returned)) AS [Average Days Rented]
FROM [Link]
WHERE Date_returned is not null
GROUP BY make
15. Write and execute a query that will determine the average age of customers broken out by the city
in which they reside. Note: Make sure that the average age is not truncated to an integer.

You should have 4 rows: Cary, Denver, Erie, and Hemet with average ages of 42.5, 25, 31, and 32.5

Answer:
SELECT Resid_City, CAST(AVG(CONVERT(dec(3,1),Age)) as DEC(3,1))
FROM [Link]
GROUP BY Resid_City
16. Write and execute a query that will show a list of customers who reside in the same city in which
they were born.

You should have 2 rows: Simon and Vernon

Answer:
SELECT CName
FROM [Link]
WHERE Resid_City = BirthPlace
17. Using a left outer join, write and execute a query that will display a unique list of the makes of
automobiles that have never been rented.

You should have 2 rows: Toyota and Volvo

Answer:
SELECT DISTINCT [Link]
FROM [Link] rc
LEFT OUTER JOIN [Link] r
ON [Link] = [Link]
WHERE [Link] is null
18. Using a Type I query, display a unique list of the makes of automobiles that have never been rented.

You should have 2 rows: Toyota and Volvo

Answer:
SELECT DISTINCT make
FROM [Link]
WHERE make NOT IN (SELECT DISTINCT make
FROM [Link])
19. Using a right outer join, write and execute a query that will display a unique list of customers who
have never rented an automobile.

You should have two rows: Vernon and Wilson

Answer:
SELECT DISTINCT [Link]
FROM [Link] r
RIGHT OUTER JOIN [Link] c
ON [Link] = [Link]
WHERE rtn is null
20. Using a Type II query, display a unique list of customers who have never rented an automobile.

You should have two rows: Vernon and Wilson

Answer:
SELECT DISTINCT [Link]
FROM [Link] c
WHERE NOT EXISTS
(
SELECT * FROM [Link] r
WHERE [Link] = [Link]
)
21. Write and execute a query that will display a list of the customers who picked up their rental from
the same city in which they reside.

You should have 2 rows: Black and Simon

Answer:
SELECT DISTINCT [Link]
FROM [Link] c, [Link] r
WHERE [Link] = [Link]
and [Link] = c.Resid_City
22. Write a query that will display a list of customers who have not returned their rentals.

You should have 2 rows: Green and Simon

Answer:

SELECT DISTINCT [Link]


FROM [Link] r
INNER JOIN [Link] c
ON [Link] = [Link]
WHERE Date_returned is null

23. Using a Type I query, show all of the names and ages of customers who have rented an automobile
and returned it to Erie. Sort your results in ascending order by customer name.

You should have three rows: Black, Jones, Martin.


Answer:
SELECT DISTINCT [Link], [Link]
FROM [Link] c
INNER JOIN [Link] r
ON [Link] = [Link]
WHERE [Link] IN (
SELECT DISTINCT make
FROM [Link] r2
WHERE r2.Return_city = 'Erie'
)
AND Date_returned IS NOT NULL
ORDER BY [Link] ASC

24. Using a Type II query, show all of the customers who rented an automobile and picked it up in Cary.

You should have three rows: Black, Jones, and Martin


Answer:
SELECT DISTINCT [Link]
FROM [Link] c
WHERE EXISTS (
SELECT CID
FROM [Link] r
WHERE [Link] = [Link]
AND Pickup = 'Cary')

25. Write and execute a single query that will display all of the information in the Customer, Rentals,
and Rentcost tables in a single resultset. Be sure to display each field only once in your output. Order
your results in ascending order by [Link] and [Link] .

You should have 12 rows and 12 columns in your result.


Answer:
SELECT [Link], [Link], Age, Resid_City, BirthPlace, [Link], [Link], rtn, Date_Out,
Pickup, Date_returned, Return_city
FROM [Link] c
FULL OUTER JOIN [Link] r ON [Link] = [Link]
FULL OUTER JOIN [Link] rc ON [Link] = [Link]
ORDER BY [Link], [Link] ASC

CSIS 325:  Lab 6 SQL Queries  
In this lab, you will be working with the following tables in SQL Server.  To create and popul
create database AutoRentals
go
use AutoRentals
go
create table Customer
(CID integer,
CName varchar(20),
Age integer,
Resid_C
select 'Volvo', 50
Create table Rentals
(Rtn integer,
CID integer,
Make varchar(20),
Date_Out smalldatetime,
Pickup varchar(2
1. Write and execute a query that will return the name and age of all customers.
     You should have 7 rows in your result:
2.  Write and execute a query that will display the name and resid_city of all customers that were born 
in Tampa.
You should
Answer:
SELECT CID, CName AS [Customer Name] 
FROM AutoRentals.dbo.Customer 
WHERE Age BETWEEN '25' and '40'
ORDER by CName D
5.  Write and execute a query that will display a unique list of all the automobile Makes that have ever 
been rented.  Order
6.  Write and execute a query that will display all of the customers who have ever rented an automobile.
Include the customer
7.  Write and execute a query that will return the names of customers who rented a Ford or GM.
You should have 4 rows in your
8.  Write and execute a query that will display the customer names, ages, makes, and daily cost of each 
automobile that they

You might also like