
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL CREATE VIEW Statement
SQL CREATE VIEW Statement
The CREATE VIEW statement in SQL is used to create a virtual table based on the result of a SELECT query. A view does not store data physically but acts like a table that can be queried like any regular table.
- A view is a named SQL query stored in the database.
- Views can include joins, filtering conditions, grouping, and aggregation.
- You can select data from a view just like a table using SELECT statement.
- Views do not store data, they display data from the underlying base tables.
Syntax
Following is the basic syntax of the CREATE VIEW statement in SQL:
CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
Example: Create a Simple View
Assume we have created a table named CUSTOMERS using the CREATE TABLE statement using the following query:
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now, let us insert few records into this table using the INSERT statement as follows:
INSERT INTO CUSTOMERS VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ), (2, 'Khilan', 25, 'Delhi', 1500.00 ), (3, 'Kaushik', 23, 'Kota', 2000.00 ), (4, 'Chaitali', 25, 'Mumbai', 6500.00 ), (5, 'Hardik', 27, 'Bhopal', 8500.00 ), (6, 'Komal', 22, 'Hyderabad', 4500.00 ), (7, 'Muffy', 24, 'Indore', 10000.00 );
The table will be created as follows:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Now, we create a view that displays only the ID, NAME, and SALARY of customers from the CUSTOMERS table:
CREATE VIEW CUSTOMER_SALARY_VIEW AS SELECT ID, NAME, SALARY FROM CUSTOMERS;
We get the following output:
Query OK, 0 rows affected (0.05 sec)
Verification
You can retrieve data from the view using the following SELECT query:
SELECT * FROM CUSTOMER_SALARY_VIEW;
Following is the table produced:
ID | NAME | SALARY |
---|---|---|
1 | Ramesh | 2000.00 |
2 | Khilan | 1500.00 |
3 | Kaushik | 2000.00 |
4 | Chaitali | 6500.00 |
5 | Hardik | 8500.00 |
6 | Komal | 4500.00 |
7 | Muffy | 10000.00 |
SQL View with WHERE Clause
You can also create SQL views that include a WHERE clause to filter the data shown through the view. This allows you to present only a subset of rows from the base table(s), based on specific conditions.
Syntax
Following is the basic syntax to create views using the WHERE clause in SQL:
CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
Example
In this example, we create a view that only shows customers with a salary greater than 2000:
CREATE VIEW HIGH_EARNERS AS SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000;
We get the following output:
Query OK, 0 rows affected (0.05 sec)
Verification
You can use the following SELECT query to check the view:
SELECT * FROM HIGH_EARNERS;
The table produced is as shown below:
ID | NAME | SALARY |
---|---|---|
4 | Chaitali | 6500.00 |
5 | Hardik | 8500.00 |
6 | Komal | 4500.00 |
7 | Muffy | 10000.00 |
Using Joins in Views
You can create views using JOIN operations to combine and present data from multiple related tables as a single virtual table.
The view can include selected columns from each table and can even apply filters using a WHERE clause.
Example
Assume you have another table named ORDERS having the following structure:
ORDER_ID | CUSTOMER_ID | AMOUNT | ORDER_DATE |
---|---|---|---|
101 | 1 | 250 | 2025-07-01 |
102 | 2 | 300 | 2025-07-03 |
103 | 1 | 150 | 2025-07-05 |
Now, we have two tables: ORDERS and CUSTOMERS. We can create a view to show customer names along with their order IDs:
CREATE VIEW CUSTOMER_ORDERS_VIEW AS SELECT C.ID, C.NAME, O.ORDER_ID FROM CUSTOMERS C JOIN ORDERS O ON C.ID = O.CUSTOMER_ID;
We get the following output:
Query OK, 0 rows affected (0.05 sec)
Verification
We can view the CUSTOMER_ORDERS_VIEW table using the following SELECT query:
SELECT * FROM CUSTOMER_ORDERS_VIEW;
Following is the table produced:
ID | NAME | ORDER_ID |
---|---|---|
1 | Ramesh | 101 |
2 | Khilan | 102 |
1 | Ramesh | 103 |
Modifying Views in SQL
You can change the definition of an existing view using the CREATE OR REPLACE VIEW statement. This is useful when you want to update the columns or logic in the view without dropping and recreating it manually.
Syntax
Following is the basic syntax to modify a view in SQL:
CREATE OR REPLACE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
Example
Let us modify the existing view CUSTOMER_SALARY_VIEW to include only specific columns from the CUSTOMERS table:
CREATE OR REPLACE VIEW CUSTOMER_SALARY_VIEW AS SELECT ID, NAME, AGE, SALARY FROM CUSTOMERS;
Following is the output obtained:
Query OK, 0 rows affected (0.04 sec)
Verification
To verify that the view has been updated, you can query it like a regular table:
SELECT * FROM CUSTOMER_SALARY_VIEW;
Following is the table obtained:
ID | NAME | AGE | SALARY |
---|---|---|---|
1 | Ramesh | 32 | 2000.00 |
2 | Khilan | 25 | 1500.00 |
3 | Kaushik | 23 | 2000.00 |
4 | Chaitali | 25 | 6500.00 |
5 | Hardik | 27 | 8500.00 |
6 | Komal | 22 | 4500.00 |
7 | Muffy | 24 | 10000.00 |
Dropping a View in SQL
If a view is no longer needed, you can remove it using the DROP VIEW statement.
Syntax
Following is the basic syntax to drop a view in SQL:
DROP VIEW view_name;
Example
In the example below, we drop a view named CUSTOMER_SALARY_VIEW:
DROP VIEW CUSTOMER_SALARY_VIEW;
Following is the output obtained:
Query OK, 0 rows affected (0.04 sec)
Verification
After dropping the view, trying to query it will result in an error:
SELECT * FROM CUSTOMER_SALARY_VIEW;
It will show the following error:
ERROR 1146 (42S02): Table 'testdb.customer_salary_view' doesn't exist
Important Points About CREATE VIEW
Following are the important points to remember when creating views in SQL:
- Views do not store data, they display data from underlying tables in real-time.
- You can query, filter, and join views just like normal tables.
- Use CREATE OR REPLACE VIEW to update a view.
- To remove a view, use DROP VIEW.
- Views are supported in most relational database systems including MySQL, PostgreSQL, Oracle, and SQL Server.