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.
Learn SQL
Introduction
Syntax
Data Types
Server Data Types
Commands
Commands List
Wildcards
Constraints
Aggregate Functions
Date Functions
Date Format
Injection
SQL Server Hosting
Views
Auto-incrementation
SQL Operators
AS
AND & OR
IN
BETWEEN
WHERE
GROUP BY
HAVING
ORDER BY
LIKE
NOT
NOT EQUAL
UNION
NULL
NOT NULL
DEFAULT
UNIQUE
FOREIGN KEY
PRIMARY KEY
CHECK
Indexes
ALTER TABLE
CREATE DATABASE
CREATE TABLE
DELETE
DROP
INSERT INTO SELECT
INSERT INTO
SELECT
SELECT DISTINCT
SELECT INTO
SELECT TOP
UPDATE
FULL OUTER JOIN
INNER JOIN
JOIN
LEFT JOIN
RIGHT JOIN
AVG()
COUNT
FIRST
LAST
MAX
MIN()
SUM()
LEN
UCASE
MID
NOW
ROUND
FORMAT
LOWER
CONVERT
ISNULL