You are on page 1of 28

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 AutoRentals.dbo.Customer
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 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.

You should have 3 rows in your resultset.

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.

Your results should include 8 rows:

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.

You should have 4 rows in your resultset.

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.

You should have 8 rows in your answer:

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.

You should have one row in your result: Tampa

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.

You should have two rows in your resultset:

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.

You should have one cell: 4

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.

You should have one cell: 160

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.

You should have one cell: 1880

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.

You should have 2 rows: Simon and Vernon

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.

You should have 2 rows: Toyota and Volvo

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.

You should have 2 rows: Toyota and Volvo

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.

You should have two rows: Vernon and Wilson

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.

You should have two rows: Vernon and Wilson

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.

You should have 2 rows: Black and Simon

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.

You should have 2 rows: Green and Simon

Answer:

SELECT DISTINCT c.CName


FROM AutoRentals.dbo.Rentals r
INNER JOIN AutoRentals.dbo.Customer c
ON r.CID = c.CID
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 c.CName, c.Age
FROM AutoRentals.dbo.Customer c
INNER JOIN AutoRentals.dbo.Rentals r
ON c.CID = r.CID
WHERE r.Make IN (
SELECT DISTINCT make
FROM AutoRentals.dbo.Rentals r2
WHERE r2.Return_city = 'Erie'
)
AND Date_returned IS NOT NULL
ORDER BY c.CName 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 c.CName
FROM AutoRentals.dbo.Customer c
WHERE EXISTS (
SELECT CID
FROM AutoRentals.dbo.Rentals r
WHERE c.CID = r.CID
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 Customer.CID and Rentcost.Make .

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


Answer:
SELECT c.CID, c.CName, Age, Resid_City, BirthPlace, rc.Make, rc.Cost, rtn, Date_Out,
Pickup, Date_returned, Return_city
FROM AutoRentals.dbo.Customer c
FULL OUTER JOIN AutoRentals.dbo.Rentals r ON r.CID = c.CID
FULL OUTER JOIN AutoRentals.dbo.Rentcost rc ON r.Make = rc.Make
ORDER BY c.CID, rc.Make ASC

You might also like