Sub topic 4
Using Views to Simplify and Abstract Queries
This chapter assumes you can write SELECT statements with WHERE, ORDER BY, and GROUP BY, join tables, create tables, insert rows, and understand basic database users and privileges.
A view is a named query stored in a database. You query it with SELECT as if it were a table, but a regular view usually stores only the query definition, not a separate copy of the rows.
Simplifying repeated joins
Suppose an application often needs employee names together with their department names. Without a view, every query must repeat the join:
SELECT e.id,
e.name,
d.name AS department
FROM employees AS e
JOIN departments AS d
ON d.id = e.department_id
WHERE e.status = 'active'
ORDER BY e.name;Create a view when this relationship is part of the application's normal vocabulary:
CREATE VIEW active_employee_directory AS
SELECT e.id,
e.name,
d.name AS department
FROM employees AS e
JOIN departments AS d
ON d.id = e.department_id
WHERE e.status = 'active';Callers can now use the view:
SELECT id, name, department
FROM active_employee_directory
WHERE department = 'Engineering'
ORDER BY name;The view hides the join and the definition of an active employee. A caller can focus on the question, such as “Which active employees work in Engineering?”
Abstracting a stable interface
A view can act as an abstraction, which is a simpler interface that hides implementation details. For example, the database might later store department information in several tables. If the view keeps the same column names, application queries may not need to change.
Choose view columns deliberately. Avoid exposing every column with SELECT *, because adding a column to a base table can unexpectedly change the view's output or break consumers.
CREATE VIEW employee_directory AS
SELECT e.id AS employee_id,
e.name AS employee_name,
d.name AS department_name
FROM employees AS e
JOIN departments AS d
ON d.id = e.department_id;The aliases become the view's output column names. Consumers can use those names consistently:
SELECT employee_id, employee_name
FROM employee_directory
WHERE department_name = 'Engineering';A view can also present calculated values:
CREATE VIEW employee_summary
(employee_id, employee_name, annual_salary) AS
SELECT id,
name,
salary * 12
FROM employees;The column list after the view name explicitly names the result columns. Use it when expressions would otherwise receive unclear or database-specific names.
Choosing a view instead of copying data
A regular view is useful when the source data changes frequently and consumers should see current results. It avoids duplicating the join logic across reports, services, and analysts.
A view does not automatically make a complex query fast. The database commonly expands the view definition into the surrounding query and then optimizes the combined statement. Indexes on the underlying tables still matter, and filters or joins inside a view can affect optimization.
Use a view when the main benefit is reuse, consistency, or a narrower interface. Consider a materialized view when the query is expensive and slightly stale results are acceptable.
Exercise
Exercise
Write a view named engineering_employees that returns employee IDs and names for active employees whose department is Engineering.
Show answer
CREATE VIEW engineeringemployees AS SELECT e.id, e.name FROM employees AS e JOIN departments AS d ON d.id = e.departmentid WHERE e.status = 'active' AND d.name = 'Engineering';