Answer: Creating Views
Find a detailed explanation of creating views in SQL.
Solution
The solution is given below:
Press + to interact
/*Query to create view named SalesTransactionsByCategoryto show each sales transaction with thecorresponding category by each employee every month*/CREATE VIEW SalesTransactionsByCategory ASSELECT S.SalesID AS 'Sales ID',E.EName AS 'Employee Name',PC.CategoryName AS Category,S.SalesAmount AS 'Sales Amount',S.Month AS MonthFROM Sales SJOIN ProductCategories PC ON S.CategoryID = PC.CategoryIDJOIN Employees E ON S.EID = E.EID;SELECT * FROM SalesTransactionsByCategory ORDER BY Category ASC;
Code explanation
The explanation of the solution code is given below:
Line 4: The
CREATE VIEW
statement creates a view namedSalesTransactionsByCategory
.Lines 5–9: The columns of the view are
Sales ID
,Employee Name
,Category
,Sales Amount
, andMonth
.Line 10: The data is retrieved from the
Sales
table.Line 11: The
JOIN
is performed withSales
andProductCategories
on the columnsCategoryID
in both the tables.Line 12: The result of the previous
JOIN
is joined ...
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy