Answer: Using INTERSECT
Explore how to use the INTERSECT operator to retrieve employee names with sales in multiple categories by combining SELECT statements. Understand aliases, JOINs, filtering with WHERE, and alternative solutions using GROUP BY, DISTINCT, and subqueries to handle intermediate SQL interview questions focused on set operators.
We'll cover the following...
Solution
The solution is given below:
Code explanation
The explanation of the solution code is given below:
Line 3: The
SELECTstatement selects the columnsEName. The data is retrieved from theEmployeestable. We useASto set an alias for the columns and tables.Line 4: The
JOINis applied withSaleson columnsEIDin both the tables.Line 5: Another
JOINis applied to connectSalesandProductCategorieson columnsCategoryIDin both the tables.Line 6: The
WHEREclause specifies the condition on which we want to retrieve the data from the table. We get the data for the month ofJanuaryandCosmeticscategory.Line 8: The
INTERSECTkeyword is used to intersect the results of two queries.Line 10: The
SELECTstatement selects the columnsEName. The data is retrieved from ...