Sql nested select

database , sql , select Haifa, Israel
  • 7 years ago

    I have 3 tables: people, loans, and deposits people: id, name, address etc loans and deposits tables are identical: id, sum,date etc

    each person may have any number of loans or deposits, I want to know how much sum total money this person has in his account now.

    for the loans:

    select people.id,sum(loans.sum) from people INNER JOIN loans ON people.id=loans.id group by people.id

    i did the same for the deposits table

    select people.id,sum(deposits.sum) from people INNER JOIN deposits ON people.id=deposits.id group by people.id

    How can I combine these two to get ONE number for each id? I know there is something called a nested select, but I can't figure out how to add it in this case

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter” - Eric Raymond