×

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database.

The SQL view is similar to the SQL tables. In SQL Tables, we store entire data in rows and columns. In the same way, SQL View is also used to store records in rows and columns, but only those data which the user wants, not unnecessary data.

SQL VIEW is a virtual table. SQL View allows accessing only specific columns records rather than the entire table data.

We can easily create a view by selecting one or more tables using CREATE VIEW keyword. We can also update and delete the view.

In this entire article, we will understand the concept of view about how to create a view, delete a view, and update a view.

1 CREATE SQL VIEW: -

In SQL, we can easily create a view by using CREATE VIEW keyword. We can create a View for the single table and multiples table.

The syntax for CREATE VIEW (Single Table)

CREATE VIEW VIEW_NAME AS SELECT COLUMN_NAME1, COLUMN_NAME2 FROM TABLE_NAME WHERE CONDITION;    

The above syntax is for creating a view from a single table. In the above syntax, VIEW_NAME is the name of the view to create a view in SQL. Table_name is the table's name from where we will select specific records, WHERE clause is optional in the SQL query.

The syntax for CREATE VIEW (Multiple Table)

CREATE VIEW VIEW_NAME AS SELECT T1.COLUMN_NAME, T1.COLUMN_NAME2, T2.COLUMN_NAME1, T2.COLUMN_NAME2 FROM T1, T2 WHERE CONDITION;

The above syntax is for creating a view from multiple tables. In the above syntax, VIEW_NAME is the name of the view to create a view in SQL. T1, T2 is the name of tables from where we will select specific records, WHERE clause is optional in the SQL query.

Consider the following tables along with the given records.

Table 1: Emp

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65000PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAORACLE1
1003NIKHILVANI50000JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAITESTING4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWHERE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4

Table 2: Manager.

Manageridmanager_namemanager_department
1Snehdeep KaurORACLE
2Kirti KirtaneFMW
3Abhishek ManishJAVA
4Anupam MishraTESTING

Examples to CREATE VIEW from a single table.

Example 1: Write a query to create a view with Employee Id, First name, Last name, Salary from Emp table.

CREATE VIEW EMPLOYEE_VIEW AS SELECT EMPLOYEEID, CONCAT (FIRST_NAME, CONCAT (“ ”, LAST_NAME)) AS NAME, SALARY FROM EMP;

In the above query, we created a view name EMPLOYEE_VIEW with Employee Id, concatenate first name and last name as Name, Salary from Emp Table.

Whenever we want to look at the records in the table, we use the SELECT * FROM query; in the same manner we will do for view, instead of the table name, we will use view name.

SELECT * FROM EMPLOYEE_VIEW;
SQL View

Example 2: Write a query to create a view with Manager Id, Manager Name, and Department from Manager Table.

CREATE VIEW MANAGER_VIEW AS SELECT MANAGERID, MANAGER_NAME, MANAGER_DEPARTMENT FROM MANAGER;

In the above query, we created a view name MANAGER_VIEW with Manager Id, Manager Name, and Department from Manager Table.

Whenever we want to look at the records in the table, we use the SELECT * FROM query. We will use the view name. Similarly, we will do for view instead of the table name.

SELECT * FROM MANAGER_VIEW;
SQL View

Example 3: Write a query to create a view with Employee Id, First name, Last name, Salary, City of those employees whose salary is greater than 54000 and City include Pune and Mumbai from Emp table.

CREATE VIEW EMPLOYEE_VIEW1 AS SELECT EMPLOYEEID, CONCAT (FIRST_NAME, CONCAT (“ ”, LAST_NAME)) AS NAME, SALARY, CITY FROM EMP WHERE SALARY > 54000 AND  CITY IN (‘PUNE’, ‘MUMBAI’);

In the above query, we created a view name EMPLOYEE_VIEW1 with Employee Id, concatenate first name and last name as Name, Salary, City of those employees whose salary is greater than 54000 and we used AND operator with City include Pune and Mumbai from Emp Table.

Whenever we want to look at the records in the table, we use the SELECT * FROM query. We will use the view name. Similarly, we will do for view instead of the table name.

SELECT * FROM EMPLOYEE_VIEW1;
SQL View

Example 4: Write a query to create a view with Manager Id, Manager Name, and Department of those managers whose department is 'Oracle' and 'Java' from Manager Table.

CREATE VIEW MANAGER_VIEW1 AS SELECT MANAGERID, MANAGER_NAME, MANAGER_DEPARTMENT FROM MANAGER; WHERE MANAGER_DEPARTMENT IN (‘ORACLE’, ‘JAVA’);

In the above query, we created a view name MANAGER_VIEW with Manager Id, Manager Name, and Department of those managers whose department is ‘Oracle’, and ‘Java’ from Manager Table.

Whenever we want to look at the records in the table, we use the SELECT * FROM query. We will use the view name. Similarly, we will do for view instead of the table name.

