The Fee Tracker Database

Updated · 14 min read
easyPostgreSQL 18sqlsample-databaseschemafee-tracker

Every example in this course runs on one small database: the Fee Tracker. It holds the records of a family-run tutoring program where three sisters, Srihitha, Advitha and Navika Anamaneni, take courses in Java, SQL, JavaScript and Git. Knowing it well pays off: when a later lesson says "the students with no payments," you'll already know who that is. This lesson tours its five tables, explains every column, shows how the tables connect, and points out the special cases that later lessons rely on.

In One Minute

  • The Fee Tracker has five tables: students, courses, enrollments, payments and a small lookup table, cities.
  • Enrollments connect students to courses; payments belong to an enrollment, and one enrollment can have several payments (installments).
  • It's small on purpose (3 students, 6 courses, 7 enrollments, 7 payments), so you can see every row and check every answer by eye.
  • A few special cases are built in on purpose: a student with no payments, a course nobody takes, a payment with no method, two late payments.
  • Every Try it run starts from a fresh copy, and locally you reset it by loading fee-tracker.sql again.

The Five Tables at a Glance

Here is the whole database in one picture: every table, every column and every connection. The marks at the ends of the lines say how many rows can match on each side, as you learned in What Is a Database.

The whole Fee Tracker: five tables and how they connect
TableOne row isRows
studentsOne student3
coursesOne course the program offers6
enrollmentsOne student taking one course7
paymentsOne fee payment7
citiesOne city where students live, with its time zone2

Students

The students table has one row per student. Run it to see every column:

SQL▶ Try it
SELECT *
FROM students
ORDER BY student_id;
Output
 student_id | first_name | last_name |             email              |     phone      |     city      | state | grade | mentor_id | joined_on
------------+------------+-----------+--------------------------------+----------------+---------------+-------+-------+-----------+------------
        101 | Srihitha   | Anamaneni | srihitha.anamaneni@example.com | (415) 555-0142 | San Francisco | CA    |    12 |      NULL | 2024-08-19
        102 | Advitha    | Anamaneni | NULL                           | (415) 555-0178 | San Francisco | CA    |    10 |       101 | 2025-01-13
        103 | Navika     | Anamaneni | NULL                           | NULL           | Dallas        | TX    |     7 |       102 | 2025-09-02
(3 rows)
ColumnMeaning
student_idThe primary key: a number that identifies the student
first_name, last_nameThe student's name
email, phoneContact details; either can be NULL when it's not on file
city, stateWhere the student lives; together they match a row in cities
gradeSchool grade, from 1 to 12
mentor_idThe student_id of the student who mentors this one, or NULL for no mentor
joined_onThe date the student joined the program

Srihitha is the eldest and mentors Advitha; Advitha mentors Navika. Srihitha and Advitha live in San Francisco, and Navika lives in Dallas.

Courses

The courses table has one row per course.

SQL▶ Try it
SELECT *
FROM courses
ORDER BY course_id;
Output
 course_id |       title       | technology |    level     | fee_usd | prerequisite_id
-----------+-------------------+------------+--------------+---------+-----------------
         1 | Java Basics       | Java       | Beginner     |  450.00 |            NULL
         2 | Java OOP          | Java       | Intermediate |  500.00 |               1
         3 | Java Collections  | Java       | Intermediate |  550.00 |               2
         4 | SQL Basics        | SQL        | Beginner     |  400.00 |            NULL
         5 | JavaScript Basics | JavaScript | Beginner     |  450.00 |            NULL
         6 | Git Basics        | Git        | Beginner     |  250.00 |            NULL
(6 rows)
ColumnMeaning
course_idThe primary key
titleThe course name; no two courses share one
technologyJava, SQL, JavaScript or Git
levelBeginner or Intermediate
fee_usdThe current fee in US dollars, with exactly two decimal places
prerequisite_idThe course_id of the course you must finish first, or NULL if there isn't one

The prerequisites form a chain: Java Basics, then Java OOP, then Java Collections.

Enrollments

The enrollments table has one row for each student taking a course. This table is what connects students to courses: a student can take many courses, and a course can have many students.

SQL▶ Try it
SELECT *
FROM enrollments
ORDER BY student_id, course_id;
Output
 student_id | course_id | enrolled_on |  status
------------+-----------+-------------+-----------
        101 |         1 | 2025-08-25  | completed
        101 |         2 | 2025-10-27  | completed
        101 |         3 | 2025-12-22  | active
        101 |         4 | 2026-01-02  | active
        102 |         1 | 2025-12-29  | active
        102 |         4 | 2025-12-29  | active
        103 |         5 | 2025-11-10  | active
(7 rows)
ColumnMeaning
student_idWho is enrolled; matches a row in students
course_idWhich course; matches a row in courses
enrolled_onThe date they signed up
statusactive, completed or dropped

An enrollment has no id of its own: the pair student_id and course_id is its primary key, so the same student can't enroll in the same course twice.

Each filled cell is one enrollment; Git Basics has none

Payments

The payments table has one row per payment.

SQL▶ Try it
SELECT *
FROM payments
ORDER BY payment_id;
Output
 payment_id | student_id | course_id | amount_usd |   due_on   |  paid_on   | method
------------+------------+-----------+------------+------------+------------+--------
          1 |        101 |         1 |     450.00 | 2025-09-01 | 2025-08-28 | card
          2 |        101 |         2 |     250.00 | 2025-11-01 | 2025-10-30 | card
          3 |        101 |         2 |     250.00 | 2025-12-01 | 2025-12-03 | check
          4 |        101 |         3 |     550.00 | 2026-01-02 | 2025-12-29 | card
          5 |        101 |         4 |     200.00 | 2026-01-05 | 2026-01-05 | NULL
          6 |        103 |         5 |     225.00 | 2025-11-15 | 2025-11-14 | card
          7 |        103 |         5 |     225.00 | 2025-12-15 | 2025-12-19 | check
