This repository contains examples for working with databases and writing queries. All examples are written for PostgreSQL and are Docker-based for easy setup. You can also try pgcli for a better PostgreSQL experience.
Each project is self-contained with its own docker-compose.yml. To run any example:
cd <project-directory>
docker-compose up -dConnect to the database:
# Using psql
psql -h localhost -U postgres -d pgsql
# Or using pgcli (recommended)
pgcli -h localhost -U postgres -d pgsqlDefault credentials for all projects: postgres / postgres
Difficulty: Beginner
A basic example showing how to write SQL files and mount them into PostgreSQL Docker containers.
Concepts covered:
- Basic table creation with
CREATE TABLE - Data types:
serial,text,timestamp - Primary keys and
NOT NULLconstraints - Default values with
DEFAULT now() - Docker volume mounting for SQL initialization
Difficulty: Intermediate
A ride-sharing system model (similar to Snapp/Uber) with drivers, passengers, and ride tracking.
Concepts covered:
- Custom enumeration types with
CREATE TYPE ... AS ENUM - Foreign key relationships with
REFERENCES - Event sourcing pattern for ride lifecycle
- Subqueries with
EXISTSandALL - Pattern matching with
LIKE ANYoperator for array/subquery comparisons
Schema:
passengers (id, first_name, last_name, national_id)
drivers (id, first_name, last_name, national_id)
rides (id, passenger_id, driver_id, event_name, created_at)
Example queries:
- Find passengers who always had the same driver
- Find passengers with cancelled rides matching a name pattern
Difficulty: Intermediate
An example demonstrating PostgreSQL's role-based access control system.
Concepts covered:
- Role creation with
CREATE ROLE - Role attributes:
LOGIN,SUPERUSER,CREATEROLE - Password management and expiration with
VALID UNTIL - Role inheritance with
GRANT ... TO - Permission management with
GRANT SELECT - Role modification with
ALTER ROLE - Row-level security (RLS) with
ENABLE ROW LEVEL SECURITY - Security policies with
CREATE POLICY - Bypassing RLS with
BYPASSRLS
Difficulty: Beginner
An example for schema evolution and database views.
Concepts covered:
- Basic table creation
- Schema evolution with
ALTER TABLE ADD COLUMN - Conditional modifications with
IF EXISTS/IF NOT EXISTS - Creating views with
CREATE VIEW - Data abstraction through views
Difficulty: Intermediate
An example demonstrating PostgreSQL array data types for flexible data modeling.
Concepts covered:
- Array columns with
varchar[]anddouble precision[] - Array initialization with
'{}'::varchar[] - Array manipulation with
array_append() - Array membership checking with
ANY() - Many-to-many relationships using arrays (denormalized approach)
Schema:
tags (id, name, created_at, updated_at)
rooms (id, tag_ids[], coordinates[], created_at, updated_at)
Difficulty: Advanced
A comprehensive example using the DVD rental sample database for advanced reporting and procedural programming.
Concepts covered:
Stored Procedures & Functions:
CREATE PROCEDUREwith PL/pgSQLCREATE FUNCTIONwithRETURNS TABLE- Conditional logic with
IF/ELSIF RAISE INFOfor debugging output- Calling functions within functions
Triggers:
CREATE TRIGGERon INSERT/UPDATE- Trigger functions with
RETURNS TRIGGER - Accessing
NEWrecord in triggers - Audit logging patterns
Advanced Reporting:
ROLLUPfor hierarchical aggregationsCUBEfor multi-dimensional aggregationscrosstab()for pivot tables (requirestablefuncextension)- Window functions with
RANK() OVER (PARTITION BY ... ORDER BY ...) - Date extraction with
EXTRACT() - Subqueries in FROM clause
Setup: This project requires downloading the DVD rental sample database. Run ./data-source.sh first.
Difficulty: Intermediate
A large sample database for books and publishing, sourced from sample-db.net.
Concepts covered:
- Working with large, realistic datasets
- Complex multi-table schemas
- Exercise queries for practice
- Geographic data (countries, towns)
Included SQL files:
books-pgsql-10-script.sql- Main schema and databooks-pgsql-10-countries.sql- Countries reference databooks-pgsql-10-towns.sql- Towns reference databooks-pgsql-10-exercises.sql- Practice queries
| Project | Key Topics | Difficulty |
|---|---|---|
| psql-users | Basic DDL, Docker setup | Beginner |
| psql-persons | ALTER TABLE, Views | Beginner |
| psql-tags | Arrays, array_append, ANY | Intermediate |
| psql-rides | Enums, Foreign Keys, Subqueries | Intermediate |
| psql-roles | RBAC, RLS, Policies | Intermediate |
| psql-bookworks | Large datasets, Practice | Intermediate |
| psql-movie | Procedures, Triggers, OLAP | Advanced |
| Feature | Project |
|---|---|
| CREATE TABLE | psql-users, psql-persons |
| ALTER TABLE | psql-persons |
| CREATE VIEW | psql-persons |
| CREATE TYPE (ENUM) | psql-rides |
| Foreign Keys | psql-rides |
| Arrays | psql-tags |
| Subqueries (EXISTS, ALL, ANY) | psql-rides |
| Roles & Permissions | psql-roles |
| Row-Level Security | psql-roles |
| Stored Procedures | psql-movie |
| User-Defined Functions | psql-movie |
| Triggers | psql-movie |
| ROLLUP / CUBE | psql-movie |
| Window Functions | psql-movie |
| crosstab (Pivot) | psql-movie |