21

I have researched and haven't found a way to run INTERSECT and MINUS operations in MS Access. Does any way exist

1
  • EXCEPT is the ANSI/ISO SQL term for MINUS.
    – jarlh
    Commented Sep 23, 2022 at 13:51

6 Answers 6

32

INTERSECT is an inner join. MINUS is an outer join, where you choose only the records that don't exist in the other table.


INTERSECT

select distinct
  a.*
from
  a
  inner join b on a.id = b.id

MINUS

select distinct
  a.*
from
  a
  left outer join b on a.id = b.id
where
  b.id is null

If you edit your original question and post some sample data then an example can be given.

EDIT: Forgot to add in the distinct to the queries.

6
  • Why so? The logic produces the same results as the SQL standard INTERSECT and MINUS.
    – LeppyR64
    Commented Dec 3, 2008 at 15:12
  • 1
    This is exactly what I was saying and I got voted down. Ah well. +1 for you, since you took the time to actually give syntax examples, though. Commented Dec 3, 2008 at 15:18
  • I'm not redefining any standards. MS Access does not use the standard.
    – LeppyR64
    Commented Dec 3, 2008 at 15:38
  • 1
    But this still only works properly if "ID" is a candidate key table a. Commented Dec 3, 2008 at 15:58
  • You need to have something to compare against, regardless, Dave. Can you elaborate? Commented Dec 3, 2008 at 16:03
16

INTERSECT is NOT an INNER JOIN. They're different. An INNER JOIN will give you duplicate rows in cases where INTERSECT WILL not. You can get equivalent results by:

SELECT DISTINCT a.*
FROM a
INNER JOIN b
   on a.PK = b.PK

Note that PK must be the primary key column or columns. If there is no PK on the table (BAD!), you must write it like so:

SELECT DISTINCT a.*
FROM a
INNER JOIN b
   ON  a.Col1 = b.Col1
   AND a.Col2 = b.Col2
   AND a.Col3 = b.Col3 ...

With MINUS, you can do the same thing, but with a LEFT JOIN, and a WHERE condition checking for null on one of table b's non-nullable columns (preferably the primary key).

SELECT DISTINCT a.*
FROM a
LEFT JOIN b
   on a.PK = b.PK
WHERE b.PK IS NULL

That should do it.

1
  • Cheers, forgot to add the distinct in my post. +1.
    – LeppyR64
    Commented Dec 3, 2008 at 15:56
3

They're done through JOINs. The old fashioned way :)

For INTERSECT, you can use an INNER JOIN. Pretty straightforward. Just need to use a GROUP BY or DISTINCT if you have don't have a pure one-to-one relationship going on. Otherwise, as others had mentioned, you can get more results than you'd expect.

For MINUS, you can use a LEFT JOIN and use the WHERE to limit it so you're only getting back rows from your main table that don't have a match with the LEFT JOINed table.

Easy peasy.

0
0

Unfortunately MINUS is not supported in MS Access - one workaround would be to create three queries, one with the full dataset, one that pulls the rows you want to filter out, and a third that left joins the two tables and only pulls records that only exist in your full dataset.

Same thing goes for INTERSECT, except you would be doing it via an inner join and only returning records that exist in both.

2
  • Your MINUS implementation is much too convoluted. -1
    – LeppyR64
    Commented Dec 3, 2008 at 14:28
  • It's not Access that doesn't support it. If you write a passthrough to SQL server using MINUS or INTERSECT it will work just fine. It's the Jet database engine whose SQL dialect does not these two operators. All SQL dialects are incomplete so this isn't some huge problem with Jet. Commented Dec 5, 2008 at 3:49
0

No MINUS in Access, but you can use a subquery.

SELECT DISTINCT a.*
FROM a
WHERE a.PK NOT IN (SELECT DISTINCT b.pk FROM b)
-1

I believe this one does the MINUS

SELECT DISTINCT
  a.CustomerID, 
  b.CustomerID
FROM 
  tblCustomers a
LEFT JOIN 
  [Copy Of tblCustomers] b
ON
  a.CustomerID = b.CustomerID
WHERE
  b.CustomerID IS NULL
3
  • how is this answer different from the others? Commented Aug 16, 2019 at 6:01
  • Actually, I tried the other answers, and I felt compelled to share that it works.
    – Porebo
    Commented Aug 16, 2019 at 6:20
  • then you can then just wait to be able to upvote and upvote it later Commented Aug 16, 2019 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.