SQLite: check if a table exists
Codemia Knowledge Hub ·
Query the sqlite_master (or sqlite_schema) table with type = 'table' and the table name. A returned row means the table exists. If you only check so you can create the table, skip the check and use CREATE TABLE IF NOT EXISTS, which does it in one statement with no check-then-act race.
Querying the schema table
For a 1 or 0 result:
SQLite 3.33.0 (released August 2020) added sqlite_schema as an alias. Both names work in modern SQLite, but sqlite_master is more portable if you must support older versions.
Keep the type = 'table' filter. sqlite_master holds every schema object, so without it a view named users would match and you would wrongly conclude the table exists.
Parameterized checks in application code
When the table name comes from a variable, pass it as a parameter to avoid SQL injection.
Python:
JavaScript (better-sqlite3, Node.js):
Java (JDBC):
PRAGMA table_info for column metadata
If the table exists, this returns one row per column with its name, type, default value and nullable flag. An empty result means the table does not exist (a table with no columns is not possible in practice).
It is useful when you also need the columns, but it is less direct for a pure existence check, and it cannot be parameterized like a normal query. That makes it unsafe with table names from user input.
CREATE TABLE IF NOT EXISTS
This beats checking and then creating because:
- It is atomic, so two threads cannot both see "not found" and both try to create the table.
- It takes one round trip to the database instead of two.
- The code is shorter and harder to get wrong.
If the table already exists, the statement does nothing. It does not check that the existing table has the schema you expect, so a table with the right name but wrong columns passes silently. Use PRAGMA table_info if you need to validate columns.
Views, indexes and other schema objects
The type column can be table, view, index or trigger:
Temporary tables
Tables created with CREATE TEMP TABLE live in a separate catalog and do not appear in sqlite_master. Query sqlite_temp_master (or sqlite_temp_schema) instead:
To cover both regular and temporary tables, query both catalogs:
Attached databases
An attached database has its own catalog, prefixed with the alias from the ATTACH statement:
Comparing the methods
| Method | Returns | Parameterizable | Finds Temp Tables | Best For |
SELECT FROM sqlite_master | Row if exists | Yes | No | General existence check |
SELECT EXISTS(...) | 1 or 0 | Yes | No | Boolean result in code |
PRAGMA table_info(name) | Column metadata | No | Yes | Existence + schema inspection |
CREATE TABLE IF NOT EXISTS | Nothing (DDL) | No | N/A | Safe table creation |
SELECT FROM sqlite_temp_master | Row if exists | Yes | Yes (only temp) | Temp table checks |
Related reading
- How do I check to see if a value is an integer in MySQL?
- How do I clone a Django model instance object and save it to the database?
- How do I Cluster Hibernate ORM Identifiers when using GenerationType.Table
- How do I configure HikariCP in my Spring Boot app in my application.properties files?
- How do I connect to a MySQL Database in Python?
- How do I connect to mongodb with node.js and authenticate?
- How do I convert from BLOB to TEXT in MySQL?
- How do I copy a database from one MongoDB server to another?
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.