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
| rental_id | customer | bike | price |
|---|---|---|---|
| 1 | Ana | city | 10 |
| 2 | Ben | city | 12 |
| 3 | Ana | electric | 25 |
| 4 | Chloe | city | 8 |
| 5 | Ben | electric | 30 |
| 6 | Ana | city | 10 |
| 7 | Dev | cargo | 20 |
| 8 | Chloe | electric | 25 |
| 9 | Ana | electric | 28 |
| 10 | Ben | city | 9 |
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;| bike | rental_count |
|---|---|
| cargo | 1 |
| city | 5 |
| electric | 4 |
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;| bike | revenue |
|---|---|
| electric | 108 |
| city | 49 |
| cargo | 20 |
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;| customer | rental_count | total_spent |
|---|---|---|
| Ana | 4 | 73 |
| Ben | 3 | 51 |
| Chloe | 2 | 33 |
| Dev | 1 | 20 |
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;| customer | rental_count |
|---|---|
| Ana | 4 |
| Ben | 3 |
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;| customer | electric_spent |
|---|---|
| Ana | 53 |
| Ben | 30 |
You just wrote five GROUP BY queries. MintQuery has 500+ more exercises like these, in lessons of a few minutes.
Start freeGet the app