SQL View - SQL Create View Statement - Views in SQL



SQL View

  • In Microsoft SQL Server (MSSQL), a view is a virtual table that is based on the result of a SELECT query. Views allow you to simplify complex queries, encapsulate logic, and provide a consistent interface to the data stored in one or more tables. You can create, modify, and query views using SQL statements. Here's how to work with views in MSSQL:

Demo wikitechydatabase

  • Below is a selection from the wikitechytable table used in the examples:
sql-view-table

1. Creating a View:

  • To create a view, you use the CREATE VIEW statement. Here's the basic syntax:

Syntax

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • view_name: The name of the view you want to create.
  • column1, column2, ...: The columns you want to include in the view.
  • table_name: The table(s) from which you want to retrieve data.
  • condition (optional): A condition to filter the data.

Example

CREATE VIEW wikitechyview AS
SELECT *
FROM wikitechytable
WHERE fees >= 50;

Output

sql-view-create

2. Querying a View:

  • Once a view is created, you can query it just like you would query a table.

Example

SELECT *
FROM wikitechyview;

Output

sql-view-querying

3. Dropping a View:

  • To remove a view from the database, you use the DROP VIEW statement.

Example

DROP VIEW wikitechyview;

Output

sql-view-drop


Related Searches to SQL View - SQL Create View Statement - Views in SQL