0% found this document useful (0 votes)
10 views

Extra-Practice-Problems-for-SQL

Uploaded by

sagarvshinde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Extra-Practice-Problems-for-SQL

Uploaded by

sagarvshinde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL Query Extra Practice

Use these as extra practice – either for your SQL #1 assignment, studying for the exams, or both!

Single Table Queries


For these use the mxsales database (remember to use your own id – for example, m100sales). It has a
single table, called retail_sales_feb. The table has the following fields (columns):

Field name Data type Description


OrderID integer The unique ID of the order
Ord_Date date The date of the order
Order_No integer The internal order number for tracking purposes
Cust_No integer The customer number associated with the order
Customer varchar The customer name associated with the order
Sales_Rep varchar The last name of the sales representative for the customer
Product_No varchar The product number of the product in the order
Product varchar Short name of the product in the order
Department varchar The name of the department from where the order comes
Quantity integer The number sold of that product for the order
Price decimal The unit price of the product sold in the order
Total decimal The total price (Price * Quantity) for the order

It may also be helpful to run a query that retrieves all data in the table to see how it’s structured (i.e.,
SELECT * FROM mxsales.retail_sales_feb).

1. How many customers bought a dartboard?

2. How many customers bought a treadmill?

3. How many customers bought something from the Computer department?

4. What were the names of customers who bought an Organizer?

5. What are the names of customers who had Dakic as a sales rep?

6. Which sales rep had the highest total sales (list them all to find out)?

7. Which sales rep sold the most laser printers (list them all to find out)?

8. How many total items were sold overall?

9. What is the least expensive product in the database?

10. What is the name of the customer who had Sheedy as a sales rep and bought a Color Scanner?

11. Which customers bought products more than $300 and what were the names of those products?
Multi-table Queries (Joins)
For these use the moviedb database. Make sure you look at the Movie Rental Schema.

12. Which customer lives at the address “1531 Sal Drive?”

13. How many customers live in Canada?

14. Which country has the most customers (list them all to find out)?

15. What is the postal code of Cape Coral?

16. What is the largest single payment made by Emily Diaz? (use a subselect)

17. Which customers made the largest single payment in India? What was the amount? (use a
subselect)
Answer Key for SQL Practice Problems
Remember, your schema name will look a little different (i.e., m100sales, m118sales, etc.). Use your own MySQL account.

Question Query Answer


