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 SalesTransactionsByCategory
to show each sales transaction with the
corresponding category by each employee every month*/
CREATE VIEW SalesTransactionsByCategory AS
SELECT S.SalesID AS 'Sales ID',
E.EName AS 'Employee Name',
PC.CategoryName AS Category,
S.SalesAmount AS 'Sales Amount',
S.Month AS Month
FROM Sales S
JOIN ProductCategories PC ON S.CategoryID = PC.CategoryID
JOIN 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 named SalesTransactionsByCategory.

  • Lines 5–9: The columns of the view are Sales ID, Employee Name, Category, Sales Amount, and Month.

  • Line 10: The data is retrieved from the Sales table.

  • Line 11: The JOIN is performed with Sales and ProductCategories on the columns CategoryID 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