🔥 $100K Hit! Where Will Bitcoin Go Next? Find Out Live!

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];
DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

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.