Skip to content
Advertisement

Print out everything from table 1 and matching data from table 2

I am trying to learn sql and I came across a basic question. This problem mainly focuses on the join, which join to use in order to execute the correct data. What queries I should write to validate and compare between these two tables? Let’s say I have two tables (parent and child):

Parent has two columns ID(Primary key) | data1

Child also has two columns PID(Foreign key) | data2

And I am trying to print out everything from parent and record from child(if matching..)

What query should I write? I have these lines so far:

Select * from parent,
(
Select * child
(
If ( parent.data1 = child.data2)
))

Order by parent.id; 

Advertisement

Answer

I am trying to print out everything from parent and record from child(if matching..)

You are describing a left join:

select p.*, c.data2
from parent p
left join child c on c.pid = p.id
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement