Skip to content

Views

A view is a virtual table based on a SQL query. This chapter introduces how to create and use views.

Create View

sql
-- Basic view
CREATE VIEW active_users AS
SELECT id, name, email
FROM users
WHERE status = 'active';

-- Query view
SELECT * FROM active_users;

View with JOIN

sql
-- View with multiple tables
CREATE VIEW user_orders AS
SELECT 
    u.id,
    u.name,
    o.order_number,
    o.total_amount
FROM users u
JOIN orders o ON u.id = o.user_id;

Replace View

sql
-- Replace existing view
CREATE OR REPLACE VIEW active_users AS
SELECT id, name, email, city
FROM users
WHERE status = 'active';

Drop View

sql
-- Drop view
DROP VIEW IF EXISTS active_users;

View Advantages

  1. Simplify complex queries
  2. Security - hide sensitive columns
  3. Reusability
  4. Data abstraction

View Limitations

  1. Performance overhead
  2. Cannot always update
  3. Depends on base tables

Summary

  • CREATE VIEW: Create view
  • DROP VIEW: Delete view
  • Views simplify queries
  • Use for security and abstraction

Next Step: Learn INDEXES

Content is for learning and research only.