New month, new goals. Your help keeps the project moving forward. 🖥️ Support sqltest →
SQL code copied to buffer

Countries database (PostGIS): spatial tables and SQL exercises

Countries is a PostGIS database for learning spatial SQL: world countries and capitals, plus New York City layers with census blocks, neighborhoods, streets and subway stations. On SQLtest.online you can query it right in your browser: solve exercises with automatic checking and run your own queries in the playground, with nothing to install.

  • 7 spatial tables
  • 246 countries
  • 491 subway stations
  • 11 SQL exercises

What is Countries

PostGIS is the PostgreSQL extension that adds geometry types and hundreds of spatial functions: distances, areas, intersections, coordinate transformations. This database lets you try them on familiar data.

The New York City tables come from the well-known PostGIS workshop "Introduction to PostGIS", and the world tables hold country borders and capitals. Together they cover points, lines and polygons in two coordinate systems.

What's inside

The tables fall into two groups.

World

countries with border polygons and capitals with points, both in SRID 4326 (longitude and latitude).

New York City

nyc_census_blocks, nyc_neighborhoods, nyc_streets, nyc_subway_stations and nyc_homicides, in SRID 26918 (UTM zone 18N, meters).

The key thing to remember: the world tables store degrees, the New York tables store meters. Distances and areas in the New York layers come out in meters directly; for the world tables, cast to geography or transform the geometry first.

How much data the tables hold:

TableRowsContents
countries246countries and their borders
capitals192capitals
nyc_census_blocks38,794census blocks with population
nyc_neighborhoods129neighborhoods
nyc_streets19,091streets
nyc_subway_stations491subway stations
nyc_homicides3,982homicides

Table structure

Click a table to see its columns, a sample row and its keys.

List of tables

countries - list of countries with geometry.
  • idunique record identifier (PK)
  • namecountry name
  • bordercountry geometry (MultiPolygon, SRID 4326)
id name border
1 France MultiPolygon(...) [SRID=4326]
  • PRIMARY KEY, btree (id)
capitals - list of capitals with location.
  • idunique record identifier (PK)
  • namecapital name
  • country_idreference to country (FK)
  • locationcapital location (Point, SRID 4326)
id name country_id location
1 Paris 1 Point(...) [SRID=4326]
  • PRIMARY KEY, btree (id)
  • FOREIGN KEY (country_id) REFERENCES countries(id)
nyc_census_blocks - New York City census blocks with demographic data.
  • gidunique record identifier (PK)
  • blkidcensus block ID
  • popn_totaltotal population
  • popn_whitewhite population
  • popn_blackblack population
  • popn_nativnative population
  • popn_asianasian population
  • popn_otherother population
  • boronameborough name
  • geomcensus block geometry (MultiPolygon, SRID 4326)
gid blkid popn_total popn_white popn_black popn_nativ popn_asian popn_other boroname geom
1 360050001001000 1000 500 200 50 150 100 Manhattan MultiPolygon(...) [SRID=4326]
  • PRIMARY KEY, btree (gid)
nyc_homicides - New York City homicide incidents.
  • gidunique record identifier (PK)
  • incident_dincident date
  • boronameborough name
  • num_victimnumber of victims
  • primary_moprimary motive
  • idincident ID
  • weaponweapon used
  • light_darklight or dark condition
  • yearyear of incident
  • geomincident location (Point, SRID 4326)
gid incident_d boroname num_victim primary_mo id weapon light_dark year geom
1 2003-01-01 Manhattan 1 Unknown 1 Firearm D 2003 Point(...) [SRID=4326]
  • PRIMARY KEY, btree (gid)
nyc_neighborhoods - New York City neighborhoods.
  • gidunique record identifier (PK)
  • boronameborough name
  • nameneighborhood name
  • geomneighborhood geometry (MultiPolygon, SRID 4326)
gid boroname name geom
1 Manhattan Financial District MultiPolygon(...) [SRID=4326]
  • PRIMARY KEY, btree (gid)
nyc_streets - New York City streets.
  • gidunique record identifier (PK)
  • idstreet ID
  • namestreet name
  • onewayone-way indicator
  • typestreet type
  • geomstreet geometry (LineString, SRID 4326)
gid id name oneway type geom
1 1 Broadway NO avenue LineString(...) [SRID=4326]
  • PRIMARY KEY, btree (gid)
nyc_subway_stations - New York City subway stations.
  • gidunique record identifier (PK)
  • objectidobject ID
  • idstation ID
  • namestation name
  • alt_namealternative name
  • cross_stcross street
  • long_namelong name
  • labellabel
  • boroughborough
  • nghbhdneighborhood
  • routesroutes
  • transferstransfers
  • colorcolor
  • expressexpress indicator
  • closedclosed indicator
  • geomstation location (Point, SRID 4326)
gid objectid id name alt_name cross_st long_name label borough nghbhd routes transfers color express closed geom
1 1 1 Times Square Times Sq 7th Ave Times Square-42nd Street Times Sq Manhattan Midtown 1,2,3,7,A,C,E,N,Q,R,S,W 42nd St Red Yes No Point(...) [SRID=4326]
  • PRIMARY KEY, btree (gid)

Sample queries

These queries show how the data is connected. Copy any of them and run it in the playground.

A capital in its country: point coordinates with ST_X / ST_Y and a spatial check with ST_Contains.

SELECT c.name AS capital, co.name AS country,
       round(ST_Y(c.location)::numeric, 2) AS lat,
       round(ST_X(c.location)::numeric, 2) AS lon,
       ST_Contains(co.border, c.location) AS inside_border
FROM capitals c
JOIN countries co ON co.id = c.country_id
ORDER BY c.name
LIMIT 3;
capitalcountrylatloninside_border
Abu DhabiUnited Arab Emirates24.3054.70true
AbujaNigeria9.087.40true
AccraGhana5.60-0.19true

Subway stations and their SRID: New York layers use the projected system 26918.

SELECT s.name AS station, s.borough, s.routes, ST_SRID(s.geom) AS srid
FROM nyc_subway_stations s
ORDER BY s.gid
LIMIT 3;
stationboroughroutessrid
Cortlandt StManhattanR,W26918
Rector StManhattan126918
South FerryManhattan126918

SQL exercises by topic

There are 11 PostGIS exercises on this database: distances, areas, lengths, conversions to text and JSON, and spatial joins. Solutions are checked automatically on a real PostgreSQL with PostGIS. The number on the right is how many exercises a topic has; the colored dots show its difficulty range.

Where to start

The first exercises on the Countries database:

  1. Extract Geometry as Text
  2. Extract Geometry as JSON
  3. Distance between cities
  4. Country Area
  5. Manhattan Subway Stations
  6. Area of ​​the Neighborhood
  7. Area of ​​the Neighborhood
  8. Neighborhood Average Area
  9. Length of New York Streets
  10. Little Italy Stations

All Countries exercises →

FAQ

Do I need to install PostGIS to use this database?

No. The exercises and the playground on SQLtest.online run your queries on our servers, so a browser is all you need. In the playground, choose PostgreSQL 17 + PostGIS WorkShop.

What is an SRID?

A spatial reference identifier: it says which coordinate system the coordinates are in. 4326 is longitude and latitude in degrees (WGS 84); 26918 is UTM zone 18N in meters, used for New York.

Where do the New York tables come from?

From the data set of the "Introduction to PostGIS" workshop published on postgis.net, a common starting point for learning PostGIS.