SQL · Module 1 · Lesson 41 min read

Setup & Your Practice Database

Option 1: Supabase (easiest, in the browser) ✅

  1. Create a free project at supabase.com
  2. Open SQL Editor → New query
  3. Paste the practice script from the code example → Run

Option 2: Install PostgreSQL locally

  • Windows/macOS: download the installer from postgresql.org (includes pgAdmin)
  • macOS: brew install postgresql@16
  • Linux: sudo apt install postgresql
  • Docker: docker run -e POSTGRES_PASSWORD=secret -p 5432:5432 postgres:16

Tools to write SQL

  • Supabase SQL Editor – web-based, nothing to install
  • pgAdmin – the official GUI
  • DBeaver / TablePlus / DataGrip – multi-database GUIs
  • psql – the command-line client: psql -U postgres
  • VS Code extensions (SQLTools, PostgreSQL)

Handy psql commands

  • \l list databases, \c dbname connect
  • \dt list tables, \d expenses describe a table
  • \x expanded display, \timing show query time, \q quit

The practice database 🗄️

A mini BudgetBuddy with 5 tables:

TableWhat it stores
users6 people with their city, monthly budget and who referred them
categoriesFood, Transport, Bills… with sub-categories (Groceries → Food)
expenses31 expenses from September to November 2026
accountsbank, cash and wallet balances
billsupcoming bills and whether they are paid

Some data is missing on purpose (Maya and Hari have no expenses, Hari has no city, Maya has no budget, nobody spent on Health) so you can practise NULLs and joins.

💡 If you ever break your data, just run the script again — it starts by dropping the tables and rebuilding everything.

Example

SQL
-- 🗄️ BudgetBuddy practice database (PostgreSQL / Supabase)
-- Run this once. Every lesson's examples use these tables.

DROP TABLE IF EXISTS bills, accounts, expenses, categories, users CASCADE;

