🗄️ Databases

View

View

Using a stored query like a table. A virtual table.

📖 Detailed Explanation

A view is a database object that stores a SQL query and lets you use it like a virtual table. It does not store actual data; it runs the defined query at the time of retrieval and returns the result.

Main advantages of views: simplifying complex queries (defining frequently used joins/aggregations as views), strengthened security (hiding sensitive columns), data abstraction (minimizing impact when table structures change), maintaining consistency (centrally managing business logic).

An Updatable View allows INSERT/UPDATE/DELETE when it meets certain conditions (based on a single table, no aggregation, etc.).

💻 Code Example

-- 기본 뷰 생성
CREATE VIEW active_customers AS
SELECT id, name, email, created_at
FROM customers
WHERE status = 'active' AND deleted_at IS NULL;

-- 뷰 사용 (테이블처럼)
SELECT * FROM active_customers WHERE created_at > '2024-01-01';

-- 복잡한 조인을 뷰로 캡슐화
CREATE VIEW order_details AS
SELECT
    o.id AS order_id,
    c.name AS customer_name,
    p.name AS product_name,
    oi.quantity,
    oi.price,
    o.created_at
FROM orders o
JOIN customers c ON o.customer_id = c.id
JOIN order_items oi ON o.id = oi.order_id
JOIN products p ON oi.product_id = p.id;

-- 보안용 뷰: 민감 정보 제외
CREATE VIEW public_users AS
SELECT id, username, department
FROM users;  -- password, salary 등 제외

-- 업데이트 가능 뷰
CREATE VIEW pending_orders AS
SELECT * FROM orders WHERE status = 'pending';

-- 뷰를 통한 업데이트 (기반 테이블에 반영)
UPDATE pending_orders SET status = 'confirmed' WHERE id = 123;

🗣️ Examples of Practical Conversations

Junior developer: "This 5-table join query has been copied in several places."

Senior developer: "It would be good to make it a view and manage it centrally. When you need to change it, you only have to fix one place."

Junior developer: "Do views affect performance?"

Senior developer: "A view itself just stores a query, so it runs every time. If there's a performance issue, consider a Materialized View."

Interviewer: "Please explain why you use views and their limitations."

Candidate: "Views are used for reusing complex queries, security (hiding columns), and abstracting table structures. As for limitations, running the query every time on retrieval causes performance overhead, and views that include aggregation or joins cannot be UPDATEd."

Interviewer: "Please tell us about your actual experience using them."

Candidate: "I controlled data access by providing different views per API permission, and I defined complex queries for reporting as views and provided them to the analytics team."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

Reviewer: "This view has another subquery inside it, and that references yet another view."

Developer: "I wanted to reuse the existing view."

Reviewer: "When a view references a view, debugging is difficult and performance is hard to predict. Check the execution plan, and if necessary, rewrite it as a single query or switch to a Materialized View."

Developer: "Looking at the execution plan, the nesting makes it inefficient. I'll clean it up."

⚠️ Cautions

🔗 Related Terms

📚 Learn More