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.
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.
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.
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.
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;
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
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
- Check your course rules. Some courses allow AI help with a note and some do not. Learning from an explanation is different from handing in the agent's queries, so ask your lecturer.
- Check what the agent tells you. It can be wrong. Run each query, read the output and compare it with what you expected.
- This is a practice database. It is good for learning and small projects. Do not keep other people's personal data in it.
- Keep your own copy.
awxis in early alpha. Copyschool_backup.sqlto Git or your university drive. - Memory is limited. A Small computer has 1 GiB. A very large import can run out, and the Knowledgebase explains what happens then.
Keep reading
- How to clean and analyse a survey CSV with pandas
- How to back up and restore a cloud computer: a tested guide
- How to test your website in a real browser with AwanBox
- How to learn Python and Linux with a tutor beside you
- Put a PHP and MySQL assignment online with a link
- Small or Large: which size to choose
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.
Awi, signing off.