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:
| Table | Rows | Contents |
|---|---|---|
| countries | 246 | countries and their borders |
| capitals | 192 | capitals |
| nyc_census_blocks | 38,794 | census blocks with population |
| nyc_neighborhoods | 129 | neighborhoods |
| nyc_streets | 19,091 | streets |
| nyc_subway_stations | 491 | subway stations |
| nyc_homicides | 3,982 | homicides |
Table structure
Click a table to see its columns, a sample row and its keys.
List of tables
- idunique record identifier (PK)
- namecountry name
- bordercountry geometry (MultiPolygon, SRID 4326)
| id | name | border |
|---|---|---|
| 1 | France | MultiPolygon(...) [SRID=4326] |
- PRIMARY KEY, btree (id)
- 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)
- 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)
- 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)
- 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)
- 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)
- 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;
| capital | country | lat | lon | inside_border |
|---|---|---|---|---|
| Abu Dhabi | United Arab Emirates | 24.30 | 54.70 | true |
| Abuja | Nigeria | 9.08 | 7.40 | true |
| Accra | Ghana | 5.60 | -0.19 | true |
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;
| station | borough | routes | srid |
|---|---|---|---|
| Cortlandt St | Manhattan | R,W | 26918 |
| Rector St | Manhattan | 1 | 26918 |
| South Ferry | Manhattan | 1 | 26918 |
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:
- Extract Geometry as Text
- Extract Geometry as JSON
- Distance between cities
- Country Area
- Manhattan Subway Stations
- Area of the Neighborhood
- Area of the Neighborhood
- Neighborhood Average Area
- Length of New York Streets
- Little Italy Stations
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.