I have three tables: Customers, Receipts, Salary
Customers
Id Name
1 john
Receipts
Id Amount
1 500
2 250
3 600
4 700
Salary
Id Amount
1 300
2 300
3 680
Id like to find sum of salary and receipts related to john. I thought it's simple sql statement like:
select cus.Name, sum(sal.Amount) as Salary, sum(re.Amount) as Recieved
from Customers as cus inner join Salaries as sal on cus.Id = sal.Id
inner join Receipts as re on cus.Id = re.Id
where cus.Id= 1
Group by cus.Name
but I found the result so bigger
Name Salary Recieved
john 6150 4320
When I write the query without sum:
select cus.Name, sal.Amount as Salary, re.Amount as Recieved
from Customers as cus inner join Salaries as sal on cus.Id = sal.Id
inner join Receipts as re on cus.Id = re.Id
where cus.Id= 1
I get these duplicate records
john 500 100
john 500 300
john 500 680
john 250 100
john 250 300
john 250 680
john 600 100
john 600 300
john 600 680
john 700 100
john 700 300
john 700 680
why the inner join behavior like this? I expect to get the records one time but it repeated 4 times! Did I have to try sub-select queries?