ADVERTISEMENT

SQL VIEW

A VIEW is a virtual table based on the result of a SQL query. It doesnโ€™t store data itself, but shows data dynamically from underlying tables โ€” great for simplifying complex queries or restricting access.

๐Ÿ”น Basic Syntax

-- Create a view
CREATE VIEW active_employees AS
SELECT id, name, department
FROM employees
WHERE status = 'active';

-- Query the view like a table
SELECT * FROM active_employees;

-- Update a view (if supported)
CREATE OR REPLACE VIEW active_employees AS
SELECT id, name, department, hire_date
FROM employees
WHERE status = 'active';

๐Ÿ”น Dropping a View

-- Drop the view
DROP VIEW active_employees;

๐Ÿ”น Key Uses

  • Simplify complex queries
  • Abstract logic for reusability
  • Restrict access to specific data
  • Enhance readability in reporting

๐Ÿ›‘ Note: Not all views are updatable. A view can only be updated if itโ€™s based on a single table without group functions, DISTINCT, or joins.

๐Ÿง  Quick Recap

Key PointExplanation
DefinitionVirtual table based on a SELECT query
SyntaxCREATE VIEW view_name AS SELECT...
UsageUsed like a table in queries
BenefitsSimplifies access, improves security
LimitationNot always updatable

๐Ÿ’ก Use views to expose only whatโ€™s needed โ€” perfect for clean, safe, reusable querying.

ADVERTISEMENT