x

SQL – VIEWS

Prev     

  • A VIEW in SQL is nothing but a virtual table. It has columns & rows just like a physical/real table. The columns & rows in this virtual table will be based on the result-set of the SQL statement that we use to create the VIEW.
  • A VIEW will always display current data, whenever it is used.
  • Generally, a VIEW is created when we need columns from different tables in one table. Syntax for SQL VIEW is given below

SQL Syntax for VIEW:

SQL Syntax for VIEW CREATE VIEW view_name AS
SELECT t1.column_name1, t2.column_name2, etc
FROM table_name1 AS t1, table_name2 AS t2 etc
WHERE  [condition];

Note:

  • You can use either single table to create VIEW or multiple tables with multiple columns in the SQL statement as per your requirement.
  • This SQL statement is nothing but the normal SQL query that you use to fetch the data based on your requirement.

Example for how to use VIEW in SQL:

Please consider following 2 tables with few records as given below.


Table1:

Table name (for example): student1
Column names in this table: Student_ID, Student_name, City and Age
Available records: 4 rows

sql-table1

Table2:

Table name (for example): student2
Column names in this table: Student_ID, Department, College and Rank
Available records: 4 rows