SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. SQL Views

Text copied!

« Previous
Next »

SQL Views

July 5, 2023, 5:29 p.m. under SQL

  • Hi! You should check SQL Variables post first.

    In SQL, Views are like saved queries result that act as virtual tables, It's not an actual table but a saved query result that you can treat as if it were a table.

    Views help you organize and simplify your data, making it easier to work with and control who sees what.

    Here's the basic syntax for creating a view in SQL :
    CREATE VIEW view_name 
    AS
    SELECT column1, column2, column3, ...
    FROM table_name;
            

    • CREATE VIEW : This keyword is used to indicate that you're creating a new view.

    • view_name : This is the name you want to give to your view.

    • AS : This keyword is used to specify that you're defining the structure of the view.

    • After the "AS" keyword, we specify the SELECT statement.

    Here's an example of creating a simple view in SQL :

    1. Let's assume you have a database with two tables : [i]. Person [ii]. Departments

    SQL Views

    2. Now, let's create a view that combines data from both tables to show the names, group names of [person] along with their respective department names from [Departments] table:

    3. Please run the following SQL statement :

    CREATE VIEW Employees_view 
    AS
    SELECT e.[Name], e.GroupName, d.Department_Name
    FROM [Person] e
    JOIN [Departments] d ON d.Department_id = e.Department_id;
            
    SQL Views

    4. In above statement, We're creating a view called "EmployeeDepartmentView". The view combines data from the employees and departments tables by joining them on the department_id column. It includes the employee's name (e.name), salary (e.salary), and department name (d.name).

    Selecting View :

    5. Now, you can query this view as if it were a table.

    SELECT * FROM Employees_view;
            
    SQL Views

    6. Above query returns a result set showing each person's name, group_name, and the department they belong to, without having to join the tables every time.

    Deleting View :

    7. To drop (delete) a view in SQL, run below SQL statement.

    DROP VIEW Employees_view;
            
    SQL Views

    8. After executing above statement, the "Employees_view" view will be deleted from the database, and you won't be able to query it anymore.

    Great job! You've successfully created, selected, and deleted views in SQL!

    Frequently Asked Questions :

    What are the views in SQL?

    Views in SQL are virtual tables that display a subset of data from one or more tables.

    What is a SQL view vs table?

    A SQL view is a virtual table based on the result-set of an SQL statement, while a table contains actual data.

    What are the benefits of views in SQL?

    Benefits of views in SQL include data abstraction, security, simplified querying, and logical data independence.

    What is the use of view?

    Views in SQL are used to provide a simplified and customized view of data, enhance security, and facilitate complex queries.
    Thank You! You should check SQL Merge post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy