writing SQL in Access 2010

I have two tables in Access 2010 let's say table1 and table2. Both tables have field 1, field 2, and field 3 that are common fields. I want to query all the records in table 1 where field 1, field 2, and field 3 exist in table2. So basically something like

Select * from table1 where table1.field1 = table2.field2 and table1.field2 = table2.field2 and table1.field3 = table2. field3

let's say I want to find all the records in table 1 where name, phone, and address all exist in table 2. I just don't know how to write that exactly. Thanks!
Who is Participating?

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

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.

PortletPaulEE Topic AdvisorCommented:
To locate all rows that match via the  common fields you use an INNER JOIN, like this:
Select * 
from table1 
inner join table2 on  table1.field1 = table2.field2 and table1.field2 = table2.field2 and table1.field3 = table2. field3

Open in new window

Once you have that join in place you an also add further filtering conditions in the where clause like this. Note the logic you require MIGHT be better using OR's instead of AND's in the tht where clause, you may have to experiment with those.
Select * 
from table1 
inner join table2 on  table1.field1 = table2.field2 and table1.field2 = table2.field2 and table1.field3 = table2. field3
where table1.name= table2.name
and table1.phone=table2.phone
and table1.address=table2.address

Open in new window

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
SharathData EngineerCommented:
In case if it complains, you can enclose the JOIN in parentheses.
FROM   (table1 
        INNER JOIN table2 
                ON table1.field1 = table2.field2 
                   AND table1.field2 = table2.field2 
                   AND table1.field3 = table2. field3) 

Open in new window

Paul is on the right track.  If you want exact matches on all three fields, use his first example.  No WHERE clause is necessary.  If you want to get partial matches, then use a cross join  with OR as the relational operator for the WHERE clause.  But in the case where you are using the "OR", you will need to select the relevant columns from BOTH tables.

Select table1.*, table2.*
from table1, table2
where table1.name= table2.name
OR table1.phone=table2.phone
OR table1.address=table2.address
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

mrosierAuthor Commented:
Thanks folks! So if in the results I get for this query, if I delete records, it only deletes from table1 since I am only pulling FROM table1, correct?
mrosierAuthor Commented:
The query worked great, I managed to query all the records I wanted to query. I tried deleting them from table1 as that is my ultimate goal, but Access wouldn't allow that. I worked around it via using the unique IDs of these records, created a new table with these results, and just deleted from table1 using referencing this table. Is there an easy way to perform a delete function using this same query though?
You didn't say which query you used.  The cross join query is not updateable so you wouldn't be able to delete from either table.

In a query that joins multiple tables, you can only delete from the lowest level "child" table.  These tables do not have relationships defined so you probably won't be able to delete from either table since Access won't be able to determine the relationship to know which table is the "child".  

Therefore, you are going to have to identify the records using one of these queries and then save the IDs you want to delete into a temp table and use that to control the deleting.
mrosierAuthor Commented:
Oh I used Paul's query, and thanks for the info Pat!
mrosierAuthor Commented:
Paul had a typo in his query, but I believe his intentions should be clear. He had said table2.field2 at the start of the join when it should be table2.field1
PortletPaulEE Topic AdvisorCommented:
inner join table2 on  table1.field1 = table2.field2

mea culpa that was an error, it should have been

          inner join table2 on  table1.field1 = table2.field1

(in my defense I was answering using a phone bouncing about in a train :-)

mrosierAuthor Commented:
Wow thanks Paul! I think EE should get that train moment on video and pay you for an advertisement emphasizing troubleshooting from anywhere :p
PortletPaulEE Topic AdvisorCommented:
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.