SQL Views

SQL views are virtual tables based on the result of an SQL query. They do not store data physically but present data from one or more tables in a simplified or specific format.

Views can be used to:

Tutorials dojo strip
  • Simplify complex queries
  • Present specific columns or filtered data
  • Improve security by limiting access to specific rows or columns

Note: Views are automatically refreshed when queried — no need to manually update the data.

SQL Views Syntax

To create a view, use the following syntax:

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

SQL Views Example

The following example creates a view named Brazil_Customers showing only customers from Brazil:

CREATE VIEW Brazil_Customers AS
SELECT first_name, last_name
FROM Patients
WHERE gender = 'F';

You can then query the view like a regular table:

SELECT * FROM Brazil_Customers;

SQL Views Updating a View Example

To update a view, most SQL environments support the CREATE OR REPLACE VIEW syntax:

CREATE OR REPLACE VIEW Brazil_Customers AS
SELECT first_name, last_name, dob
FROM Patients
WHERE gender = 'F';

Note: The OR REPLACE clause is not supported in our playground.
If you try this in our playground, you’ll get a syntax error.

Workaround for our playground

To “update” a view in our playground, you must first drop the existing view, then create it again:

DROP VIEW Brazil_Customers;

CREATE VIEW Brazil_Customers AS
SELECT first_name, last_name, dob
FROM Patients
WHERE gender = 'F';

This approach ensures compatibility while achieving the same result.

SQL Views Dropping a View Example

To remove a view from the database, use:

DROP VIEW Brazil_Customers;

SQL Views Visual Diagram

SQL Views Labs

Tutorials dojo strip
Scroll to Top