Wednesday, December 18, 2024

SQL CREATE VIEW Keyword

 The CREATE VIEW keyword in SQL is used to create a view, which is essentially a stored query or a virtual table that is based on the result set of a SELECT statement. Views do not store data themselves but provide a way to encapsulate complex queries, allowing you to query them as though they were actual tables.

Syntax:

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • view_name: The name you want to assign to the view.
  • SELECT statement: This is the query that defines the view, specifying which data it will return when queried.

Example:

CREATE VIEW EmployeeView AS
SELECT employee_id, first_name, last_name, department
FROM employees
WHERE status = 'Active';

This creates a view called EmployeeView that shows the employee ID, first name, last name, and department for all employees whose status is 'Active'. You can now query EmployeeView just like a table:

SELECT * FROM EmployeeView;

Key Points:

  • A view is a virtual table that is based on a SELECT query.
  • Views can simplify complex queries, enhance security, and allow for easier data manipulation.
  • Views can be updated or deleted using ALTER VIEW or DROP VIEW.

No comments:

Post a Comment