awx

← Blog

For students · 17 November 2026

How to practise SQL with PostgreSQL: SELECT, JOIN, GROUP BY

A school database you can break, set up in a few minutes. You type the queries, the agent explains them.

A database course asks you to write SQL on day one, and the first problem is not SQL. It is getting a database server running, creating tables, and finding some data worth asking questions about. Many people lose the first evening to that.

An awx computer already has PostgreSQL 16 on it. This post follows a real run: we asked the agent to set up a school database, then typed the lessons ourselves in the terminal.

Awi the cat agent in a graduation cap

The first message

In Workbench we pressed New chat, chose On a computer I already have and picked a Small computer. Then we sent this:

I am a first-year student and my database course starts this
week. Please set up PostgreSQL on this computer and make a
database called school with three tables: students, courses
and enrolments. Add about 15 students, 5 courses and some
enrolments with grades. Then teach me, one small step at a
time, how to list the students, join them to their courses,
and find the average grade of each course. Tell me exactly
what to type in the terminal.

This message lets the agent do the setup, which is the boring part. If you would rather type the setup too, say so and ask it to explain each statement before you run it.

What the agent set up

The agent checked the computer first. PostgreSQL 16 was installed but switched off, and there is no system service manager inside the computer, so it started the server by hand, made a database login for the user agent (no password needed from the terminal), and created the database. It wrote its code to a file, school_setup.sql, so we could read what it had made.

The awx workbench: on the left the agent lists what it set up, 15 students, 5 courses and 42 enrolments, and the first lesson; on the right an empty terminal
The agent's summary: 15 students, 5 courses, 42 enrolments, three files it saved, and Step 1. The terminal on the right is empty and waiting for us.

Three of the 42 enrolments have no grade yet, on purpose. That turns out to matter in lesson 2.

Lesson 1: SELECT

We typed, in the Terminal tab on the right:

psql -d school

psql is the terminal client for PostgreSQL, and -d school means "connect to the database called school". The prompt changed to school=>. Then the first query:

SELECT * FROM students;

Read it as "show all the columns of the table students". The semicolon at the end matters: SQL only runs when it sees one.

The terminal after psql -d school and SELECT * FROM students: 15 rows with student_id, first_name, last_name and email
Fifteen students, with an id, a name and an email each.

Lesson 2: JOIN

The grades are not in the students table. They are in enrolments, a table that holds a student_id and a course_id in each row, which links one student to one course. To see names next to course titles, you join the three tables on those ids:

SELECT s.first_name, c.course_name, e.grade
FROM enrolments e
JOIN students s ON s.student_id = e.student_id
JOIN courses c ON c.course_id = e.course_id
ORDER BY e.grade DESC
LIMIT 8;

e, s and c are short names for the tables, so the query is easier to read. ON says which columns must match. LIMIT 8 keeps the first eight rows.

The result of the join: the first three rows have an empty grade, then Grace 97, Chloe 95, Grace 94, Chloe 92 and Maya 91
We asked for the highest grades first, and the first three rows are empty.

We did not expect that. The three students with no grade came out on top. In PostgreSQL a missing value, called NULL, sorts as bigger than every number, so a descending sort puts it first. The fix is to say where the empty ones go:

ORDER BY e.grade DESC NULLS LAST

With that change the same query starts with Grace's 97, then Chloe's 95. A result that looks wrong at first is a good place to learn what NULL means, and a good thing to ask the agent about.

Lesson 3: GROUP BY

To get one row per course instead of one row per enrolment, group the rows and summarise each group:

SELECT c.course_name,
       ROUND(AVG(e.grade), 1) AS average_grade,
       COUNT(*) AS enrolled
FROM courses c
JOIN enrolments e ON e.course_id = c.course_id
GROUP BY c.course_name
ORDER BY average_grade DESC;
The top five grades with NULLS LAST, then the average grade and number enrolled for each of the five courses; Web Development is first with 83.3
The fixed sort at the top, then one row for each course. Web Development has the best average, 83.3.

Look at the enrolled column: 9, 8, 8, 10 and 7 add up to 42, so it counts every enrolment, graded or not. AVG skips the empty grades. The same table gives two different numbers depending on the function, which is a common source of marks lost in a first SQL assignment.

Lesson 4: ask the database how it works

Put EXPLAIN ANALYZE in front of a query and PostgreSQL runs it and prints how:

EXPLAIN ANALYZE
SELECT s.first_name, c.course_name, e.grade
FROM enrolments e
JOIN students s ON s.student_id = e.student_id
JOIN courses c ON c.course_id = e.course_id
WHERE e.grade > 90;

On our data the plan used two hash joins and three sequential scans, which means it read each table from the top. It removed 36 enrolments that did not pass the filter, returned 6, and took 0.072 ms to run. Notice that the planner had guessed 617 rows for the filter and the real answer was 6. On a table this small the guess does not matter. On a big table, wrong guesses are why a query is slow, and this is the first place to look.

The output is wider than the terminal, so ask the agent to explain it line by line, or press q to leave the pager.

Keep your work

Leave psql with \q. To save the whole database as one text file:

pg_dump school > school_backup.sql
The terminal after pg_dump: school_backup.sql is 7.1K, next to school_setup.sql at 3.1K and start-postgres.sh at 124 bytes, and the first lines of the dump
The backup is a 7.1 KB text file of SQL. You can read it, send it to a friend, or load it into another database with psql -d newdb -f school_backup.sql.

We then stopped the computer and started it again to see what survives. The files were all there and all 42 enrolments were still in the database. The server itself was off, so the first psql said Connection refused. There is no service manager to start PostgreSQL on its own, so the agent had left a one-line script for this, and running sudo /home/agent/start-postgres.sh brought it back. If you see that error after the computer has slept, that is the reason.

What it cost

We ran this on a Small computer, which costs RM0.03 an hour while it runs and nothing while it sleeps. The whole session took well under an hour, so it came to a few sen of the RM5 welcome credit. How running time and credit work has the details.

Cautions

Keep reading

Getting started

Sign in with your Google account and open Workbench. Start a chat on a Small computer and paste the first message from this post.

Open the workbench

Awi the cat agent celebrates with confetti

Awi, signing off.

← All posts