CREATE TABLE users (
  id             INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name           TEXT NOT NULL,
  email          TEXT NOT NULL UNIQUE,
  city           TEXT,
  monthly_budget NUMERIC(10,2),
  referred_by    INT REFERENCES users(id),
  created_at     TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE categories (
  id        INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name      TEXT NOT NULL UNIQUE,
  parent_id INT REFERENCES categories(id)
);

CREATE TABLE expenses (
  id          BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  user_id     INT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  category_id INT REFERENCES categories(id),
  title       TEXT NOT NULL,
  amount      NUMERIC(10,2) NOT NULL CHECK (amount > 0),
  spent_on    DATE NOT NULL DEFAULT CURRENT_DATE,
  note        TEXT,
  tags        TEXT[] NOT NULL DEFAULT '{}',
  created_at  TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE accounts (
  id      INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  name    TEXT NOT NULL,
  balance NUMERIC(12,2) NOT NULL DEFAULT 0 CHECK (balance >= 0)
);

CREATE TABLE bills (
  id       INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  user_id  INT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  title    TEXT NOT NULL,
  amount   NUMERIC(10,2) NOT NULL,
  due_date DATE NOT NULL,
  paid     BOOLEAN NOT NULL DEFAULT false
);

INSERT INTO users (name, email, city, monthly_budget, referred_by, created_at) VALUES
  ('Asha Sharma',   'asha@example.com',   'Kathmandu', 30000, NULL, '2026-01-10'),
  ('Ravi Thapa',    'ravi@example.com',   'Pokhara',   25000, 1,    '2026-02-14'),
  ('Sita Gurung',   'sita@example.com',   'Kathmandu', 40000, 1,    '2026-03-05'),
  ('Bikash Rai',    'bikash@example.com', 'Lalitpur',  20000, 2,    '2026-04-20'),
  ('Maya Karki',    'maya@example.com',   'Pokhara',   NULL,  3,    '2026-06-01'),
  ('Hari Shrestha', 'hari@example.com',   NULL,        15000, NULL, '2026-08-15');

INSERT INTO categories (name, parent_id) VALUES
  ('Food', NULL), ('Transport', NULL), ('Bills', NULL), ('Shopping', NULL),
  ('Entertainment', NULL), ('Health', NULL),
  ('Groceries', 1), ('Restaurants', 1), ('Electricity', 3), ('Internet', 3);

INSERT INTO expenses (user_id, category_id, title, amount, spent_on, note, tags) VALUES
  (1, 7,  'Weekly groceries',  2450, '2026-09-03', NULL, '{essential}'),
  (1, 8,  'Dinner at Thamel',  1800, '2026-09-12', 'Birthday dinner', '{social}'),
  (1, 2,  'Taxi to office',     450, '2026-09-15', NULL, '{work}'),
  (1, 9,  'Electricity bill',  1350, '2026-09-28', NULL, '{essential,monthly}'),
  (1, 7,  'Vegetables',         820, '2026-10-02', NULL, '{essential}'),
  (1, 4,  'Running shoes',     6500, '2026-10-10', 'Festival sale', '{personal}'),
  (1, 10, 'Internet bill',     1500, '2026-10-25', NULL, '{essential,monthly}'),
  (1, 5,  'Movie tickets',      900, '2026-11-01', NULL, '{social}'),
  (1, 7,  'Monthly groceries', 5200, '2026-11-05', NULL, '{essential}'),
  (1, 2,  'Bus pass',          1000, '2026-11-08', NULL, '{work,monthly}'),
  (2, 8,  'Lakeside cafe',      650, '2026-09-05', NULL, '{social}'),
  (2, 2,  'Fuel',              3000, '2026-09-18', NULL, '{essential}'),
  (2, 9,  'Electricity bill',   980, '2026-09-30', NULL, '{essential,monthly}'),
  (2, 4,  'Jacket',            4200, '2026-10-14', NULL, '{personal}'),
  (2, 7,  'Groceries',         2100, '2026-10-20', NULL, '{essential}'),
  (2, 5,  'Paragliding',       8500, '2026-11-02', 'Once in a lifetime!', '{adventure,social}'),
  (2, 2,  'Fuel',              3200, '2026-11-09', NULL, '{essential}'),
  (3, 7,  'Groceries',         3100, '2026-09-02', NULL, '{essential}'),
  (3, 10, 'Internet bill',     1500, '2026-09-20', NULL, '{essential,monthly}'),
  (3, 8,  'Team lunch',        2400, '2026-10-03', NULL, '{work,social}'),
  (3, 4,  'Laptop bag',        3500, '2026-10-18', NULL, '{work}'),
  (3, 9,  'Electricity bill',  1620, '2026-10-29', NULL, '{essential,monthly}'),
  (3, 2,  'Flight to Pokhara', 7800, '2026-11-03', 'Holiday', '{travel}'),
  (3, 8,  'Dinner',            1250, '2026-11-06', NULL, '{social}'),
  (3, 7,  'Groceries',         2900, '2026-11-10', NULL, '{essential}'),
  (4, 2,  'Bike service',      1200, '2026-09-08', NULL, '{maintenance}'),
  (4, 7,  'Groceries',         1750, '2026-09-25', NULL, '{essential}'),
  (4, 5,  'Concert',           2500, '2026-10-11', NULL, '{social}'),
  (4, 8,  'Momo party',         950, '2026-10-21', NULL, '{social}'),
  (4, 4,  'Phone case',         650, '2026-11-04', NULL, '{personal}'),
  (4, 9,  'Electricity bill',   890, '2026-11-07', NULL, '{essential,monthly}');

INSERT INTO accounts (user_id, name, balance) VALUES
  (1, 'Cash', 5000), (1, 'Bank', 85000), (2, 'Bank', 42000),
  (3, 'Bank', 120000), (3, 'eSewa wallet', 3500), (4, 'Cash', 2500);

INSERT INTO bills (user_id, title, amount, due_date, paid) VALUES
  (1, 'Rent', 15000, '2026-12-01', false),
  (1, 'Internet', 1500, '2026-11-25', false),
  (2, 'Rent', 12000, '2026-12-01', false),
  (3, 'Insurance', 8000, '2026-11-20', true),
  (4, 'Phone', 500, '2026-11-28', false);

SELECT 'Practice database ready ✅' AS status;