SQL query join

Hello,

I'm having trouble with this query. I have two tables:

tableA
ID      Type  
1      apple
2      orange
3      pear

tableB
1      apple
2      orange

The ID is joined and I want it to return all the records in tableA regardless like this:

ID      TypeFromTableA     TypeFromTableB
1      apple                       apple
2      orange                       orange
3      pear

Thanks,
Joel
Genius123Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can use this to do what you are looking for...

select tb1.id,tb1.type,tb2.type
from tablea tb1
left join tableb tb2 on tb1.id=tb2.id

Open in new window


Saurabh...
Jeff DarlingDeveloper AnalystCommented:
another way is a full join.

select A.id,a.mytype,b.mytype from #tableA A
full join #tableB B on A.ID = B.ID

Open in new window

Tj aCommented:
I would probably also use a full join
SELECT tableA.id, tableA.type, tableB.type
FROM tableA
FULL OUTER JOIN tableB
ON tableA.id = tableB.id;

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Genius123Author Commented:
I get an error on FULL OUTER JOIN.
It says "Syntax error in FROM clause."

I'm running this from Access.  Any ideas?
Saurabh Singh TeotiaCommented:
Did you try the query which i posted??
Dale FyeOwner, Developing Solutions LLCCommented:
Access doesn't do FULL JOIN
Jeff DarlingDeveloper AnalystCommented:
Please disregard my suggestion,  didn't notice this was for Microsoft Access since it is posted in MS SQL Server area.

In that case, I have to go with left join as indicated by Saurabh Singh Teotia.

SELECT Table1.ID, Table1.myType, Table2.myType
FROM Table1 LEFT JOIN Table2 ON Table1.ID = Table2.ID;

Open in new window

Genius123Author Commented:
Thank you both, that worked!  For some reason, Saurabh's solution didn't work quite as well as Jeff's, but I could have been doing something wrong.  Jeff's code worked after I changed the table names to real names.
Saurabh Singh TeotiaCommented:
Geninus,

The difference between me and jeff solution is an Semicolon at the end since in order to run the query in access it need to have an Semicolon in the end.. So if you add that it will work...

select tb1.id,tb1.type,tb2.type
from tablea tb1
left join tableb tb2 on tb1.id=tb2.id;

Open in new window


Again not for points but i'm not in agreement the way this question as closed and solution has been accepted as i don't see any significant value or changes suggested from my original solution which i posted...

Saurabh...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Genius123Author Commented:
Sorry, I can't figure out how to change who has the solution.  Is there a way to do that?
Dale FyeOwner, Developing Solutions LLCCommented:
Yes,

Click the Request Attention hyperlink at the bottom right of your original post.  Then, indicate to the moderators that you want to reallocate points.
Genius123Author Commented:
Thank you.  Everyone was very prompt with suggestions to help me.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.