Black friday

Save big!

All courses under $5 - for a limited time!

Code has been added to clipboard!

Working With SQL VIEW

Reading time 2 min
Published Aug 9, 2017
Updated Oct 3, 2019

SQL views are virtual tables with rows and columns, just like actual tables in a database. To create a view, you have to select fields from tables that exist in the database.

SQL VIEW: Main Tips

  • View in SQL means virtual tables made of a result-set.
  • An SQL view has columns and rows.
  • You can use WHERE and JOIN statements to show the information as if it was being received from a single table.

SQL VIEW Syntax and Examples

Example
CREATE VIEW v_name AS
SELECT col1, col2, ...
FROM tbl_name
WHERE condition;

Note: VIEW only displays the present information.

Every one of the active cells that are inside Products table is listed in the Present List Products view:

Example
CREATE VIEW [Present List Products] AS
SELECT ID, Name
FROM Products
WHERE Discontinued = No;

The example below shows how to query a view:

Example
SELECT * FROM [Present List Products];

Updating the VIEW

The syntax example below shows how to update your view:

Example
CREATE OR REPLACE VIEW v_name AS
SELECT col1, col2, ...
FROM tbl_name
WHERE condition;

The next step to finish updating the view is to insert one more column called Category:

Example
CREATE OR REPLACE VIEW [Present List Products] AS
SELECT ID, Name, Category
FROM Products
WHERE Discontinued = No;

Dropping the VIEW

To remove a view use DROP VIEW:

Example
DROP VIEW v_name;

SQL VIEW: Summary

  • Virtual tables that are made of a result-set of an actual table(s) in a database are called SQL views.
  • WHERE and JOIN statements used on views return the information as if it was stored in a single table.