SQL View – A Complete Guide

FREE Online Courses: Click for Success, Learn for Free - Start Now!

SQL is a popular database query language. It has applications in various use cases of software designing. When we query the available data, we also come across data that is not required by us or is of little or no use.

SQL views come in handy here. By using SQL views, we can easily modify and access the data we need.

The view creates a virtual table on which we can execute all types of queries and transaction statements.

Let us learn more about SQL View.

What are SQL Views?

SQL views are virtual tables created by using a CREATE VIEW statement. Views do not take up extra memory and are not stored but can be used by their names and can be further queried using SQL statements.

By using SQL views, we can create a virtual table consisting of the columns we require. We can even use conditionals and WHERE clause while creating the view to get the data that follows a certain specified constraint.

The basic syntax of CREATE view is as follows:

Syntax:

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

Why do we need SQL Views?

In today’s world, data is being created at very high rates, and it is a big challenge to deal with these massive amounts of data.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

When a data scientist deals with data, many filters and tweaks in data are needed to be done to reach correct conclusions.

The freedom to view data in small pieces that satisfy the provided conditional without wasting extra memory proves to be a boon.

SQL views help us to assess and understand data, while the efficiency of a system is taken care of by the virtual property of the view.

SQL Demo Database and Examples

Let us first have a look at our database DataFlair_Employee :
Query:

SELECT * FROM DataFlair_Employee ;

 

View SQL database

How to create a View in SQL?

A view is created by using the CREATE VIEW statement.

Syntax:

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

Example 1: Let us start by creating a view where all the employees of DataFlair with age more than or equal to 21 years.
Query:

CREATE VIEW DataFlair_21yo AS 
SELECT *
FROM dataflair_employee
WHERE age >= 21;

 

Create view in SQL

When we query the created view using the SELECT statement, we can see the created view and the data, where the condition we provided on age is satisfied and followed.

How to Query a View in SQL?

We can run queries on a view just like a SQL table using the SELECT statement.
Syntax:

SELECT column1,column2, column3, ….. 
FROM viewName
WHERE condition ;

Example 1: Let us try to find the details of employees with a salary more than Rs 30,000 and age above 25 years by querying our created view.
Query:

SELECT * FROM dataFlair_21yo WHERE salary > 30000 AND age > 25 ;

Select view in SQL

In the result, we can see the details of the employees which satisfy our given condition.

How to perform operations on a View in SQL?

Syntax:

SELECT column1,column2, column3, ….. 
FROM viewName
WHERE condition ;

How to update a View in SQL?

We use the CREATE OR REPLACE VIEW statements to update or modify the existing view.

Syntax:

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

Example 1: Let us only store the name and salary of an employee in the view we created.
Query:

CREATE OR REPLACE VIEW dataFlair_21yo AS
SELECT name_emp, salary
FROM dataflair_employee
WHERE age >= 21 ;

Create view in SQL

We can see that our view is modified and now contains only two columns: name and age of the employees at DataFlair.

How to Drop a View in SQL?

When we drop a view it is permanently deleted and can’t be regained.
Syntax: 

DROP VIEW view_name ;

Example 1: Let us drop the view we created.
Query:

DROP VIEW dataflair_21yo ;

Drop view

We can clearly see our view is dropped, and now we can no longer access it. Let us try by running a SELECT query on the view we dropped.

Query:

SELECT * FROM DataFlair_21yo ;

Drop view in SQL

We can see the error code above, which means our view was dropped successfully.

Managing Views in SQL

While creating views we need to manage them as well. Some of these operations are:

  • Creating a view
  • Updating a view
  • Viewing the data in the view
  • Renaming a view
  • Dropping the view

Advantages of SQL View

Views are used for three major reasons:

1. Security

A database contains large amounts of data, some of which are sensitive and need to be kept safe.

When we use Views, we can decide what data is exposed and what data is kept hidden.

2. Simplicity

A database contains thousands of tables, using views, we can create simplified virtual tables of only the data we require.

This helps in reducing the query time and simplifies the storage system as well.

3. Consistency

While querying data, we often write very complex formulas and queries to get the data we require.

When we use views, we can hide this extra detailing and create consistency in the data.

Summary

We have seen what SQL views are and why do we need SQL Views. The functionality of views is the same as the tables in a database. Views are virtual tables that do not use extra memory.

We need to keep in mind that views store updated data. With the help of Views, we can easily and efficiently study our data and query for the required output data.

We have seen how to create, query, and modify an existing view with various conditions and arithmetic operations.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

1 Response

  1. Nirajah jegath says:

    Thank you so much for this article. It has helped me a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *