Skip to main content

Working with SQLite & Database Setup

In this guide, you will learn the fundamentals of SQLite, its data types, and how to set up a local database in VS Code with practice data.

Why SQLite?

SQLite is a software library that provides a lightweight, serverless relational database management system (RDBMS). Unlike traditional databases like PostgreSQL or MySQL, SQLite does not run as a separate server process. Instead, it reads and writes data directly to a single file on your computer’s disk.

Features of SQLite

  • Serverless: It does not require a separate server background process. The database engine runs inside the application process.
  • Self-Contained: A single database is stored entirely in a single cross-platform file.
  • Zero Configuration: There is no setup or administration needed. You just create a file and start using it.
  • Transactional (ACID): Even though it is lightweight, it fully supports ACID properties (Atomicity, Consistency, Isolation, Durability) to ensure data safety.

Advantages

  • Simple to use: Perfect for learning SQL, prototyping applications, and local development.
  • Highly Portable: You can copy, share, or email the database file easily.
  • Fast and Lightweight: Highly optimized code that runs fast and takes up very little memory.
  • Widely Adopted: It is the most deployed database engine in the world, used in web browsers (Chrome, Firefox), mobile phones (Android, iOS), and many desktop applications.

Limitations

  • Concurrency Issues: SQLite locks the entire database file during writes, meaning it is not suitable for high-write concurrency applications.
  • Not suited for Big Data: While it can handle up to 281 TB database sizes in theory, it is not optimized for massive multi-terabyte enterprise datasets.
  • No User Management: It does not support native user roles, permissions, or access control. Anyone with read/write access to the file can modify the database.

SQLite Data Types

Unlike other relational databases, SQLite has a dynamic type system. It supports the following storage classes (data types):

Common Examples


Prerequisites

To work with SQLite directly inside your editor, install the following Visual Studio Code extension:
  • SQLite by Alex Covizzi
The extension allows you to:
  • Open and view SQLite databases
  • Execute SQL scripts
  • Browse tables
  • View and edit records
  • Run SQL queries

Step 1: Create a Database File

  1. Create a new empty file in your workspace directory named:
  1. Open the Command Palette:
    • Windows/Linux: Ctrl + Shift + P
    • macOS: Cmd + Shift + P
  2. Search for and select:
  1. Select the employee.db file you just created from the dropdown menu/file picker.
The database is now open and ready.

Step 2: Create the Tables

  1. Create a new file in your workspace directory named:
  1. Open setup.sql and paste the following SQL statements into it:
  1. Right-click anywhere inside the editor of setup.sql and select Run Query. When prompted, select the database (employee.db) to execute the statements and create the tables.
[!IMPORTANT] Executing Queries in VS Code:
  • Run Selected Query: You can highlight/select a specific SQL query, right-click, and choose Run Query to run only that part.
  • Avoid Errors: If you run the entire document after some tables or data have already been created/inserted, SQLite will return errors (e.g., “table already exists”). It is best to highlight and run only the new queries you want to execute, or delete/comment out previously executed queries from setup.sql.

Step 3: Populate the Tables

Append the following SQL statements to your setup.sql file, then select the new statements, right-click, and choose Run Query to populate the tables.

Department Data

Employee Data

Note: Gopal Krishna has a NULL department to demonstrate LEFT JOIN and IS NULL queries.

Step 4: Verify the Data

Display all departments.
Display all employees.
Count the total number of employees.
Expected Output

Step 5: Execute Sample Queries

Display all employee names.
Display employees from Hyderabad.
Display employees with salaries greater than ₹70,000.
Display employees sorted by salary.
Display employee names with department names.
Display all employees, including those without a department.

Database Schema

Summary

You have successfully:
  • Installed the SQLite extension in Visual Studio Code.
  • Created a SQLite database.
  • Created the departments and employees tables.
  • Inserted 5 departments and 25 employee records.
  • Added one employee with a NULL department for practicing LEFT JOIN and IS NULL.
  • Verified the data.
  • Executed sample SQL queries.
Your database is now ready to practice all SQL topics, including SELECT, WHERE, GROUP BY, HAVING, JOINs, Aggregate Functions, and Window Functions. The next chapter focuses entirely on DQL (SELECT), where you’ll learn how to retrieve and analyze data from the database.