Syntax
CREATE VIEW view_name AS
SELECT columns FROM table WHERE condition;
What Is CREATE VIEW?
Imagine you have a big toy box filled with all sorts of toys: cars, blocks, dolls, puzzles. Every time you want to play with just the red cars, you have to dig through the whole box.
So someone gives you a magical window. This window doesn’t hold toys — but when you look through it, it only shows the red cars from the toy box. It saves you time and effort.
That’s exactly what a VIEW
is in SQL. It doesn’t store any new data. It just shows a filtered or simplified version of the original table.
Real SQL Example
Let’s say you have a table called toys
and you want to create a view that only shows red cars:
CREATE VIEW red_cars AS
SELECT * FROM toys
WHERE type = 'car' AND color = 'red';
Now, anytime you write:
SELECT * FROM red_cars;
It will show you only the red cars — even though the actual data is still stored in the toys
table.
Why Use a View?
- To simplify complex queries
- To display only specific data to users
- To reuse the same query logic without rewriting it
Summary
- A view is like a custom lens into your table.
CREATE VIEW
creates a virtual table from a query.- The view doesn’t hold data — it just shows data from real tables.