Professional Documents
Culture Documents
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
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
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'
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
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:
Answer:
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.”)
4. Write and execute a query that will list any customers whose names begin with the letter “G”.
Answer:
SELECT cName
FROM AutoRentals.dbo.Customer
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.
Answer:
SELECT DISTINCT Make FROM AutoRentals.dbo.Rentals
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.
Answer:
SELECT c.CName,
r.Make,
r.Pickup
FROM AutoRentals.dbo.Customer c
INNER JOIN AutoRentals.dbo.Rentals r
ON c.cid = r.cid
ORDER BY r.Pickup
7. Write and execute a query that will return the names of customers who rented a Ford or GM.
Answer:
SELECT DISTINCT c.CName
FROM AutoRentals.dbo.Customer c
INNER JOIN AutoRentals.dbo.Rentals r
ON c.cid = r.cid
WHERE r.Make 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.
Answer:
SELECT c.CName, c.Age, r.Make, rc.Cost
FROM AutoRentals.dbo.Customer c
INNER JOIN AutoRentals.dbo.Rentals r
ON c.cid = r.cid
INNER JOIN AutoRentals.dbo.Rentcost rc
ON rc.Make = r.Make
ORDER BY rc.Cost
9. Write and execute a query that will return the unique list of birth places of everyone who has ever
rented a Ford.
Answer:
SELECT DISTINCT c.BirthPlace
FROM AutoRentals.dbo.Customer c
INNER JOIN AutoRentals.dbo.Rentals r
ON c.cid = r.cid
AND r.Make = '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.
Answer:
SELECT DISTINCT c.CName, c.Age
FROM AutoRentals.dbo.Customer c
INNER JOIN AutoRentals.dbo.Rentals r
ON c.cid = r.cid
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.
Answer:
SELECT DATEDIFF(day, r.Date_Out, r.Date_returned) as [Total Days]
FROM AutoRentals.dbo.Customer c
INNER JOIN AutoRentals.dbo.Rentals r
ON c.cid = r.cid
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.
Answer:
SELECT SUM(rc.Cost) FROM AutoRentals.dbo.Customer c
INNER JOIN AutoRentals.dbo.Rentals r ON c.CID = r.CID
INNER JOIN AutoRentals.dbo.Rentcost rc ON rc.Make = rc.Make
WHERE c.CName = '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.
Answer:
SELECT SUM(DATEDIFF(day, r.Date_Out, r.Date_returned) * rc.Cost) as [Total Cost]
FROM AutoRentals.dbo.Rentcost rc
inner join AutoRentals.dbo.Rentals r ON rc.Make = r.Make
inner join AutoRentals.dbo.Customer c on c.CID = r.CID
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 AutoRentals.dbo.Rentals
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 AutoRentals.dbo.Customer
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.
Answer:
SELECT CName
FROM AutoRentals.dbo.Customer
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.
Answer:
SELECT DISTINCT rc.make
FROM AutoRentals.dbo.Rentcost rc
LEFT OUTER JOIN AutoRentals.dbo.Rentals r
ON r.Make = rc.Make
WHERE r.Make is null
18. Using a Type I query, display a unique list of the makes of automobiles that have never been rented.
Answer:
SELECT DISTINCT make
FROM AutoRentals.dbo.Rentcost
WHERE make NOT IN (SELECT DISTINCT make
FROM AutoRentals.dbo.Rentals)
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.
Answer:
SELECT DISTINCT c.CName
FROM AutoRentals.dbo.Rentals r
RIGHT OUTER JOIN AutoRentals.dbo.Customer c
ON r.CID = c.CID
WHERE rtn is null
20. Using a Type II query, display a unique list of customers who have never rented an automobile.
Answer:
SELECT DISTINCT c.CName
FROM AutoRentals.dbo.Customer c
WHERE NOT EXISTS
(
SELECT * FROM AutoRentals.dbo.Rentals r
WHERE r.CID = c.CID
)
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.
Answer:
SELECT DISTINCT c.CName
FROM AutoRentals.dbo.Customer c, AutoRentals.dbo.Rentals r
WHERE c.CID = r.CID
and r.Pickup = c.Resid_City
22. Write a query that will display a list of customers who have not returned their rentals.
Answer:
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.
24. Using a Type II query, show all of the customers who rented an automobile and picked it up in 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 Customer.CID and Rentcost.Make .