0% found this document useful (0 votes)
11 views6 pages

Exam 1

Uploaded by

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

Exam 1

Uploaded by

Timafa12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

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.

What is the ID of the first record?

SELECT first_name, last_name, actor_id as ID


FROM `classdata-324219.sakila.actor`
WHERE first_name = 'JENNIFER'
UNION ALL
SELECT first_name, last_name, customer_id as ID
FROM `classdata-324219.sakila.customer`
WHERE first_name = 'JENNIFER'
ORDER BY ID

2. How many loans approved in 2020, had an InitialApprovalAmount greater than


30,000 and had 1 Job Reported?
JobsReported is in the project_info table
InitialApprovalAmount is in the project_info table
DateApproved is in the project_info table.

SELECT *, EXTRACT(year FROM l.DateApproved) as y


FROM `classdata-324219.ppp_states.loan_status` as l
INNER JOIN
`classdata-324219.ppp_states.project_info` as p
ON l.LoanNumber = p.LoanNumber
WHERE l.InitialApprovalAmount > 30000
AND p.JobsReported = 1

SELECT *, EXTRACT(year FROM l.DateApproved) as year


FROM `classdata-324219.ppp_states.loan_status`
WHERE InitialApprovalAmount > 30000
AND year = '2020'
UNION ALL
SELECT *
FROM `classdata-324219.ppp_states.project_info`
WHERE InitialApprovalAmount > 30000
AND JobsReported = 1

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

3. For all actors that appeared in more than 11 movies rated R


or more than 9 movies rated G, return the Actor ID,
Rating and the number of films they appeared in for each category.
Use the film_actor and film tables.
Order by Actor ID, Rating and then the number of films.
What how many G rated films did the actor in the last row appear in?

SELECT fa.actor_id, f.rating, f.film_id


FROM `classdata-324219.sakila.film_actor` as fa
INNER JOIN
`classdata-324219.sakila.film` as f
ON fa.film_id = f.film_id
WHERE rating = 'R' OR rating = 'G'
GROUP BY fa.actor_id, f.rating, f.film_id

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?

SELECT customer_id, email


FROM `classdata-324219.sakila.customer`
WHERE active = 1
AND last_name LIKE 'A%____'
ORDER BY customer_id, email

27

Why can't we use the percentage for this question?

SELECT customer_id, email


FROM `classdata-324219.sakila.customer`
where active = 1 and last_name like 'A____'
order by 1,2

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)?

SELECT customer_id, inventory_id


FROM `classdata-324219.sakila.rental`
WHERE date(return_date) between '2005-05-15' AND '2005-05-30'
ORDER BY customer_id, inventory_id

597

Hint: You should have 295 rows in your results set.

Is my code not correct?


6. Q1: Return the customer ID and inventory Id for customers that returned a movie
between
May 15, 2005 and May 30, 2005. (rental table). Order by 2,1.
What is the customer_id of the customer listed at the bottom of the results table
(row 993)?

SELECT customer_id, inventory_id


FROM `classdata-324219.sakila.rental`
WHERE date(return_date) between '2005-05-15' AND '2005-05-30'
ORDER BY 2,1

198

Why is my answer incorrect the correct date should return date?


SELECT customer_id, inventory_id
FROM `classdata-324219.sakila.rental`
Where date(rental_date) between '2005-05-15' and '2005-05-30'
order by 2,1

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.

Hint: You should have 19 rows in your results table.

What is the customer_id of the customer in row 1?

SELECT customer_id, first_name, last_name, email


FROM `classdata-324219.sakila.customer`
WHERE active = 1
AND last_name LIKE 'A%'
AND length(last_name) >= 4
ORDER BY customer_id, first_name, last_name, 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'

Why did my code not work?

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?

SELECT customer_id, email


FROM `classdata-324219.sakila.customer`
WHERE active = 1
AND last_name LIKE 'A%____'
ORDER BY customer_id, email

591

Why does my code not work?


Correct Answer:
SELECT customer_id, email
FROM `classdata-324219.sakila.customer`
where active = 1 and last_name like 'A____'
order by 1,2

11. Return the customer_id, number of rentals and


the total amount of payments for each customer with total payments greater than
$185.00.
Alias the sum amount as Payments.
Use the payment table.
Order by customer_id, number of rentals and then payment amount.
What is the number of rentals in the last row of the results table?

SELECT c.customer_id, p.amount as payments


FROM `classdata-324219.sakila.customer` as c
INNER JOIN
`classdata-324219.sakila.payment` as p
ON c.customer_id = p.customer_id
WHERE amount > 185
GROUP BY p.rental_id
ORDER BY c.customer_id, p.rental_id, p.amount

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?

SELECT actor_id, first_name, last_name


FROM `classdata-324219.sakila.actor`
ORDER BY last_name, first_name, actor_id

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?

SELECT customer_id, inventory_id, rental_date


FROM `classdata-324219.sakila.rental`
WHERE date(rental_date) between '2005-05-15' AND '2005-05-30'
ORDER BY 1, 2, 3

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?

SELECT actor_id, first_name, last_name


FROM `classdata-324219.sakila.actor`
WHERE last_name IN
(SELECT aa.last_name
FROM `classdata-324219.sakila.actor` aa
INNER JOIN
`classdata-324219.sakila.actor` aaa
on aa.last_name = aaa.last_name
WHERE aa.last_name = 'AKROYD' OR aa.last_name = 'ALLEN')
Order BY first_name

58

Correct Answer
select actor_id, first_name, last_name
FROM `classdata-324219.sakila.actor`
where last_name in('AKROID', 'ALLEN')
order by 2

You might also like