Postgres Lab Starting database… Playground

Learn PostgreSQL by running it.

27 free lessons, from your first SELECT to window functions, JSONB, indexes and triggers. Every example runs on a real PostgreSQL 16 database inside this page, and every exercise is checked against it.

shopco=# SELECT n, title, part FROM course.lessons ORDER BY n;

Course contents
ntitlepart
1How this course worksStart here
2Databases, tables and SQLStart here
3SELECT: choosing columnsQuerying data
4WHERE: filtering rowsQuerying data
5Sorting, limiting and DISTINCTQuerying data
6Aggregates, GROUP BY and HAVINGQuerying data
7JOINs: combining tablesCombining data
8SubqueriesCombining data
9UNION, INTERSECT, EXCEPTCombining data
10CASE, COALESCE and NULLIFTransforming data
11Text, number and type functionsTransforming data
12Dates, times and intervalsTransforming data
13CTEs and recursive queriesAdvanced queries
14Window functionsAdvanced queries
15Data typesBuilding the database
16CREATE, ALTER and constraintsBuilding the database
17INSERT, UPDATE, DELETE and UPSERTBuilding the database
18TransactionsBuilding the database
19JSON and JSONBPostgreSQL superpowers
20ArraysPostgreSQL superpowers
21Regular expressions and full-text searchPostgreSQL superpowers
22Views and materialized viewsPostgreSQL superpowers
23Indexes and EXPLAINPerformance
24Functions, procedures and PL/pgSQLProgramming PostgreSQL
25TriggersProgramming PostgreSQL
26Roles, privileges and row-level securityProgramming PostgreSQL
27Challenge setPractice
(27 rows)

This contents page is a real table. Run the same query in the playground, or filter it: WHERE level = 'beginner'.

How this course works

This site runs a real PostgreSQL 16 server inside your browser tab. Every code box is editable, and the Run button (or Ctrl/⌘ + Enter) sends your SQL to that server and shows exactly what PostgreSQL returns, including its error messages.

You work with one small shop database called shopco. It has six tables: departments, employees, customers, products, orders and order_items. Open Tables in the top bar at any time to see their columns. The data is deliberately imperfect (a customer with no city, a product nobody bought, an employee with no department) so you can see how NULLs and joins behave in real life.

Changes you make last while you move between lessons, until you reload the page or press Reset database. So go ahead and delete things; you can always put them back.

How each lesson is built

  • Concept: what the feature is and when you need it.
  • Syntax: the general shape of the command.
  • Examples: runnable queries, each followed by a line-by-line explanation.
  • Exercises: write your own query and press Check answer. The checker compares your result with the expected result, so any correct query passes.

Installing PostgreSQL on your own machine

When you want to practise outside this site: on Windows and macOS use the installer from postgresql.org (or Postgres.app on Mac); on Ubuntu run sudo apt install postgresql; or use Docker with docker run -e POSTGRES_PASSWORD=secret -p 5432:5432 postgres:16. Connect with the command-line client psql -h localhost -U postgres, or a GUI such as pgAdmin or DBeaver. Everything you learn here works the same there.

Syntax

SELECT what_you_want
FROM which_table;

Examples

Edit the code and run it. Output is printed the way psql prints it.

1.1Your first query

SELECT 'Hello, PostgreSQL!' AS greeting;

SELECT asks PostgreSQL to return values. Here there is no table at all, just a text value in single quotes. AS greeting names the output column. Every statement ends with a semicolon.

1.2Look at a table

SELECT *
FROM products
LIMIT 5;
  • SELECT * means “all columns”.
  • FROM products picks the table.
  • LIMIT 5 returns only the first five rows, which is a good habit when exploring a table you don’t know yet.

Exercises

Write a query and check it. Any query that returns the right rows passes.

Exercise 1.1

Show every row and every column of the departments table.
Hint

Use SELECT * with FROM.

Solution
SELECT * FROM departments;

Questions

Is this PostgreSQL course free?

Yes. All 27 lessons, the exercises and the playground are free, with no sign-up.

Do I need to install PostgreSQL?

No. A real PostgreSQL 16 server runs inside your browser using WebAssembly, so every example runs instantly. When you are ready, the first lesson explains how to install PostgreSQL on Windows, macOS, Linux or Docker.

Who is this course for?

Beginners who have never written SQL, and developers who know basic SQL and want PostgreSQL features such as window functions, JSONB, indexes, PL/pgSQL and row-level security.

How are the exercises checked?

Your query runs on the live database and its result is compared with the expected result, so any correct query passes, not only one exact answer.

Is my data saved?

Changes to the practice database last while you move between lessons and are cleared when you reload the page or press Reset database. Your lesson progress is remembered in your browser.