Skip to main content
Main Concepts Covered

Working with SQLite & Database Setup

In this guide, you will learn the fundamentals of SQLite, its data types, and how to set up, connect, and populate a local database in VS Code using the Database Client extension — a universal tool that allows you to connect to and manage SQLite, PostgreSQL, MySQL, and any other database.

Interactive Jupyter Practice Notebook

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 databases directly inside Visual Studio Code, install either of these extensions:
  1. Database Client (by cweijan) — ⭐ Primary / Recommended Universal Tool
    • Universal database manager for SQLite, PostgreSQL, MySQL, MariaDB, SQL Server, Redis, MongoDB, Snowflake, DuckDB, etc.
    • Built-in visual data grid, SQL query editor with auto-completion, ER diagram generator, mock data generator, and export tools (CSV/JSON/SQL).
    • Manages a dedicated Query folder in your project structure for your .sql files.
  2. SQLite (by Alex Covizzi) — Alternative Lightweight Option
    • Simple extension specifically for opening .db files and running queries.

Connecting Database Client to Any Database

The Database Client extension turns VS Code into a universal database IDE. Whether you are using a local SQLite file or a remote database server, the workflow is consistent.

1. Supported Databases

2. The Query Folder in Project Structure

When you work with Database Client, SQL query scripts are organized directly in your project structure under a Query folder (or .dbclient folder):

3. Connecting to External Databases (PostgreSQL, MySQL, etc.)

To connect to any database server:
  1. Click the Database (cylinder) icon in the VS Code Activity Bar (left sidebar).
  2. Click + (Create Connection) →\rightarrow select your database engine (e.g. PostgreSQL or MySQL).
  3. Enter your connection credentials:
    • Host: localhost (or cloud endpoint)
    • Port: 5432 (PostgreSQL) / 3306 (MySQL)
    • Username: postgres / root
    • Password: <your-password>
    • Database: <database_name>
  4. Click Test Connection, then click Connect.

Step 1: Create Database and Connect in Database Client

  1. Create a new empty database file in your workspace root directory named:
  1. Open the Database Client panel by clicking the Database (cylinder) icon in the VS Code Activity Bar (left sidebar).
  2. Click the + (Create Connection) button at the top of the panel.
  3. Select SQLite from the database list.
  4. Enter the connection settings:
    • Connection Name: Employee DB
    • Database Path / File: Click the Browse / File Picker icon and select employee.db (or type ./employee.db).
  5. Click Connect (or Save).
Your database is now connected and visible in the Database Client explorer:

Step 2: Create Tables Using .sql Script

  1. Create a new SQL script file in your workspace directory (or inside your Query/ folder) named:
  1. Open setup.sql and paste the table definition DDL statements:
  1. Execute in Database Client:
    • In VS Code with setup.sql open, make sure Employee DB is selected in the Database Client toolbar or status bar.
    • Highlight the CREATE TABLE statements.
    • Press Cmd + Enter (macOS) or Ctrl + Enter (Windows/Linux), or click the green Run / Execute button in the query toolbar.
  2. Click the Refresh icon in the Database Client panel. You will now see departments and employees listed under the Tables folder!
[!IMPORTANT] Executing Selected Queries: Always highlight/select the specific SQL statements you want to run before executing (Cmd+Enter / Ctrl+Enter). Running the whole script again after tables are created will result in a “table already exists” error.

Step 3: Populate Tables Using .sql Script

Append the following INSERT INTO statements to your setup.sql file (or Query/setup.sql):

Department Data

Employee Data

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

Execute in Database Client:

  1. Highlight the INSERT INTO statements.
  2. Press Cmd + Enter (macOS) or Ctrl + Enter (Windows/Linux) to run the insertion.
  3. In the Database Client sidebar, double-click the employees table to open the interactive data grid and view all 25 rows populated live!

Step 4: Verify the Data

Method 1: Using Database Client Interactive Data Grid (Fastest)

Double-click on the departments or employees table in the Database Client sidebar. An interactive spreadsheet view will open displaying all columns, rows, and data types.

Method 2: Running Verification Queries in SQL Console

Run the following queries in the Database Client SQL editor by highlighting and pressing Cmd + Enter / Ctrl + Enter: Display all departments:
Display all employees:
Count the total number of employees:
Expected Output

Step 5: Execute Sample Practice Queries

Execute the following queries in the Database Client SQL editor (Cmd + Enter / Ctrl + Enter):

1. Display all employee names

2. Display employees from Hyderabad

3. Display employees with salaries greater than ₹70,000

4. Display employees sorted by salary descending

5. Display employee names with their department names (INNER JOIN)

6. Display all employees including those without a department (LEFT JOIN)

Advanced Database Client Features:

  • 📊 Column Sorting & Filtering: Click on any column header in the result grid to sort ascending/descending or filter by values.
  • 🗺️ Visual ER Diagram (ERD): Right-click Employee DB in the sidebar and choose Visual Relationship (ERD) to view relationships between departments and employees.
  • 📤 Export Results: Right-click the result table or any table in the sidebar to export data into CSV, JSON, or executable SQL INSERT scripts.

Option 2: Using SQLite Extension Plugin

If you prefer a lightweight, SQLite-specific tool instead of the universal Database Client, you can use the SQLite extension by Alex Covizzi.

Step 1: Install the Extension

  1. Open the Extensions view (Ctrl + Shift + X on Windows/Linux or Cmd + Shift + X on macOS).
  2. Search for SQLite by Alex Covizzi and click Install.

Step 2: Open Your Database

  1. Open the Command Palette:
    • Windows/Linux: Ctrl + Shift + P
    • macOS: Cmd + Shift + P
  2. Search for and select: SQLite: Open Database.
  3. Choose employee.db from your workspace directory.
  4. An SQLITE EXPLORER section will appear in your VS Code Explorer sidebar showing the database and its tables.

Step 3: Run Queries from .sql Files

  1. Open setup.sql in VS Code.
  2. Select/highlight any query (or block of queries) you wish to run.
  3. Right-click anywhere in the editor and choose Run Query (or press Cmd + Shift + Q / Ctrl + Shift + Q).
  4. When prompted, select employee.db.
  5. The query results will appear in a separate SQLite Results panel in VS Code.

Step 4: Browse Tables & Records

  • In the SQLITE EXPLORER panel in the Explorer sidebar, expand employee.db to see all tables (departments, employees).
  • Click the play / show table icon next to any table to view all its records instantly.

Running SQL in Jupyter Notebooks

If you prefer using Jupyter Notebooks (.ipynb) instead of .sql files in VS Code, you can run SQL queries directly inside notebook cells using two different approaches.

Approach 1: Using %sql Cell Magics (ipython-sql)

ipython-sql allows you to write raw SQL directly inside Jupyter Notebook cells using %sql (single line) and %%sql (entire cell).

Step 1: Install Required Packages & Versions

Run this in your terminal or virtual environment:
[!NOTE] prettytable<3.12 prevents compatibility issues when rendering tabular SQL results with ipython-sql.

Step 2: Load Extension & Connect to Database

In your Jupyter Notebook cell, load the extension and connect to employee.db:

Step 3: Run Single-Line SQL Queries (%sql)

Step 4: Run Multi-Line SQL Queries (%%sql)

Use %%sql at the very top of a notebook cell to write multi-line SQL queries:

Step 5: Convert Query Result to Pandas DataFrame

Approach 2: Using Python sqlite3 + pandas

You can also use Python’s built-in sqlite3 module combined with pandas to execute queries and display clean DataFrame outputs without installing magic extensions.
[!TIP] Which approach should you use?
  • Use Approach 1 (%%sql magic) for quick, interactive SQL exploration directly in notebook cells.
  • Use Approach 2 (sqlite3 + pandas) when building data analysis pipelines, creating visualizations (Matplotlib/Seaborn), or writing production Python scripts.

Database Schema

Summary

You have successfully:
  • Connected and managed databases using Database Client in Visual Studio Code as the primary universal method.
  • Explored Option 2 using the lightweight SQLite plugin by Alex Covizzi.
  • Created the employee.db database and managed .sql scripts.
  • Created the departments and employees tables using DDL scripts.
  • Populated 5 departments and 25 employee records using DML scripts.
  • Verified data visually via the interactive data grid and through verification queries.
  • Executed sample SQL queries and explored ER Diagram visualization.
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.