a shot of dev knowledge

RELATED TAGS

What are views in SQL?

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.

Syntax

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 Student.

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.

Code example

CREATE VIEW students_with_passing_grade AS
SELECT *
FROM Student
WHERE s_grade <> 'F'; 

SELECT * FROM students_with_passing_grade;

RELATED TAGS

RELATED COURSES

View all Courses

Keep Exploring