SQL VIEW Keyword
In SQL, a view is a virtual table based on the result set of an SQL statement. A view does not store data itself — it dynamically pulls data from the underlying table(s).
SQL CREATE VIEW Syntax
SQL
x
1
CREATE VIEW view_name AS
2
SELECT column1, column2, ...
3
FROM table_name
4
WHERE condition;
SQL CREATE VIEW Example
This query creates a view named FemalePatients that returns a list of female patients from the Patients table.
SQL
1
1
CREATE VIEW FemalePatients AS
2
SELECT patient_id, first_name, last_name
3
FROM Patients
4
WHERE gender = 'F';

SQL Query The View Example
This query selects all records from the FemalePatients view.
SQL
1
1
SELECT *
2
FROM FemalePatients;

SQL CREATE OR REPLACE VIEW Syntax
SQL
1
1
CREATE OR REPLACE VIEW view_name AS
2
SELECT column1, column2, ...
3
FROM table_name
4
WHERE condition;
SQL CREATE OR REPLACE VIEW Example
This query updates the FemalePatients view to include the gender column.
SQL
1
1
CREATE OR REPLACE VIEW FemalePatients AS
2
SELECT patient_id, first_name, last_name, gender
3
FROM Patients
4
WHERE gender = 'F';
Note: This code does not work in TechKubo playground. SQLite does not support CREATE OR REPLACE VIEW. You must use DROP VIEW first and then CREATE VIEW again to simulate replacing a view.
SQL DROP VIEW Syntax
SQL
1
1
DROP VIEW view_name;
SQL DROP VIEW Example
This query deletes the FemalePatients view from the database.
SQL
1
1
DROP VIEW FemalePatients;