SELECT * FROM MANAGER_VIEW1;
SQL View

All the above examples of CREATE VIEW are from single tables. The next example of CREATE VIEW is from multiple tables.

Consider the following tables along with the given records.

Table 1: Emp

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65000PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAORACLE1
1003NIKHILVANI50000JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAITESTING4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWHERE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4

Table 2: Manager.

Manageridmanager_namemanager_department
1Snehdeep KaurORACLE
2Kirti KirtaneFMW
3Abhishek ManishJAVA
4Anupam MishraTESTING

Example 1: Write a query to create View with Employee id, Name, City, Manager Id, and Manager Name from Emp Table and Manager Table.

CREATE VIEW EMP_MAN_VIEW AS SELECT EMP.EMPLOYEEID, CONCAT(EMP.FIRST_NAME, CONCAT(“ ”, EMP.LAST_NAME)) AS NAME, CITY, MANAGER.MANAGERID, MANAGER.MANAGER_NAME FROM EMP, MANAGER WHERE EMP.MANAGERID = MANAGER.MANAGERID;   

In the above query, we created a view with Employee id, Name, City, Manager Id, and Manager Name from Emp Table and Manager Table.

Whenever we want to look at the records in the table, we use the SELECT * FROM query. We will use the view name. Similarly, we will do for view instead of the table name.

SELECT * FROM EMP_MAN_VIEW;
SQL View

2 UPDATE SQL VIEW: -

The update statement modifies the existing table and existing view in the SQL. We can also insert new data into the existing view in the SQL. In SQL, a view is modified only when the below-given conditions are met. If one of the given conditions is not met, we will not be able to modify the view.

  • DISTINCT keyword shouldn’t be used in the SELECT statement.
  • The view should not have all NOT NULL values.
  • Shouldn’t use ORDER BY clause and GROUP BY clause while creating a view in the SELECT statement.
  • If the created view contains columns from the single table, then we are allowed to modify the view. i.e., the use of multiple tables in view is not allowed.
  •  The view shouldn't be created using a subquery or include complex queries
  • The view contains any SQL aggregate functions; we are not allowed to modify the existing view.

CREATE OR REPLACE VIEW statement is used to add new fields or drop fields from the view.

Syntax: 

CREATE OR REPLACE VIEW VIEW_NAME AS SELECT COLUMN_NAME1, COLUMN_NAME2 FROM TABLE_NAME WHERE CONDITION;    

Example 1: Write a query to update the Employee_View and add the Manager Id column from the Emp table in the view.

CREATE OR REPLACE VIEW EMPLOYEE_VIEW AS SELECT EMPLOYEEID, CONCAT(FIRST_NAME,CONCAT(" ", LAST_NAME)) AS NAME, SALARY, MANAGERID FROM EMP WHERE DEPARTMENT IN ('ORACLE', 'FMW');

The above query modifies the existing Employee_view and modifies the records based on the given SELECT query.

Whenever we want to look at the records in the table, we use the SELECT * FROM query. We will use the view name. Similarly, we will do for view instead of the table name.

SELECT * FROM EMPLOYEE_VIEW;
SQL View

Example 2: Write a query to update the Manager_View.

CREATE OR REPLACE VIEW MANAGER_VIEW AS SELECT MANAGERID, MANAGER_NAME FROM MANAGER WHERE MANAGER_NAME LIKE ‘A%’;

The above query modifies the existing Employee_view and modifies the records based on the given SELECT query.

Whenever we want to look at the records in the table, we use the SELECT * FROM query. We will use the view name. Similarly, we will do for view instead of the table name.

SELECT * FROM MANAGER_VIEW;
SQL View

Inserting new record into the existing view

We insert the new records into the table. In the same way, we can also insert it into view.

Syntax

INSERT INTO VIEW_NAME VALUES(VALUE1, VALUE2);

Example: write a query to insert a new record into the Manager_view.

INSERT INTO MANAGER_VIEW VALUES (5, ‘Akash Kadam’);

As we insert new records into Manager_View. To check that new data is inserted or not, we will use the SELECT query:

SELECT * FROM MANAGER_VIEW; 
SQL View

3 DROP VIEW: -

We drop the table. In the same way, we can also drop view.

Syntax:

DROP VIEW VIEW_NAME;

Example: Write a query to drop the EMP_MAN_VIEW.

DROP VIEW EMP_MAN_VIEW; 

If we want to check whether a view is deleted or not, we will use the SHOW TABLES query.

SHOW TABLES;
SQL View

View name EMP_MAN_VIEW is not in the database, which means we successfully drop the view.


Related Topics

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

SQL SELECT BETWEEN Operator

This SQL tutorial explains and helps us understand how to use the BETWEEN operator in the SELECT query with examples. The SQL SELECT BETWEEN operator retrieves the data from the table...

2 minutes read.