Answer: Using INTERSECT
Find a detailed explanation of using INTERSECT in SQL queries.
We'll cover the following...
Solution
The solution is given below:
/* The query to find employees who have sales in themonth of January for both Cosmetics and Footwear */SELECT E.EName AS 'Employee Name' FROM Employees EJOIN Sales S ON E.EID = S.EIDJOIN ProductCategories PC ON S.CategoryID = PC.CategoryIDWHERE S.Month = 'January' AND PC.CategoryName = 'Cosmetics'INTERSECTSELECT E.EName AS 'Employee Name' FROM Employees EJOIN Sales S ON E.EID = S.EIDJOIN ProductCategories PC ON S.CategoryID = PC.CategoryIDWHERE S.Month = 'January' AND PC.CategoryName = 'Footwear';
Code explanation
The explanation of the solution code is given below:
Line 3: The
SELECT
statement selects the columnsEName
. The data is retrieved from theEmployees
table. We useAS
to set an alias for the columns and tables.Line 4: The
JOIN
is applied withSales
on columnsEID
in both the tables.Line 5: Another
JOIN
is applied to connectSales
andProductCategories
on columnsCategoryID
in both the tables.Line 6: The
WHERE
clause specifies the condition on which we want to retrieve the data from the table. We get the data for the month ofJanuary
andCosmetics
category.Line 8: The
INTERSECT
keyword is used to intersect the results of two queries.Line 10: The
SELECT
statement selects the columnsEName
. The data is retrieved from ...