Link to home
Start Free TrialLog in
Avatar of asmyatt
asmyatt

asked on

SQL - compare data between two tables

I have two tables that contain the same fields: LoanNumber, LoanAmount

I need a query that gives me all records where the LoanNumber matches, but the LoanAmount is different.

What's wrong with my code below? Thanks!
select LoanNumber, loanamount
from tableA
where (tableA.LoanNumber in 
             (select loannumber from tableB) 
              and tableA.loanamount <> tableB.loanamount)

Open in new window

Avatar of Aaron Tomosky
Aaron Tomosky
Flag of United States of America image

Select loannumber, loanamount, (select loanamount from table b) as loanamountb from tablea
Where loanamount <> loanamountb

This will only work if table b doesn't have loan numbers that are not in tablea
ASKER CERTIFIED SOLUTION
Avatar of Aaron Tomosky
Aaron Tomosky
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No offense intended but that is really not that good of a query.


SELECT A.loannumber
  ,A.loanamount AS LoanAmountA
  ,B..loannumber AS LoanAmountB
FROM tablea A
INNER JOIN tableb B
ON  A.loannumber = B.Lloannumber
WHERE A.loanamount <> B.loanamountb;

Open in new window