I need to intersect multiple subqueries in MS Access. Unfortunately, Access-SQL does not support the INTERSECT keyword. I understand, how we can use INNER JOIN of two tables to get the intersection we want. But how can I programmatically create a query that will make the intersection of N subqueries?
To be more specific: I have a table of Employees and a table of Specializations. Each employee can have multiple specializations which means there's a many to many relationship between Employees and Specializations represented by an additional table which contains the ids of employees and specializations, nothing complicated.
Now let's say that I want a list of employees, all of which have all of the specializations specified somewhere. In any other SQL engine I would simply create a list of subqueries and used the INTERSECTION keyword to "join" these subqueries together creating something like:
SELECT * FROM (
(SELECT id, first_name, last_name FROM Employees JOIN Emp_spec
ON Employee.id = Emp_spec.spec_id WHERE Emp_spec.spec_id=x_1 )
INTERSECT
...
INTERSECT
(SELECT id, first_name, last_name FROM Employees JOIN Emp_spec
ON Employee.id = Emp_spec.spec_id WHERE Emp_spec.spec_id=x_n )
);
, where x_1,...,x_n represent some ids corresponding to some specializations. This query returns a set of employees, all of which have all the specializations x_1,...,x_n. So how do I create such query in Access without the INTERSECT keyword. I've been trying to write the equivalent query with INNER JOIN but I can't seem to succeed.
SELECT DISTINCT Employees.* FROM (Employees INNER JOIN Emp_spec ON Employees.ID = Emp_spec.Employee_ID) INNER JOIN Emp_spec AS Emp_spec_1 ON Employees.ID = Emp_spec_1.Employee_ID WHERE (((Emp_spec.Spec_ID)=1) AND ((Emp_spec_1.Spec_ID)=3));In this example, we are filtering for 1 and 3. It is much easier to visualize in the Query Design Grid. The trick is to bring the Emp_Spec table multiple times. One copy for each criteria. Each one joined to the Employee table by ID. - tbur