SQL Complex views


Example

A view can be a really complex query(aggregations, joins, subqueries, etc). Just be sure you add column names for everything you select:

Create VIEW dept_income AS
SELECT d.Name as DepartmentName, sum(e.salary) as TotalSalary
FROM Employees e
JOIN Departments d on e.DepartmentId = d.id
GROUP BY d.Name;

Now you can select from it as from any table:

SELECT * 
FROM dept_income;
DepartmentNameTotalSalary
HR1900
Sales600