Bank Account Summary Problem (LC 1587)

Sonalee Bhattacharyya
1 min readSep 13, 2022

--

Photo by Nick Pampoukidis on Unsplash

I just solved Leetcode problem 1587. In this problem, you are given a table of Users, with their names and account ids. You are also given a table of transactions with the account numbers, amount of the transaction, transaction id, and date of the transaction.

The problem is to produce a list of names of bank account holders with transactions summing to more than 10,000. In other words, if you add up the deposits and subtractions, the total should be more than $10,000.

I had some trouble with this because I tried to use group by and could not get it to work. Then I tried an inner join and it was successful. I realized you have to use having after group by, but where comes before group by. Here is my solution:

SELECT name, sum(t.amount) as balance
FROM Users u
INNER JOIN Transactions t
ON u.account = t.account
GROUP BY name
HAVING SUM(t.amount) > 10000

Every time I solve one of these problems, I feel like I have learned something and become a bit more proficient at SQL.

--

--

Sonalee Bhattacharyya
Sonalee Bhattacharyya

Written by Sonalee Bhattacharyya

Mathematics lecturer transitioning to a career in data analysis

No responses yet