Sub topic 6
Limiting What Users Can See with SQL Views
Assume you already know SELECT, JOIN, WHERE, table creation, and basic database privileges. A view can expose a controlled shape of data while hiding columns and rows that a user does not need.
Exposing selected columns and rows
Suppose the employees table contains private information:
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
);An analyst may need employee names and departments, but not salaries or email addresses. Create a view that selects only the approved columns:
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
WHERE e.status = 'active';The analyst can query the view as if it were a table:
SELECT id, name, department
FROM employee_directory
WHERE department = 'Engineering'
ORDER BY name;The view hides inactive employees and does not expose email or salary. It also gives users a stable name for a business query. If the join condition changes later, applications can continue using employee_directory.
A view filters data only for users who are required to use it. If a user can query employees directly, the user can bypass the view.
Granting access to the view
Privileges determine which database objects a user or role can access. A role is a named collection of privileges that can be assigned to users.
REVOKE ALL ON employees FROM analyst;
REVOKE ALL ON departments FROM analyst;
GRANT SELECT ON employee_directory TO analyst;The exact privilege syntax varies between database systems. Some systems also require the view owner or security configuration to allow the view to read its underlying tables on behalf of the caller. Test the permissions using a session connected as the intended role.
| User access | Can read `employee_directory`? | Can read `employees` directly? |
|---|---|---|
| `analyst` has `SELECT` on the view only | Yes | No |
| `analyst` has `SELECT` on both objects | Yes | Yes |
| `analyst` has no privilege on either object | No | No |
Understanding the security boundary
A view can enforce two common policies:
- Column restriction: return only approved columns.
- Row restriction: return only approved rows, such as active employees or records from one department.
The following view gives a department manager access only to employees in Engineering:
CREATE VIEW engineering_employees AS
SELECT id, name, email, status
FROM employees
WHERE department_id = 10
AND status = 'active';
GRANT SELECT ON engineering_employees TO engineering_manager;This design works when the department is fixed. If many managers each need a different department, creating one view per department may become difficult to maintain. Database-specific row-level security may be a better choice because it can apply a policy based on the logged-in user.
Common pitfalls
A view can expose sensitive data indirectly. For example, returning salary is obvious, but counts, totals, or carefully chosen filters may also reveal private information. Review expressions and aggregate results before granting access.
Changing or dropping a view can break reports and applications. Inspect dependencies before changing its definition, and use migration scripts so changes are reviewed and repeatable.
Exercise
An analyst has SELECT permission on employee_directory, but also has SELECT permission on employees. Can the view prevent the analyst from reading salary?
Show answer
No. The analyst can bypass the view and query employees directly. Revoke the direct table privilege or use a separate database security policy.