Install the PostgreSQL-compatible version of PgExercises on the YugabyteDB distributed SQL database. Work through 81 exercises to learn SQL or test your knowledge.
You can install and use the PgExercises database using:
- A local installation of YugabyteDB. To install YugabyteDB, refer to Quick Start.
- Using cloud shell or a client shell to connect to a cluster in YugabyteDB Managed. Refer to Connect to clusters in YugabyteDB Managed. To get started with YugabyteDB Managed, refer to Quick Start.
In either case, you use the YugabyteDB SQL shell (ysqlsh) CLI to interact with YugabyteDB using YSQL.
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.
exercises database consists of three tables (for members, bookings, and facilities) and the table relationships as shown in the entity relationship diagram.
Install the PgExercises sample database
The PGExercise SQL scripts reside in the
share folder of your YugabyteDB or client shell installation. They can also be found in the
sample directory of the YugabyteDB GitHub repository. The following files will be used for this exercise:
- clubdata_ddl.sql — Creates the tables and other database objects
- clubdata_data.sql — Loads the sample data
Follow the steps here to install the PgExercises sample database.
Open the YSQL shell
If you are using a local installation of YugabyteDB, run the
ysqlsh command from the
yugabyte root directory.
If you are connecting to YugabyteDB Managed, open the ysqlsh cloud shell, or run the YSQL connection string for your cluster from the
yugabyte-client bin directory.
Create the PgExercises database
To create the
exercises database, run the following SQL
CREATE DATABASE command.
CREATE DATABASE exercises;
Confirm that you have the
exercises database by listing the databases on your cluster using the
Connect to the
yugabyte=# \c exercises
You are now connected to database "exercises" as user "yugabyte". exercises=#
Build the PgExercises tables and objects
To build the tables and database objects, run the
exercises=# \i share/clubdata_ddl.sql
You can verify that all three tables have been created by running the
Load the sample data
To load the
exercises database with sample data, run the following command to execute commands in the file.
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
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.
- Simple SQL Queries
- JOINs and Subqueries
- Modifying Data
- Working with Timestamps
- String Operations
- Recursive Queries
YugabyteDB returns the same results as expected based on the solutions on the PostgreSQL Exercises website, with one exception. In the "Find telephone numbers with parentheses" exercise, YugabyteDB returns results with a sort order of strings different than in PostgreSQL due to hash sharding in YugabyteDB.