(7 rows)
ColumnMeaning
payment_idThe primary key
student_id, course_idThe enrollment this payment belongs to. The pair must match a row in enrollments, so nobody can pay for a course they never enrolled in
amount_usdThe amount actually paid, always more than zero
due_onThe date the payment was due
paid_onThe date it was paid
methodcard, check or cash, or NULL if nobody recorded it

One enrollment can have several payments: Srihitha paid for Java OOP in two installments (payments 2 and 3), and so did Navika for JavaScript Basics (payments 6 and 7).

All seven payments by the date they were paid; two were late

Cities

The cities table is a small lookup table: one row per city, with its time zone.

SQL▶ Try it
SELECT *
FROM cities
ORDER BY state;
Output
     city      | state |      time_zone
---------------+-------+---------------------
 San Francisco | CA    | America/Los_Angeles
 Dallas        | TX    | America/Chicago
(2 rows)

Its primary key is the pair city and state, because two states can have a city with the same name. Every student's city and state must match a row here. The time zones matter when a later lesson sends reminders at the right local time: Dallas is on Central time and San Francisco on Pacific time.

How the Tables Connect

Every connection is a foreign key: a column (or a pair of columns) whose values must match a key in another table. These are all of them, read from the database itself:

Child columnMust matchIn plain English
students.(city, state)cities.(city, state)Every student lives in a known city
students.mentor_idstudents.student_idA mentor is another student (or NULL)
enrollments.student_idstudents.student_idEvery enrollment is for a real student
enrollments.course_idcourses.course_idEvery enrollment is for a real course
courses.prerequisite_idcourses.course_idA prerequisite is another course (or NULL)
payments.(student_id, course_id)enrollments.(student_id, course_id)Every payment belongs to an enrollment
payments.student_idstudents.student_idAlso checked on its own
payments.course_idcourses.course_idAlso checked on its own

Here is what that looks like with real rows. Start from one payment and follow the matching values into every other table:

From payment 2 to its enrollment, student, course and city
mentor_id and prerequisite_id point back into their own tables

Deliberate Special Cases

A sample database where everything is neat teaches you very little. Real data has gaps, and many SQL features exist to handle them. The Fee Tracker includes a few on purpose, and later lessons come back to each one:

The gaps and quirks later lessons rely on
Special caseWhy it's there
Advitha has no paymentsFinding rows with no match (LEFT JOIN, NOT EXISTS)
Git Basics has no enrollmentsThe same idea from the course side (FULL JOIN)
Advitha and Navika have no email; Navika has no phoneHandling NULL (COALESCE, IS NULL)
Payment 5 has no methodNULL in a column you group by
Payments 3 and 7 were paid after their due dateComparing dates and labeling late payments (CASE)
Two payments of $250.00 and two of $225.00Ties when ranking (RANK and DENSE_RANK)

You can see the late payments already, with a WHERE that compares two date columns:

SQL▶ Try it
SELECT payment_id, due_on, paid_on
FROM payments
WHERE paid_on > due_on
ORDER BY payment_id;
Output
 payment_id |   due_on   |  paid_on
------------+------------+------------
          3 | 2025-12-01 | 2025-12-03
          7 | 2025-12-15 | 2025-12-19
(2 rows)

Resetting the Data

In Try it, there's nothing to reset. Every run starts from a fresh copy of the Fee Tracker, so you can insert, change and delete rows freely; the next run brings everything back.

On your own PostgreSQL, changes stay until you undo them. To start over, drop the database and load the script again, as in Setting Up SQL. Connect to postgres first, because PostgreSQL won't drop the database you're connected to:

text
feetracker=# \c postgres
postgres=# DROP DATABASE feetracker;
postgres=# CREATE DATABASE feetracker;
postgres=# \c feetracker
feetracker=# \i /Users/you/Downloads/fee-tracker.sql
Try it resets itself; your own copy is dropped and reloaded

Common Mistakes

Expecting an enrollment_id. Enrollments are identified by the pair student_id and course_id. Payments use the same pair to point at their enrollment.

Confusing fee_usd and amount_usd. courses.fee_usd is the current price of a course; payments.amount_usd is what one payment actually was. A course fee of $500.00 can be paid as two payments of $250.00.

Assuming every student has a payment, or every course has a student. Advitha has none, and Git Basics has none. Queries that join tables have to decide what to do with them.

Practice

Challenge: Who still has an active enrollment? List the student_id and course_id of every enrollment whose status is active, sorted by student_id and then course_id. How many are there?

Show solution
SQL▶ Try it
SELECT student_id, course_id
FROM enrollments
WHERE status = 'active'
ORDER BY student_id, course_id;
Output
 student_id | course_id
------------+-----------
        101 |         3
        101 |         4
        102 |         1
        102 |         4
        103 |         5
(5 rows)

Five enrollments are active. Srihitha's first two courses are completed, so they don't appear.

Quick check: The Fee Tracker database

3 questions
1Which table connects students to the courses they take?
2What identifies a row in enrollments?
3Which student has no payments, on purpose?

Key Takeaways

You want toLook in
A student's name, contact details or mentorstudents
A course's fee or prerequisitecourses
Who takes which courseenrollments
Who paid what, and whenpayments
A city's time zonecities

Was this tutorial helpful?

Lesson 8 of 115