TechnoLearnAcademy

TECHNOLEARN

SQL VIEWS

A VIEW is a virtual table based on the result of a SQL SELECT query.

Syntax:      

                   CREATE VIEW view_name AS                                                                                                                                                                         SELECT column1, column2,                                                                                                                                                                     FROM  table_name                                                                                                                                                                                     WHERE condition;
Let’s understand the views with an example. For demonstration purposes, we will be using the table emp.
         Table EMP
         Table Project

Example:

   Objective: In this example, we will create a view that will retrieve records from the Emp Table where the location is Texas. 
                      CREATE VIEW VW_Texas_Records AS                                                                                                                                                              SELECT * FROM Tbl_EMP                                                                                                                                                                             WHERE Location = ‘Texas’;
   View Query:
                      SELECT * FROM VW_Texas_Records
   Output:

Example:

   Objective: In this example, we will use SQL joins to create views. 
                      CREATE VIEW VW_ProjectDetail AS                                                                                                                                                                SELECT e.FirstName, e.LastName , e.Emailaddress, P.Project                                                                                                                         FROM Tbl_Emp as e INNER JOIN Tbl_Project as p                                                                                                                                         on e.Ecode = p.Ecode;
   View Query:
                      SELECT * FROM VW_ProjectDetail;
   Output:
Drop View

Example:      

                  DROP VIEW VW_Texas_Records 
Alter View

Example:

   Objective: In this example, we will modify the existing view to add a condition where the department is IT. 
                      ALTER VIEW VW_Texas_Records AS                                                                                                                                                              SELECT * FROM Tbl_EMP                                                                                                                                                                             WHERE Location = ‘Texas’ and Department = ‘IT’;
   View Query:
                      SELECT * FROM VW_Texas_Records
   Output:
Scroll to Top