Back to cookbooks list Articles Cookbook

How to Create View in SQL

Problem:

You want to create a view from a table in a database.

Example:

We would like to create a view called it_employee with employees who work in the IT department, based on data from the table employee.

Solution:

CREATE VIEW it_employee AS
SELECT first_name, last_name
FROM employee
WHERE department = 'IT';

Discussion:

If you want to create a new view in a database, use the CREATE VIEW keyword followed by the name of the view (in our example: it_employee). Next is the keyword AS. Then in the SELECT statement, you specify the data you want to select and the table and the columns they come from. In our example, the table is employee, and the data are from the columns first_name and last_name. You can also use a WHERE clause to filter the records displayed in the view. In our example, we used WHERE to select only the employees who work in the IT department named (WHERE department='IT').

Of course, when creating a view, you can use any of the SELECT statement options, which can make the query more complex.

Recommended courses:

Recommended articles:

See also: