Compare items from different tables

2

I have two tables:

Pessoa1                     Pessoa2
+----+--------+             +----+--------+
| Id |  Nome  |             | Id |  Nome  |
+----+--------+             +----+--------+
| 1  | Maria  |             | 3  | Maria  |
| 2  | João   |             | 4  | João   |
+----+--------+             | 5  | Pedro  |
                            +----+--------+

I would like to get the names that appear in both tables.

Resultado
+--------+
|  Nome  |
+--------+
| Maria  |
| João   |
+--------+
    
asked by anonymous 08.09.2015 / 22:44

2 answers

4

Since the ID of both tables are the same, you can do the following:

SELECT a.Nome FROM Pessoa1 a JOIN Pessoa2 b ON a.Id = b.Id;

Or change the Id by Name, but the result would be the same and the ideal is to always use the identifier.

EDIT

Depending on the issue you made in the question:

SELECT a.Nome FROM Pessoa1 a JOIN Pessoa2 b ON a.Nome = b.Nome;
    
08.09.2015 / 22:47
2

You can use INNER JOIN for column Nome

SELECT Pessoa1.Nome FROM Pessoa1 INNER JOIN Pessoa2 ON (Pessoa1.Nome = Pessoa2.Nome)

EDIT:

It is not ideal to use INNER JOIN without an indexed column, you can leave the query very slow depending on the amount of record you have in both tables.

    
08.09.2015 / 22:47