Structured Query Language (SQL) provides a variety of functions and operators. One such function is called a view. It is a virtual table that is formed as a result of a query. Views are created so that the table is stored permanently unless it is dropped, and can be accessed anytime.
CREATE VIEW viewName AS SELECT columnName FROM tableName WHERE condition;
Let’s create a demo table called
Student and implement a query to create a view. Run the code below to create the table
CREATE TABLE Student ( s_ID int, s_name varchar(255), s_major varchar(255), s_grade varchar(255), s_age int ); INSERT INTO Student VALUES (89237,'Joseph','CS','A',19); INSERT INTO Student VALUES (87496,'Chris','Econ','B',18); INSERT INTO Student VALUES (83927,'Lisa','CS','A',20); INSERT INTO Student VALUES (83216,'Paul','Psych','F',17); INSERT INTO Student VALUES (87344,'Kelly','Psych','C',18); SELECT * FROM Student;
To create a view that consists of students who got a passing grade, we use the keywords
CREATE VIEW and
AS. The query that comes after these keywords will be a standard query.
CREATE VIEW students_with_passing_grade AS SELECT * FROM Student WHERE s_grade <> 'F'; SELECT * FROM students_with_passing_grade;
View all Courses