MintQueryStart free
← All guides

SQL GROUP BY and HAVING practice: 5 exercises you can run in the browser

A bike rental shop has been open for one week, and you are the only person there who knows SQL. The owner has ten rentals in a table and a lot of questions about them. Each exercise below is one of those questions, easiest first. The table is small enough to check every answer by eye, and there is nothing to install and no account needed.

The table

rentals
rental_idcustomerbikeprice
1Anacity10
2Bencity12
3Anaelectric25
4Chloecity8
5Benelectric30
6Anacity10
7Devcargo20
8Chloeelectric25
9Anaelectric28
10Bencity9

One row is one rental. The shop has three kinds of bike: city, electric and cargo.

Exercise 1: your first GROUP BY

Which bikes go out the most?

Show each kind of bike with the number of times it was rented, as rental_count. Sort by bike.

Hint

GROUP BY the column you want one row for, and COUNT the rows in each group.

Show solution
SELECT bike, COUNT(*) AS rental_count
FROM rentals
GROUP BY bike
ORDER BY bike;
bikerental_count
cargo1
city5
electric4

Exercise 2: add it up per group

Fine, but which ones make the money?

Show each kind of bike with the money it brought in, as revenue. Put the biggest earner first.

Hint

SUM the price inside each group, then sort by that sum.

Show solution
SELECT bike, SUM(price) AS revenue
FROM rentals
GROUP BY bike
ORDER BY revenue DESC;
bikerevenue
electric108
city49
cargo20

Exercise 3: two numbers per group

Who are our regulars, and what have they spent?

Show each customer with how many times they rented, as rental_count, and what they paid in total, as total_spent. Sort by customer.

Hint

One GROUP BY can feed several aggregates in the same SELECT.

Show solution
SELECT customer, COUNT(*) AS rental_count, SUM(price) AS total_spent
FROM rentals
GROUP BY customer
ORDER BY customer;
customerrental_counttotal_spent
Ana473
Ben351
Chloe233
Dev120

Exercise 4: filter the groups with HAVING

I want to give a loyalty card to anyone who has rented more than twice.

Show the customers who get a loyalty card, as customer and rental_count. Sort by customer.

Hint

The condition is about a count, so it cannot go in WHERE.

Show solution
SELECT customer, COUNT(*) AS rental_count
FROM rentals
GROUP BY customer
HAVING COUNT(*) > 2
ORDER BY customer;
customerrental_count
Ana4
Ben3

Exercise 5: WHERE and HAVING together

The electric bikes cost me a fortune. Which customers have spent 30 or more on them?

Looking only at electric bikes, show each customer who spent 30 or more on them, with the amount as electric_spent. Sort by customer.

Hint

Pick the electric rows first, then group, then keep the groups with a big enough sum.

Show solution
SELECT customer, SUM(price) AS electric_spent
FROM rentals
WHERE bike = 'electric'
GROUP BY customer
HAVING SUM(price) >= 30
ORDER BY customer;
customerelectric_spent
Ana53
Ben30

You just wrote five GROUP BY queries. MintQuery has 500+ more exercises like these, in lessons of a few minutes.

Start freeGet the app