Get Started: SQL Views

In This Article, You Will Know About SQL Working With Dates.

Before moving ahead, let’s know a bit about the SQL CREATE INDEX Statement.

Table of Contents

SQL VIEW

SQL CREATE VIEW Statement

In SQL, View is like a real table but it is a virtual table.

It is as same as a real table and stores query in row & column format taken from the database.

A SQL View is created with CREATE VIEW statement.

CREATE VIEW

Syntax

				
					CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
				
			

Table1: Student_Info

Serial Number

Name

Class

Roll_No

Subject

1

A

I

111

Eco

2

B

II

121

Math

3

C

III

131

Sci

4

D

IV

141

Eng

SQL CREATE VIEW Examples

Example: Use the SQL View to create a virtual cum real table.

Select all classes having Roll_No < 130.

				
					CREATE VIEW Student_data AS
SELECT Class FROM Student_Info
WHERE Roll_No < 130;
				
			

Example: Use the SQL View to create a virtual cum real table.

Select records having Roll_No < 130.

				
					CREATE VIEW Student_data AS
SELECT * FROM Student_Info
WHERE Roll_No < 130;
				
			

Example: Use the SQL View to create a virtual cum real table.

Select records that are in Class II with Roll_No = 121.

				
					CREATE VIEW Student_Data AS
SELECT * FROM Student_Info
WHERE Class = 'II' AND Roll_No = 121;
				
			

Example: Use the SQL View to create a virtual cum real table.

Select records that are in Class II with Roll_No = 121.

				
					CREATE VIEW Student_Data AS
SELECT Class, Roll_No
FROM Student_Info
WHERE Class = 'II' AND Roll_No = 121;

				
			

Example: Use the SQL View to create a virtual cum real table.

Select all records that are Serial Number > 2 with Roll_No = 130.

				
					CREATE VIEW Student_Data AS
SELECT * FROM Student_Info
WHERE Serial Number > 2 AND Roll_No = 130;
				
			

Example: Use the SQL View to create a virtual cum real table.

Select all records that are Serial Number < 3 with Roll_No = 121 OR > 121.

				
					CREATE VIEW Student_Data AS
SELECT * FROM Student_Info
WHERE Serial Number < 3 AND (Roll_No = 121 OR Roll_No > 121);
				
			

Example: Use the SQL View to create a virtual cum real table.

Select records that are Serial Number < 3 with Roll_No = 121 OR > 121.

				
					CREATE VIEW Student_Data AS
SELECT Serial Number, Roll_No 
FROM Student_Info
WHERE Serial Number < 3 AND (Roll_No = 121 OR Roll_No > 121);
				
			

SQL Updating a View

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

SQL CREATE OR REPLACE VIEW

Syntax

				
					CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
				
			

Example: Use the SQL View to create a virtual cum real table.

Select records having Roll_No < 130.

				
					CREATE OR REPLACE VIEW Student_data AS
SELECT * FROM Student_Info
WHERE Roll_No < 130;
				
			

SQL Dropping a View

A view is deleted with the DROP VIEW statement.

SQL DROP VIEW

Syntax

				
					DROP VIEW name;
				
			

Example: Use the SQL DROP VIEW to create a virtual cum real table.

				
					Delete the VIEW Student_Info
DROP VIEW [Student_Info];

				
			

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

1 thought on “Get Started: SQL Views”

Leave a Comment