Example of Create View

Consider a Students table with the following data:

Schema of students Table

After Inserting Some data into the Students table, the table looks:

Data in students Table

Following is an example to create a view from the Students table.

Example 1

Query:

Let’s create an view for some columns in the students table using some clauses.

CREATE VIEW high_scorers AS
SELECT name, percentage, city
FROM students
WHERE percentage > 80.0;

Output:

Create high_scorers view

Explanation: The above query creates a view naming high_scorers which have 3 columns, namely “name,” “percentage” and “city.” It includes the data only of students with more than 80 in the percentage column of the Students table.

We can check if the View is created successfully using the .tables command. In the above image we can see high_scorers and students as output of .tables command. high_scorers is visible in the output as we just created that view.

Query:

Let’s execute queries on high_scorers view similarly to how you query an actual table.

Output:

Output of high_scorers

Query:

Let’s findout the city in the high_scorers WHERE city name is City1.

Output:

Explanation: This query will display all the records where city is City1 (Students of City1) from the high_scorers view.

Temporary View

We haven’t used the “TEMPORARY” keyword, so We View will be saved even after closing the Database Connection.

Below is an example using the “TEMPORARY” keyword. The View created is only visible to the database connection that created it and is automatically deleted when the connection is closed.

Query:

CREATE TEMPORARY VIEW high_scorers AS
SELECT name, percentage, city
FROM students
WHERE percentage > 80.0;

Output:

Create Temporary view named high_scorers

Explanation: The above query will create a temporary view including “name“, “percentage” and “city” columns from students table. The VIEW will get deleted once the Database Connection is closed. We can perform all the operations as the standard View. The View is deleted once the Database connection is closed.

How to Create View in SQLite

SQLite is a self-contained, serverless, and open-source relational database management system. It is used for simplicity, efficiency, and portability, SQLite is widely employed in diverse applications, from embedded systems to mobile devices and largescale software.

It is serverless, zero-configuration setup, and supports standard SQL making it a preferred choice for developers to perform operations and queries. It supports various platforms, allowing for easy implementation and reliable data storage and retrieval.

Similar Reads

What is a View?

A VIEW is a composition of a table in the form of a predefined SQLite query. A view can contain all rows of a table or selected rows from one or more tables. A view can be created from one or many tables, which depends on the written SQLite query to create a view....

Example of Create View

Consider a Students table with the following data:...

Drop a View

Syntax:...

Conclusion

In SQLite, views provide a powerful mechanism for simplifying complex queries and enhancing the overall manageability of databases. Views offer a layer of abstraction that hides the complexity of underlying data structures by allowing users to encapsulate specific queries into virtual tables. This can be particularly useful for organizing and presenting data that aligns with clear business logic or reporting requirements....