Day 66 – SQL CREATE VIEW Keyword
SQL CREATE VIEW keyword is used to create a virtual table based on the result of a SELECT query. A view is a stored SQL query that can be referenced as if it were a table, and it can be used to simplify complex queries, to provide a controlled view of data to different users or applications, or to hide sensitive or irrelevant data from users.
The basic syntax for using the CREATE VIEW keyword is as follows:
CREATE VIEW view_name
AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Here, view_name
is the name of the new view, and the SELECT statement defines the columns and data that will be included in the view. The table_name
and condition
are optional, and they define the table(s) and the condition(s) that the data should be retrieved from.
For example, to create a view named “student_info” that retrieves information about students from a “Students” table, the SQL statement would be:
CREATE VIEW student_info
AS
SELECT StudentID, FirstName, LastName, Email
FROM Students
WHERE Status = 'Active';
This would create a new view named “student_info” that retrieves the columns “StudentID”, “FirstName”, “LastName”, and “Email” from the “Students” table, but only for students whose “Status” is set to “Active”.
Once a view has been created, it can be used like a regular table in SQL queries, and any changes made to the underlying tables will be reflected in the view. However, it’s important to note that views do not store any data themselves, and they are only a virtual representation of the underlying data.