Course contentsShow
← Databases & SQL
Lesson 1110 of 1,95310. Embedded and Lightweight DatabasesFree lesson

The Role of Virtual Tables

Extend SQLite capabilities by mapping non-SQL data sources into queryable tables.

In this lesson, you will learn how to bridge the gap between traditional SQL databases and external, non-relational data sources using SQLite’s Virtual Tables.

The Bridge Between Worlds

Usually, a database acts as a gatekeeper: it owns the data, manages the storage pages, and controls the index structures. However, in the world of embedded databases, you often need to query data that lives outside of the database file—such as a JSON file, a live network stream, or even the contents of a local directory.

A Virtual Table acts as a proxy. Instead of storing data on disk, it provides a structured interface that tells SQLite how to fetch data from an external source on the fly. When you run a SELECT statement against a virtual table, SQLite invokes a set of "methods" defined in the virtual table implementation to translate your SQL request into the specific commands required to pull that external data.

Seeing Through the Lens

Think of a virtual table as a camera lens: the underlying data is the landscape, and the virtual table is the frame through which you view it. You can perform standard SQL operations like WHERE clauses or JOINs on the virtual table, and SQLite handles the communication with the external data provider.

For example, if you want to treat a large CSV file like a database table, you might implement an FTS5 (Full-Text Search) or CSV virtual table:

-- Create a virtual table mapped to a CSV file
CREATE VIRTUAL TABLE temp.my_csv_data 
USING csv(filename='data_source.csv');

-- Query the external file as if it were a local table
SELECT * FROM my_csv_data WHERE status = 'active';

In this example, SQLite does not "import" the CSV; it interprets the file in real-time as the rows are requested. This allows you to integrate heterogeneous data into your application's logic without the overhead of moving or transforming that data into a formal schema. It keeps your application’s data access layer unified under the familiar syntax of SQL, regardless of where the data actually resides.

Key Takeaway: Virtual tables extend SQLite’s capabilities by allowing you to query external, non-relational data sources using standard SQL syntax without requiring physical storage within the database file.