join

PostgreSQL: Inner Joins | Course | 2019



Sharing buttons:

all right now that we have a foreign key

constraint between our two tables person

and car let's go ahead and learn about

in their joints so any joints is an

effective way of actually combining two

tables and the way it works is that you

have a table a as well as a table B and

what you want to do is actually combine

these two tables now the inner join

takes whatever is common in both tables

so if you have a record inside of the

table a and also a record inside of the

table B so if you have a foreign key

which is present in both tables then it

takes those two records and then gives

you the result of both which we're going

to call it C so a plus B equals to C and

to recap an image Oien takes two tables

and B and then if we have a foreign key

that is present in both tables then we

have a new record called C let's go

ahead and learn how to use any joints

with Postgres all right now that you

know what a join is let's go ahead in

this video perform a join between our

two tables car and person so I'm gonna

select star from person and also select

and then start from an in car so we want

to perform a join based on this foreign

key right here so car ID links to this

ID inside of this car table so to

perform a join we simply have to say

select and then star so we want to

select every single column and then from

and then here is where you specify the

first table so person and then don't

press semi column and then on a new line

simply say join so this is how you join

to another table now you specify the

actual table that you want to join in

our cases car

and then you need to say on so on takes

a column which can be used to join these

two tables so in our example is the

foreign key so car ID found in person

will join to ID found in car so let's go

ahead and say person dot and then car

underscore ID equals two and then car

dot and then ID if you go ahead and

press semicolon press ENTER you can see

that we've got two results and in fact

because you can't see properly I'm gonna

show you a nice trick so if you press Q

and then press backslash and then X and

then enter you can see that we have

expanded display on now if I perform the

same Select you can see that now we have

this information that can be easily read

and there we go you can see that would

performed a joint between two tables and

pretty much this right here is Oh

actually sorry this entire selection is

everything from person and then the rest

is from car and you can see the same for

Fernanda so Fernanda right here she has

the car ID of two and you can see that

this is the actual car and this is how

you perform joints so obviously so if I

scroll up so obviously Adriana is not

included because she doesn't have a car

so remember a joint simply links to

tables where the primary key and the

foreign key is found in both tables so

just let me go ahead and show you one

more thing so you saw that we selected

everything so select star from person so

what we can do just I mean remove that

so so you can see that we get every

single column so now what we can do is

just grab certain columns from each

table so to do that let's go ahead and

say sell leg

and now I can go ahead and say person

don't

and then first name and then comma let's

go ahead and select the car don't and

then make comma car dot and then model

and then car dot and then price and then

we can go ahead on a new line so if I

play the screen on a new line from and

then person and then we going to join so

let's join and then car on and then

person dot car underscore ID equals to

car dot ID semicolon if I press ENTER

you can see that now we selected only

the columns that we wanted and let me go

ahead and remove this expanded display

on and to turn it off back slash eggs

it's a toggle now if I perform the same

selection you can see that if I remove

that you can see that now we simply

selected few columns from each table and

this is how you perform a join in the

next video I'm going to show you how to

perform a left join join me there see ya