Query - Comparison of distinct bases with equal columns

2

How would a query make the selection between two tables according to the value of a specific column?

For example: I have two tables that contain account number and Balance, I need to know which accounts are not with the same balance.

Tab1
Conta Saldo
123   100,00

Tab2
Conta Saldo
123   150,00

In case I need to know which accounts do not have the balance beating.

    
asked by anonymous 30.04.2018 / 21:08

1 answer

3

Depending on the DBMS you use, you will use the "MINUS" or "EXCEPT" function. In the case of SQL Server, your query looks like this:

SELECT Conta, Saldo FROM Tab1 EXCEPT SELECT Conta, Saldo from Tab2.

If you also want to know the different records between the two tables (number of accounts that exist in one and not another) you can also do the following:

Accounts that exist in table 1 and not in table 2:

Select tabela1.conta, tabela1.saldo From tabela1 Left Join Tabela2 On tabela2.conta = tabela1.conta Where tabela2.conta Is Null 

Accounts that exist in table 2 and not in 1:

Select tab2.conta, tab2.saldo From tab2 Left Join Tab1 On tab1.conta = tab2.conta Where tab1.conta Is Null 
    
30.04.2018 / 21:35