Exam 1
Exam 1
Return the first name, last name, ID for all actors and customers with the first
name Jennifer.
Order by: ID
Hint: Use actor and customer tables. You should have two records in your results
table.
Correct Answer:
SELECT count(*)
FROM `classdata-324219.ppp_states.loan_status` as a
inner join
`classdata-324219.ppp_states.project_info` as b
on a.LoanNumber =b.LoanNumber
where InitialApprovalAmount > 30000 and DateApproved < '2021-01-01' and
JobsReported =1
Correct Answer:
SELECT fa.actor_id, f.rating, count(*) as Count
FROM `classdata-324219.sakila.film_actor` fa
inner join
`classdata-324219.sakila.film` f
on fa.film_id = f.film_id
group by fa.actor_id, f.rating
having (f.rating = 'R' and count > 11) or (f.rating = 'G' and count > 9)
order by 1, 2, 3
4. Select the customer_id and email address for all active customers (active = 1)
with the last name beginning with the letter A and is a total of five characters
long?
Use the customer table.
Order by customer_id then email.
What is the customer ID of the customer in row 2?
27
5. Return the customer ID and inventory ID for customers that returned a movie
between May 15, 2005 and May 30, 2005. (use rental table).
Order by customer ID, inventory ID
What is the customer_id of the customer listed at the bottom of the results table
(row 993)?
597
198
7. Return the customer ID, first name, last name and email address of all active
customers with the last names that start with the letter A and is four or more
letters long.
Order by customer_id, first_name, last_name and email.
11
8. Write a query against the rental table that returns the IDs of the customers
who rented a film on August 23, 2005.
(use the rental_date column, and you can use the date() function to ignore the time
component).
Include only one row for each customer ID.
Order by customer_id descending.
What is the seventh ID in the list?
SELECT customer_id,rental_date
FROM `classdata-324219.sakila.rental`
WHERE date(rental_date) = '2005-08-23'
ORDER BY customer_id desc
595
Missed adding Distinct! but it didn't say unique. But wouldn't the code provided be
removing rentals?
SELECT distinct customer_id
From `classdata-324219.sakila.rental`
Where date(rental_date) = '2005-08-23'
order by 1 desc;
Correct Answer 586
9. How many Male Owned businesses received loans between March 01, 2020 and March
15, 2020?
SELECT count(*)
FROM `classdata-324219.ppp_states.demographics` as d
INNER JOIN
`classdata-324219.ppp_states.loan_status` as l
ON d.LoanNumber = l.LoanNumber
WHERE d.Gender = 'Male Owned'
AND date(l.DateApproved) BETWEEN '2020-03-01' AND '2020-03-15'
SELECT count(*)
FROM
`classdata-324219.ppp_states.loan_status` as a
inner join
`classdata-324219.ppp_states.demographics` as b
on a.LoanNumber = b.LoanNumber
where a.DateApproved between '2021-03-01' and '2021-03-15' and upper(Gender) =
'MALE OWNED'
10. Select the customer_id and email address for all active customers (active = 1)
with the last name beginning with the letter A and is a total of five characters
long?
Use the customer table.
Order by customer_id then email.
What is the customer ID of the last record?
591
Correct Answer:
select customer_id, count(*) Num_Rentals, sum(amount) as Payments
FROM `classdata-324219.sakila.payment`
group by customer_id
having sum(amount) > 185
order by 1,2,3
Count the inventory at store number one (use inventory table). What is the count
(no commas in the number)?
SELECT count(*)
FROM `classdata-324219.sakila.inventory`
WHERE store_id = 1
2270
Retrieve the actor ID, first name and last name for all actors.
Sort by last name and then by first name, and then actor_id.
Use the actor table.
What is the 7th actor ID from the top?
76
Why is my code incorrect?
select actor_id, first_name, last_name
FROM `classdata-324219.sakila.actor`
order by 2, 3, 1
Return a list of all the first names of all the customers and actors. Remove
duplicate names.
Sort the results by the first_name.
Use the actor and customer tables.
Order by FirstName.
How what is the name in row 3?
SELECT first_name
FROM `classdata-324219.sakila.actor`
UNION DISTINCT
SELECT first_name
FROM `classdata-324219.sakila.customer`
ORDER BY first_name
Adrian
Return the customer ID, inventory Id and rental_date for customers that rented a
movie between May 15, 2005 and May 30, 2005.
Order by 1, 2, 3
Use the rental table.
Hint: You should have 993 rows in your results set.
What is the inventory ID on row 2?
4020
Retrieve the actor ID, first name, last name for all actors whose last name equals
AKROID or ALLEN.
Note: You must use the IN operator.
Use the actor table.
Order by first name.
What is the actor ID in row 1?
58
Correct Answer
select actor_id, first_name, last_name
FROM `classdata-324219.sakila.actor`
where last_name in('AKROID', 'ALLEN')
order by 2