Hello,
Whenever you want to return values that are present in both tables, use INNER JOIN
.
From tbesquerda inner join tbdireita
on tbesquerda.codigo = tbdireita.codigo
-------------------
|A.NOME | B.VALOR |
-------------------
1.|UM | 1.000 |
2.|UM | 2.000 |
3.|UM | 5.000 |
4.|DOIS | 4.000 |
5.|DOIS | 9.000 |
6.|TRES | 7.000 |
7.|CINCO | 4.000 |
-------------------
Whenever you want to return all the values that are present in the table on the left, use LEFT JOIN
.
From tbesquerda left join tbdireita
on tbesquerda.codigo = tbdireita.codigo
-------------------
|A.NOME | B.VALOR |
-------------------
1.|UM | 1.000 |
2.|UM | 2.000 |
3.|UM | 5.000 |
4.|DOIS | 4.000 |
5.|DOIS | 9.000 |
6.|TRES | 7.000 |
7.|QUATRO | <NULL> |
8.|CINCO | 4.000 |
-------------------
Whenever you want to return all the values that are present in the right table, use RIGHT JOIN
.
From tbesquerda right join tbdireita
on tbesquerda.codigo = tbdireita.codigo
-------------------
|A.NOME | B.VALOR |
-------------------
1.|UM | 1.000 |
2.|UM | 2.000 |
3.|UM | 5.000 |
4.|DOIS | 4.000 |
5.|DOIS | 9.000 |
6.|TRES | 7.000 |
7.|CINCO | 4.000 |
8.|<NULL> | 7.000 |
-------------------
Whenever you want to return all values that are in both tables even though they are null, use FULL OUTER JOIN
.
From tbesquerda full outer join tbdireita
on tbesquerda.codigo = tbdireita.codigo
-------------------
|A.NOME | B.VALOR |
-------------------
1.|UM | 1.000 |
2.|UM | 2.000 |
3.|UM | 5.000 |
4.|DOIS | 4.000 |
5.|DOIS | 9.000 |
6.|TRES | 7.000 |
7.|QUATRO | <NULL> |
8.|CINCO | 4.000 |
9.|<NULL> | 7.000 |
-------------------
Your example
Select tbproduto.produto,
tbcliente.cliente
From tbproduto left join tbcliente
on tbproduto.codigo = tbcliente.codigo
Now model the query for what you are looking for.