PgExercises sample database

Download and install the PostgreSQL-compatible version of PgExercises on the YugabyteDB distributed SQL database. Work through 81 exercises to learn SQL or test your knowledge.

About the PgExercises sample database

The PgExercises sample database is based on the sample dataset used for the PostgreSQL Exercises tutorial website. The dataset is for a new country club, with a set of members, facilities, and booking history.

The PostgreSQL Exercises website includes 81 exercises designed to be used as a companion to the official PostgreSQL documentation. The exercises on the PgExercises site range from simple SELECT statements and WHERE clauses, through JOINs and CASE statements, then on to aggregations, window functions, and recursive queries.

For further details about the data, see the PostgresSQL Exercises’ Getting Started page.

The exercises database consists of three tables (for members, bookings, and facilities) and the table relationships as shown in the entity relationship diagram.

PgExercises ER diagram

Install the PgExercises sample database

Follow the steps here to download and install the PgExercises sample database.

Before you begin

To use the PgExercises sample database, you must have installed and configured YugabyteDB. To get up and running quickly, see Quick Start.

1. Download the SQL scripts

You can download the PGExercise SQL scripts that is compatible with YugabyteDB from the sample directory of the YugabyteDB GitHub repository.

Here are the two files you’ll need.

2. Open the YSQL shell

To open the YSQL shell, run the ysqlsh command from the YugabyteDB root directory.

  1. $ ./bin/ysqlsh
  1. ysqlsh (11.2)
  2. Type "help" for help.
  3. yugabyte=#

3. Create the PgExercises database

To create the exercises database, run the following SQL CREATE DATABASE command.

  1. CREATE DATABASE exercises;

Confirm that you have the exercises database by listing the databases on your cluster using the \l command.

  1. yugabyte=# \l

Connect to the exercises database.

  1. yugabyte=# \c exercises
  1. You are now connected to database "exercises" as user "yugabyte".
  2. exercises=#

4. Build the PgExercises tables and objects

To build the tables and database objects, run the \i command.

  1. exercises=# \i share/clubdata_ddl.sql

You can verify that all three tables have been created by running the \d command.

  1. exercises=# \d

5. Load the sample data

To load the exercises database with sample data, run the following command to execute commands in the file.

  1. exercises=# \i share/clubdata_data.sql

You can verify that you have data to work with by running the following SELECT statement to pull data from the bookings table.

  1. exercises=# SELECT * FROM bookings LIMIT 5;

Explore the PgExercises database

You are now ready to start working through the PostgreSQL Exercises exercises using YugabyteDB as the backend. The 81 exercises at the PostgreSQL Exercises website are broken into the following major sections.

YugabyteDB returns the same results as expected based on the solutions on the PostgreSQL Exercises website, with the following exceptions.