Answer: Using CROSS JOIN
Find a detailed explanation of using CROSS JOIN in SQL.
Solution
The solution is given below:
Press + to interact
-- The query to find employees with sales in each categorySELECT E.EName AS 'Employee Name',PC.CategoryName AS 'Category Name',COUNT(S.SalesID) AS 'Sales Count'FROM Employees ECROSS JOIN ProductCategories PCLEFT JOIN Sales S ON E.EID = S.EID AND S.CategoryID = PC.CategoryIDGROUP BY E.EName, PC.CategoryName;
Code explanation
The explanation of the solution code is given below:
Lines 2–4: The
SELECT
statement selects the columnsEName
,CategoryName
, and count of sales made using the aggregate function with theSalesID
column. We useAS
to set an alias for the columns and tables.Line 5: The data is retrieved from the
Employees
table.Line 6: The
CROSS JOIN
is performed betweenEmployees
andProductCategories
.Line 7: The
LEFT JOIN
is performed withSales
,Employees
, andProductCategories
on the columnsEID
andCategoryID
.Line 8: The
GROUP BY
specifies theEName
andCategoryName
columns to group the data. ...
Recalling
Access this course and 1400+ top-rated courses and projects.