Skip to main content

Creating Views

In SQL, creating views can be achieved through the CREATE VIEW statement. A view is a virtual table based on the result-set of an SQL statement. It contains rows and columns from one or more tables. The syntax for the CREATE VIEW statement is:

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Here:

  • CREATE VIEW view_name : It creates a new view that you define with view_name.
  • AS SELECT column1, column2 ... : These are the columns you want in your view. You can choose one or more columns from one or more tables.
  • FROM table_name : table_name is the name of the table from which you want to create the view.
  • WHERE : It is an optional clause that you can use to specify conditions for displaying records.

Example:

Let's say you have a table named Employees having following data:

IDNAMESALARYDEPARTMENT_ID
1John30002
2Sue35003
3Phil45002
4Anna50001

You can create a view that shows only the employees from department 2:

CREATE VIEW Department2 AS
SELECT Name, Salary
FROM Employees
WHERE Department_ID = 2;

After running this statement, Department2 will be a saved view in your database, and you can query it like you would with a standard table:

SELECT *
FROM Department2;

This would bring up

NAMESALARY
John3000
Phil4500

In total, the CREATE VIEW statement is a useful command when you want to save a particular query and its result set for future use. This can simplify complex queries by breaking them up into manageable parts.