john doe
john doe

Reputation: 23

How should I join these two tables? is it outer join?

I have two tables.

Table A

Table_A
+------+-----+--------+
| Name | Age | Weight |
+------+-----+--------+
| John | 20  | 200    |
+------+-----+--------+
| Jack | 15  | 180    |
+------+-----+--------+

 Table_B
+------+-------+-------+
| Name | Music | Eyes  |
+------+-------+-------+
| John | Rock  | Blue  |
+------+-------+-------+
| Jack | Rap   | Green |
+------+-------+-------+

I want to add the weight column to Table B so end up with something like:

+------+-------+-------+--------+
| Name | Music | Eyes  | Weight |
+------+-------+-------+--------+
| John | Rock  | Blue  | 200    |
+------+-------+-------+--------+
| Jack | Rap   | Green | 100    |
+------+-------+-------+--------+

I am new to SQL and trying to learn join syntax. Is it:

SELECT Table_A.weight, Table_B.Name, Table_B.music, Table_B.eyes 
FROM Table_B
LEFT JOIN Table_A 
    on Table_A.name = Table_B.name

Upvotes: 0

Views: 56

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269503

Your syntax works, but an outer join is not needed for this example. There is an exact match in the key values between the tables.

I recommend also that you use table aliases in your query:

SELECT a.weight, b.Name, b.music, b.eyes 
FROM Table_B b JOIN
     Table_A a
     ON a.name = b.name;

You would use a LEFT JOIN if there were rows in b that are not in a and you wanted them in the result set.

Upvotes: 2

Related Questions