VIEW)In the previous lesson, we talked about temporary tables, which help store intermediate results during a session. Now let’s look at another important SQL tool: views. They also help simplify work with complex queries, but they do so in a different way.
A view allows you to save a SELECT query under a separate name and then reuse it later. This is especially useful in reporting, analytics, and scenarios where the same result set needs to be read many times.
A view (VIEW) is a database object that stores not the data itself, but the SQL query used to retrieve it.
In simpler terms, a view can be thought of as a “virtual table”:
SELECT;When you query a view, the DBMS usually executes the stored query and returns the current result based on the underlying tables.
A view is created with CREATE VIEW:
CREATE VIEW view_name AS
SELECT column1, column2, column3
FROM table_name
WHERE condition;
After that, you can query the view almost like a table:
SELECT *
FROM view_name;
It is important to understand that a regular view stores the query logic, not a separate copy of the result.
Suppose we often want to get a list of customers together with the total amount of their payments. Instead of writing the same query every time, we can create a view:
CREATE VIEW customer_payment_summary AS
SELECT c.customer_id,
c.first_name,
c.last_name,
SUM(p.amount) AS total_amount,
COUNT(p.payment_id) AS payment_count
FROM customer c
JOIN payment p ON c.customer_id = p.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name;
Now this logic is much easier to use:
SELECT customer_id, first_name, last_name, total_amount
FROM customer_payment_summary
ORDER BY total_amount DESC;
SELECT AVG(total_amount) AS avg_customer_revenue
FROM customer_payment_summary;
Result: the complex aggregation is defined once inside the view, and after that you can work with it like with a regular dataset in several separate queries.
Although a view often looks like a table, there are important differences between them.
INSERT, UPDATE, or DELETE.VIEW is.Views are worth using if:
JOINs, filters, and aggregations behind a simpler name;For example, you can create a view only for expensive films:
CREATE VIEW expensive_films AS
SELECT film_id, title, rental_rate, rating
FROM film
WHERE rental_rate >= 4.00;
SELECT title, rental_rate
FROM expensive_films
ORDER BY rental_rate DESC, title;
Result: the main filtering logic is saved in one place, so in later queries you do not need to repeat the condition WHERE rental_rate >= 4.00 every time.
Views and temporary tables can solve similar tasks, but there are important differences between them.
If you need an intermediate result that must exist separately and may be processed further, a temporary table is often the better choice. If you simply need to define a convenient representation over existing data once, VIEW is usually a better fit.
In many DBMSs, simple views can be used not only for reading but also for modifying data. For example, this may be possible if the view is based on a single table and does not contain complex aggregates, GROUP BY, DISTINCT, or joins between several tables.
For example, a simple view may look like this:
CREATE VIEW active_customers_basic AS
SELECT customer_id, first_name, last_name, active
FROM customer
WHERE active = 1;
In some DBMSs, you can run UPDATE through such a view. But you should not rely on this as a universal rule: the more complex the view logic is, the less likely it is to be updatable.
In practice, views are more often used for reading and simplifying queries.
When working with views, it is useful to keep several rules in mind:
INSERT, UPDATE, or DELETE;SELECT, a CTE, or a temporary table would be simpler in a particular task;CREATE OR REPLACE VIEW or view updatability rules.A well-designed view makes SQL shorter, clearer, and easier to reuse.
Imagine that analysts regularly need a list of films together with their category names. Instead of writing the same JOINs every time, you can create a view:
CREATE VIEW film_category_details AS
SELECT f.film_id,
f.title,
f.rental_rate,
c.name AS category_name
FROM film f
JOIN film_category fc ON f.film_id = fc.film_id
JOIN category c ON fc.category_id = c.category_id;
After that, any query becomes simpler:
SELECT title, category_name, rental_rate
FROM film_category_details
WHERE category_name = 'Comedy'
ORDER BY rental_rate DESC, title;
SELECT category_name, COUNT(*) AS film_count
FROM film_category_details
GROUP BY category_name
ORDER BY film_count DESC;
This approach is convenient because the complex table relationship is defined once. After that, analysts, reports, and applications can use a ready-made logical layer without constantly repeating the same JOIN logic.
Key takeaways from this lesson:
VIEW) stores an SQL query rather than a separate copy of the data.JOINs and filters.In the next lesson, we will look at materialized views and understand how they differ from regular views.