TopicsCheat sheetLessons
Lurndo › Topics › SUM

Learn SQL: SUM

Adding up with SUM

"And what did that bring in?" Lien pushes a shoebox full of till receipts aside. "The accountant wants a total." That can be done faster.

SUM adds up values

COUNT counts rows, SUM adds up the values in a column. This is the total revenue of the summer.

Just like COUNT, SUM returns one row, and just like COUNT(column), it skips NULL.

SELECT SUM(amount) AS revenue FROM rentals
Result
revenue
1701

Count or add up?

Mind the difference: COUNT(days) tells you how many rentals there are, SUM(days) how many days were rented out in total.

And a pitfall: if not a single row is left after WHERE, SUM doesn't return 0 but NULL. After all, there was nothing to add up.

SELECT COUNT(days) AS rentals, SUM(days) AS rental_days FROM rentals
Result
rentalsrental_days
3067

Try it yourself

What was the revenue in August?

Ready for more?

The full lesson has 7 exercises, from easy to challenging, with hints, points and a streak you can build.

Start the lesson
← PreviousCounting with COUNTNext →Averages with AVG
SQL topicsCheat sheetExcelLeaderboardAccountPrivacyNederlands