1. How many customers bought a dartboard? SELECT COUNT(*) FROM m0sales.retail_sales_feb 3
WHERE product = 'Dartboard';
2. How many customers bought a treadmill? SELECT COUNT(*) FROM m0sales.retail_sales_feb 7
WHERE product = 'Treadmill';
3. How many customers bought something from SELECT COUNT(DISTINCT customer) FROM m0sales.retail_sales_feb 8
the Computer department? WHERE department = 'Computer';
4. What were the names of customers who SELECT DISTINCT Customer FROM m0sales.retail_sales_feb Smith Ben
bought an Organizer? WHERE product='Organizer'; Martin Dan
Jones Mathew
Rose Clair
Lopez Maria
5. What are the names of customers who had SELECT DISTINCT Customer FROM m0sales.retail_sales_feb Sweet Mary
Dakic as a sales rep? WHERE sales_rep='Dakic'; Rose Clair
Martin Dan
Jovanovic Robert
Smith Ben
6. Which sales rep had the highest total sales SELECT Sales_Rep,SUM(Total) Jenkin 9158.00
(list them all to find out)? FROM m0sales.retail_sales_feb Dakic 7874.70
GROUP BY Sales_Rep Salt 6429.85
ORDER BY SUM(Total) DESC; Parker 4241.98
Smith 3931.06
Sheedy 1952.50
Ibrahim 1469.37
Walsh 900.00
7. Which sales rep sold the most laser printers SELECT Sales_Rep,SUM(Quantity) FROM m0sales.retail_sales_feb Dakic 14
(list them all to find out)? WHERE Product='Laser Printer' Jenkin 9
GROUP BY Sales_Rep Salt 3
ORDER BY SUM(Quantity) DESC; Parker 2
Sheedy 1
8. How many total items were sold overall? SELECT SUM(Quantity) FROM m0sales.retail_sales_feb; 321
9. What is the least expensive product in the SELECT Product FROM m0sales.retail_sales_feb Mouse Mat
database? WHERE Price=(SELECT MIN(Price) FROM m0sales.retail_sales_feb);
10. What is the name of the customer who had SELECT Customer FROM m0sales.retail_sales_feb Smith Ben
Sheedy as a sales rep and bought a Color WHERE Sales_Rep='Sheedy'
Scanner? AND Product='Color Scanner';
11. Which customers bought products more than SELECT Customer,Product Nelson Mary Walkman
$1000 and what were the names of those FROM m0sales.retail_sales_feb Dinh Tran Walkman
products? WHERE Price > 1000; Rose Clair Digital Camera
12. Which customer lives at the address “1531 SELECT customer.first_name, customer.last_name DOROTHY TAYLOR
Sal Drive?” FROM moviedb.customer, moviedb.address
WHERE customer.address_id = address.address_id
AND address='1531 Sal Drive';
13. How many customers live in Canada? SELECT COUNT(*) FROM moviedb.customer,moviedb.address, 5
moviedb.city,moviedb.country
WHERE customer.address_id = address.address_id
AND address.city_id = city.city_id
AND city.country_id = country.country_id
AND country.country='Canada';
14. Which country has the most customers (list SELECT Country, COUNT(*) India 60
them all to find out)? FROM moviedb.customer,moviedb.address,
moviedb.city,moviedb.country
WHERE customer.address_id = address.address_id
AND address.city_id = city.city_id
AND city.country_id = country.country_id
GROUP BY Country
ORDER BY COUNT(*) DESC;
15. What is the postal code of Cape Coral? SELECT postal_code 31342
FROM moviedb.address,moviedb.city
WHERE address.city_id = city.city_id
AND city.city='Cape Coral';
16. What is the largest single payment made by SELECT payment.amount 8.99
Emily Diaz? (use a subselect) FROM moviedb.payment,moviedb.rental,moviedb.customer
WHERE payment.rental_id=rental.rental_id
AND rental.customer_id=customer.customer_id
AND customer.first_name='Emily' and customer.last_name='Diaz'
AND payment.amount=(SELECT MAX(payment.amount)
FROM moviedb.payment,moviedb.rental,moviedb.customer
WHERE payment.rental_id=rental.rental_id
AND rental.customer_id=customer.customer_id
AND customer.first_name='Emily' and customer.last_name='Diaz');
17. Which customers made the largest single SELECT payment.amount,customer.first_name,customer.last_name 10.99 ADAM GOOCH
payment in India? What was the amount? FROM moviedb.payment,moviedb.rental,moviedb.customer, 10.99 SHERRI RHODES
(use a subselect) moviedb.address, moviedb.city,moviedb.country 10.99 ERIK GUILLEN
WHERE payment.rental_id=rental.rental_id 10.99 TIM CARY
AND rental.customer_id=customer.customer_id 10.99 LENA JENSEN
AND customer.address_id=address.address_id 10.99 ANITA MORALES
AND address.city_id=city.city_id 10.99 JOHN FARNSWORTH
AND city.country_id=country.country_id 10.99 BRADLEY
AND country.country='India' MOTLEY
AND payment.amount=(SELECT MAX(payment.amount) 10.99 BRADLEY
FROM moviedb.payment,moviedb.rental,moviedb.customer, MOTLEY
moviedb.address, moviedb.city,moviedb.country 10.99 LORI WOOD
WHERE payment.rental_id=rental.rental_id 10.99 JOSHUA MARK
AND rental.customer_id=customer.customer_id 10.99 NANCY THOMAS
AND customer.address_id=address.address_id
AND address.city_id=city.city_id
AND city.country_id=country.country_id
AND country.country='India');

You might also like