SQL VIEWS

Introduction

A view is a virtual table. It is used to summarize data from various tables that helps to generate reports. A view always shows updated data.

1. CREATE VIEW Statement

  • SQL VIEW is a virtual table and it contains rows and columns just like real table.
  • The fields of views are selected from one or more real tables in the database.
Syntax:
CREATE VIEW view_name AS
SELECT column_name
FROM table name
WHERE [condition];

Example : Query using CREATE VIEW Statement
Consider the following table titled 'Students'.

Stud_IDStud_NameCityCountry
1MarkLondonEngland
2AlexParisFrance
3BobSydneyAustralia
4JayaDelhiIndia
5SurendraBarodaIndia

Write a query to create a view of columns such as Stud_ID, Stud_Name, City from the above table 'Students'

CREATE VIEW Stud AS
SELECT Stud_ID, Stud_Name
FROM Students
WHERE Country = 'India';

Result is shown in the following table.

Stud_IDStud_Name
4Jaya
5Surendra

Note : SELECT * FROM students; statement is used to see the created view.

2. SQL DROP VIEW Statement

The SQL DROP VIEW Statement is used to delete or remove the view.

Syntax:
DROP VIEW View_name;

Example : Query using DROP VIEW Statement.
If user needs to delete view titled as 'Students'. The query should be written as:

DROP VIEW Students;