VIEWS

A VIEW in SQL is a virtual table based on the result of a SELECT query. It does not store data physically but presents data dynamically when queried. Views are useful for simplifying complex queries, hiding sensitive columns, and improving reusability.


Syntax

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

To query a view:

SELECT * FROM view_name;

To remove a view:

DROP VIEW view_name;

Example: Create a View for IT Employees

CREATE VIEW it_employees AS
SELECT id, name, age, gender
FROM employees
WHERE department = 'IT';

-- Now query the view
SELECT * FROM it_employees;

This view returns only the employees who belong to the IT department.


Key Points

  • Views do not store data; they reflect data from the underlying table(s)
  • Views can be used like regular tables in most queries
  • They help simplify access to commonly used or complex queries
  • Updates through views are limited and depend on the database system and view structure