View
View
A virtual table. Uses query results like a table. Data abstraction.
View
A virtual table. Uses query results like a table. Data abstraction.
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.
-- ๊ธฐ๋ณธ ๋ทฐ ์์ฑ: ํ์ฑ ์ฌ์ฉ์๋ง ์กฐํ
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;
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."