-4

I have two tables, employees and transactions, as follows:

 employees: 
 EmployeeID
 firstname
 lastname

 transactions: 
 TransactionID
 employeeID
 amount

How would I get the total 'amount' for all employees with the firstname 'John'? Thanks!

 Select sum(amount) 
 from transactions 
 where employeeID = (everyone with first name John)
Chetan_Vasudevan
  • 2,414
  • 1
  • 13
  • 34
Programmer
  • 1,266
  • 5
  • 23
  • 44

5 Answers5

3

You just need to do a JOIN:

Select  Sum(T.Amount)
From    Transactions    T
Join    Employees       E   On  E.EmployeeId = T.EmployeeId
Where   E.FirstName = 'John'
Siyual
  • 16,415
  • 8
  • 44
  • 58
3

try:

Select sum(amount)
from transactions
where employeeID 
   in (Select employeeID from employees 
       where firstname ='firstname');

thx ;)

nimour pristou
  • 153
  • 1
  • 8
3

You were almost there. Try:

SELECT SUM(amount) 
FROM transactions t
INNER JOIN employees e
    ON t.employeeID = e.employeeID
WHERE e.firstName = 'John'

Alternately, this works as well

SELECT SUM(amount) 
FROM transactions t
WHERE t.employeeId IN (
    SELECT employeeID
    FROM employees e
    WHERE e.firstName = 'John'
    )
Laughing Vergil
  • 3,706
  • 1
  • 14
  • 28
3

An INNER JOIN should do the trick. Take a look at the query provided below:

SELECT SUM(T.amount) FROM
Transactions T INNER JOIN Employees E
ON T.EmployeeID = E.EmployeeID
WHERE E.FirstName = 'John'
N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
2

Here's my guess. You have to join the two tables together, then filter the data that you want.

SELECT     SUM(tr.amount)
FROM       Employees emp
INNER JOIN transactions tr ON emp.employeeId = tr.EmployeeId
WHERE      emp.FirstName = 'John'
Beltaine
  • 2,721
  • 1
  • 18
  • 22