Reading history

SQL Views: CREATE VIEW, Uses and Limits

4 min read 0 views

Learn SQL views: CREATE VIEW and DROP VIEW, why views always show current data, using them for reports and security, and their limits.

A view gives a name to a SELECT statement and stores it in the database. Write a long three-table join once, call it student_results, and anyone can then read from it with a short SELECT * FROM student_results. A view keeps no rows of its own: the stored statement runs again whenever the view is read, so what you see always reflects the latest data.

In this lesson you will learn

  • How to create, use and drop a view
  • Why views always show up-to-date data
  • Using views to simplify joins and reports
  • Using views to hide sensitive columns
  • The limits of updating data through views

Creating and Querying a View

CREATE VIEW student_results AS
SELECT s.name, s.dept, c.title AS course, e.marks
FROM enrollments e
JOIN students s ON s.id = e.student_id
JOIN courses c ON c.id = e.course_id;

SELECT * FROM student_results WHERE dept = 'CSE' ORDER BY marks DESC;

Output

+--------+------+-----------------+-------+
| name   | dept | course          | marks |
+--------+------+-----------------+-------+
| Tania  | CSE  | Data Structures | 95    |
| Nusrat | CSE  | Databases       | 92    |
| Tania  | CSE  | Databases       | 90    |
| Nusrat | CSE  | Data Structures | 88    |
| Mitu   | CSE  | Databases       | 76    |
+--------+------+-----------------+-------+
(5 rows)
  • CREATE VIEW name AS SELECT ... saves the query under a name.
  • The three-table join from SQL JOINs now looks like one simple table, and you can add WHERE and ORDER BY to it as usual.

Views Are Always Current

CREATE VIEW dept_summary AS
SELECT dept, COUNT(*) AS students, ROUND(AVG(cgpa), 2) AS avg_cgpa
FROM students GROUP BY dept;

SELECT * FROM dept_summary ORDER BY avg_cgpa DESC;

-- A view always shows current data
UPDATE students SET cgpa = 3.85 WHERE name = 'Imran';
SELECT * FROM dept_summary WHERE dept = 'BBA';

DROP VIEW dept_summary;

Output

+------+----------+----------+
| dept | students | avg_cgpa |
+------+----------+----------+
| CSE  | 4        | 3.81     |
| BBA  | 2        | 3.33     |
| EEE  | 2        | 3.2      |
+------+----------+----------+
(3 rows)

+------+----------+----------+
| dept | students | avg_cgpa |
+------+----------+----------+
| BBA  | 2        | 3.7      |
+------+----------+----------+
(1 row)

After Imran’s CGPA changed, the BBA average in the view changed with it. Nothing had to be refreshed, because the view re-runs its query every time. DROP VIEW removes the view but leaves the underlying tables untouched.

Why Use Views?

BenefitExample
SimplicityHide a complex join behind a short name
ConsistencyEveryone uses the same definition of “active student”
SecurityShow name and dept but hide salary or phone numbers
StabilityReports keep working if the underlying tables are reorganised

Limits of Views

  • A simple view on a single table can often be updated directly in MySQL and PostgreSQL. Views with joins, GROUP BY or aggregates usually cannot. SQLite views are read-only unless you add triggers.
  • A view is not automatically faster than its query, because it runs the same query.
  • For heavy reports, some databases offer materialised views (PostgreSQL, Oracle), which store the result and are refreshed on demand.

Common Mistakes

  1. Expecting a view to store a snapshot. It always shows current data.
  2. Building views on top of views on top of views, which becomes slow and hard to debug.
  3. Using SELECT * in a view definition. Some databases fix the column list at creation time, so later table changes behave unexpectedly.

Practice Exercises

Exercise 1. Create a view top_students showing students with a CGPA of 3.7 or more.

Show answer
CREATE VIEW top_students AS
SELECT name, dept, cgpa FROM students WHERE cgpa >= 3.7;

SELECT * FROM top_students;   -- Nusrat, Tania

Exercise 2. Change the definition of an existing view.

Show answer

Drop and recreate it: DROP VIEW top_students; then CREATE VIEW top_students AS .... MySQL and PostgreSQL also support CREATE OR REPLACE VIEW.

Frequently Asked Questions

Does a view take up storage space?

Only for its definition. The data stays in the original tables. Materialised views are the exception, because they store results.

Can I create an index on a view?

Not on a normal view. Index the underlying tables instead. SQL Server’s indexed views and PostgreSQL’s materialised views can be indexed.

Summary

  • A view is a named, saved query used like a table.
  • It stores no data and always shows current results.
  • Views simplify joins, standardise reports and hide sensitive columns.
  • Most views with joins or aggregates are read-only.

Next: SQL NULL Values: IS NULL, COALESCE and NULLIF.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *