Sub topic 2
Creating and Querying Your First View
Assume you know SELECT, WHERE, JOIN, CREATE TABLE, and database privileges. This chapter uses standard SQL syntax. Small details, such as whether CREATE OR REPLACE VIEW is supported, vary between database systems.
What a view does
A view is a named query stored in the database. You query a view with SELECT as if it were a table, but a regular view usually does not store its result rows. The database runs the view's query when you use it.
A view can hide a repeated join, filter rows, or expose only the columns that callers need. The underlying tables remain the source of the data.
Create the base tables
These tables contain departments and employees. An employee belongs to a department through department_id.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255),
salary NUMERIC(10, 2) NOT NULL,
status VARCHAR(20) NOT NULL,
department_id INTEGER REFERENCES departments (id)
);
INSERT INTO departments (id, name)
VALUES
(1, 'Engineering'),
(2, 'Support');
INSERT INTO employees
(id, name, email, salary, status, department_id)
VALUES
(101, 'Ana', '[email protected]', 7200.00, 'active', 1),
(102, 'Ben', '[email protected]', 6100.00, 'inactive', 2);Define and query a view
The following view combines each employee with the employee's department. The AS keyword separates the view name from its defining query.
CREATE VIEW 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;The view has three output columns: id, name, and department. The expression d.name AS department gives the department column a clear name. Without the alias, both tables contribute a column named name, which can make the result confusing or ambiguous.
Now query the view:
SELECT id, name, department
FROM employee_directory
WHERE department = 'Engineering';The result is equivalent to running the underlying join with the same filter:
SELECT e.id, e.name, d.name AS department
FROM employees AS e
JOIN departments AS d
ON d.id = e.department_id
WHERE d.name = 'Engineering';The view does not change the meaning of the query. It gives the join a reusable name.
Check the definition
Most database systems provide a system catalog containing metadata about database objects. For example, this query lists views in PostgreSQL's default public schema:
SELECT table_name
FROM information_schema.views
WHERE table_schema = 'public';The catalog can confirm that a view exists. To inspect its complete SQL definition, use the database's specific inspection command or catalog columns. That syntax differs between PostgreSQL, MySQL, Oracle, SQL Server, and other systems.
A view depends on the tables and columns referenced by its defining query. Renaming or removing those objects can make the view invalid, or can prevent later changes, depending on the database system.
Exercise
Write a query against employee_directory that returns only active employees and includes their names and departments.
Show answer
SELECT name, department FROM employeedirectory JOIN employees ON employees.id = employeedirectory.id WHERE employees.status = 'active';