๐Ÿ—„๏ธ Databases

View

View

A virtual table. Uses query results like a table. Data abstraction.

๐Ÿ“– Detailed Explanation

A view (View) is a virtual table that stores a SELECT query retrieving data from one or more tables and lets it be used as if it were a table. It does not store actual data, but fetches data from the underlying tables at the time of the query.

The main uses of a view include simplifying complex queries (turning frequently used joins into a view for reuse), security (exposing only specific columns), data abstraction (when the underlying table structure changes, only the view needs to be modified), and encapsulating business logic.

A regular view runs its query every time it is queried, but a Materialized View physically stores the results to improve performance. Data can also be inserted or modified through an Updatable View.

๐Ÿ’ป Code Example

-- ๊ธฐ๋ณธ ๋ทฐ ์ƒ์„ฑ: ํ™œ์„ฑ ์‚ฌ์šฉ์ž๋งŒ ์กฐํšŒ
CREATE VIEW active_users AS
SELECT id, name, email, created_at
FROM users
WHERE status = 'active' AND deleted_at IS NULL;

-- ๋ทฐ ์‚ฌ์šฉ (ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ)
SELECT * FROM active_users WHERE created_at > '2024-01-01';

-- ๋ณต์žกํ•œ ์กฐ์ธ์„ ๋ทฐ๋กœ ๋‹จ์ˆœํ™”
CREATE VIEW order_summary AS
SELECT
    o.id AS order_id,
    c.name AS customer_name,
    SUM(oi.quantity * oi.price) AS total,
    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
GROUP BY o.id, c.name, o.created_at;

-- ๋ณด์•ˆ์šฉ ๋ทฐ: ๋ฏผ๊ฐ ์ •๋ณด ์ˆจ๊น€
CREATE VIEW public_employees AS
SELECT id, name, department, hire_date
FROM employees;  -- salary, ssn ๋“ฑ์€ ์ œ์™ธ

-- ์—…๋ฐ์ดํŠธ ๊ฐ€๋Šฅ ๋ทฐ (๋‹จ์ผ ํ…Œ์ด๋ธ” ๊ธฐ๋ฐ˜)
CREATE VIEW pending_orders AS
SELECT * FROM orders WHERE status = 'pending';

UPDATE pending_orders SET status = 'processing' WHERE id = 123;

๐Ÿ—ฃ๏ธ Examples of Practical Conversations

Junior developer: "This join query is used redundantly in several APIs. What should we do about it?"

Senior developer: "You can just make it a view. Once you define it, you can use it like a table, so you eliminate the duplication and only need to make changes in one place."

Junior developer: "Won't there be a performance problem?"

Senior developer: "A regular view runs the query every time, so it's the same. If performance is important, you can consider a Materialized View."

Interviewer: "Please explain the advantages and disadvantages of views and their use cases."

Candidate: "The advantages are reuse of complex queries, security (hiding columns), and minimal impact when the table structure changes. The disadvantages are performance overhead and update restrictions. I used them for reporting views for BI dashboards and for separating permissions per API."

Interviewer: "What is the difference between a view and a Materialized View?"

Candidate: "A regular view stores only the definition and runs it at query time, but a Materialized View physically stores the results and retrieves them quickly. In exchange, it needs to be refreshed."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "There are a lot of cases where a WHERE condition is added after querying this view."

Developer: "Yes, I'm adding conditions on top of the base view when I use it."

Reviewer: "If the condition can be put inside the view, the optimizer can execute it more efficiently. It's better to include frequently used filters in the view definition."

Developer: "Understood. I'll split the view into more specific ones."

โš ๏ธ Cautions

๐Ÿ”— Related Terms

๐Ÿ“š Learn More