SQL VIEW

Article Summary

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 πŸ”Ή Dropping a View πŸ”Ή Key Uses πŸ›‘ Note: Not all views are updatable. A view can […]

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.

Was this helpful?