The following will order the customers by the sum of the balances:
SELECT C.Customer#, C.Name, Address, SUM(A.Balance)
FROM Customer C JOIN
Account A
ON C.BSB# = A.BSB# AND
C.Customer# = A.Customer#
GROUP BY C.Customer#, C.Name, Address
ORDER BY SUM(A.Balance);
If you want only one row, then that depends on the database. Here are some methods:
Change the select
to:
SELECT TOP 1 . . .
Add a limit
clause to the end of the query:
LIMIT 1
Add a fetch
clause to the end of the query:
FETCH FIRST 1 ROWS ONLY
And in Oracle, you can do this with a subquery:
SELECT *
FROM (SELECT C.Customer#, C.Name, Address, SUM(A.Balance)
FROM Customer C JOIN
Account A
ON C.BSB# = A.BSB# AND
C.Customer# = A.Customer#
GROUP BY C.Customer#, C.Name, Address
ORDER BY SUM(A.Balance)
) t
WHERE rownum = 1;