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.