Skip to content
Advertisement

SQL Interview Test

Having been given the following question as part of an interview I am keen to know the optimum solution. The question has been edited so that it is not identifiable.

Question

Table ‘transactions’ has the following structure:

Write a query that returns a list of receivers that have received at least 1024 USD in at most 3 transactions. There can be more than 3 transters to that account, as long as some 3 or fewer transactions usd_value total to at least 1024 USD. The table should be ordered by name (in ascending order).

Example, given data:

the query should return the following rowset:

The Bjorn account is listed because it has received 1112 USD in the following three transactions 512 USD + 100 USD + 500 USD = 1112 USD. The Timpson account has received 1024 USD in just one transfer. The Willhelm account received 1200 USD in four transactions, but is it not listed because no three transactions to that account total at least 1024 USD.

My solution:

Set up data (postgreSQL):

— I have attempted to set up in www.sqlfiddle.com – http://sqlfiddle.com/#!15/13fc3/3


Any hints as to how I should have approached this question greatly appreciated.

Advertisement

Answer

You solution seems fine, but you could simplify the query by using the having clause:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement