;

SQL VIEW


In this tutorial, we will learn how to use, create and drop VIEW using SQL.

SQL CREATE VIEW

What is the SQL VIEW?

  • A VIEW is a virtual table which consists of rows and column like a table and it does not physically exist.
  • A VIEW is created by a SQL statement that joins one or more tables.
  • If the data changes in the underlying table, then the same changes are also reflected in the VIEW.

Syntax of CREATE VIEW

Syntax - CREATE VIEW
CREATE VIEW viewName AS
SELECT column1,column2,...columnN
FROM tableName
WHERE condition;

Example of CREATE VIEW

Example - CREATE VIEW
CREATE VIEW EmpData_view AS
SELECT EmpName,Salary,City,Country
FROM Employee
WHERE Country='India'

For Selecting data using VIEW , a query will be:

Example - select VIEW using SELECT Statement
SELECT * FROM EmpData_view;

SQL Updating a VIEW

A SQL VIEW can be updated with the CREATE OR REPLACE VIEW statement.

Syntax for CREATE OR REPLACE VIEW

Syntax -  CREATE OR REPLACE VIEW
CREATE OR REPLACE VIEW viewName AS
SELECT column1,column2,...columnN
FROM tableName
WHERE condition;

Example of CREATE OR REPLACE VIEW

Syntax - CREATE OR REPLACE VIEW
CREATE OR REPLACE VIEW EmpData_view AS
SELECT EmpName,Salary,City,Country
FROM Employee
WHERE Country='India' AND Salary > 18000;

SQL DROP VIEW

A SQL VIEW can be deleted with the DROP VIEW statement.

Syntax For SQL DROP VIEW

Syntax - DROP VIEW
DROP VIEW viewName;
Example - DROP VIEW
DROP VIEW EmpData_view;