Skip to content

Quickstart Query your data with SPARQL

New to Folio? Start here

Query your data with SPARQL

Folio answers questions about your data in SPARQL, the query language for knowledge graphs. You ask in the terms of your domain model, such as taxi zones and their pickups, and Folio reads the data files your mapping names to answer. One query can read several data files at once.

These examples use the NYC Taxi & Limousine Commission's (TLC) public trip data, which you can set up in Query data files.

The domain model is one class, Zone, with four properties:

The Zone card, a Class Schema, with its comment and four instance properties: average fare, a decimal, 0..1; borough, a string, 1..1; green pickups, an integer, 0..1; and name, a string, 1..1.
The Zone class with its four properties.

1. List the zones

A query describes the answer it wants with triple patterns. A triple pattern is a statement with a variable in it, a name written with a ?: ?zone a taxi:Zone matches each thing that is a zone. PREFIX gives https://example.org/taxi#, the namespace the Zone class is declared in, the short name taxi:. The answer has one row for each way the patterns match, and one column for each variable after SELECT.

ORDER BY sorts the rows, and LIMIT keeps the first few. Each query's SQL tab asks roughly the same question in SQL, of the data files directly.

zones.rq
PREFIX taxi: <https://example.org/taxi#>

# Select two columns: the zone's name and its borough.
SELECT ?name ?borough WHERE {
  # For each thing that is a zone
  ?zone a taxi:Zone ;
    # Select its name as name and borough as borough
    taxi:name ?name ;
    taxi:borough ?borough .
}
# Sort by name, A to Z
ORDER BY ?name
# Keep the first ten
LIMIT 10
One row for each of the first ten zones A to Z, with the borough it lies in. Folio reads the zone lookup CSV to answer.

2. Filter and sort

FILTER keeps only the rows that pass a test, and ORDER BY DESC sorts the rows largest first.

busy-zones.rq
PREFIX taxi: <https://example.org/taxi#>

# Select two columns: the zone and its pickups.
SELECT ?zone ?pickups WHERE {
  # For each zone, select its green pickups as pickups
  ?zone taxi:greenPickups ?pickups .
  # Keep only the zones with more than 2000 pickups
  FILTER(?pickups > 2000)
}
# Sort by pickups, most first
ORDER BY DESC(?pickups)
One row for each zone with more than 2000 green taxi pickups in January 2024, the most first. Folio reads the Parquet file to answer, and it knows each zone only by its number, so the zone column shows the last part of each zone's address, such as 74 for https://example.org/taxi/zone/74.

3. Join two data files

A join matches two patterns on the same variable. In this query, ?zone takes its pickups from the Parquet file and its name from the CSV file. Both maps build a zone's address from its zone number, so the two patterns match the same zone.

This query and the ones in steps 4 and 5 read the whole Parquet file, so they run with Allow full fetch selected.

busy-zone-names.rq
PREFIX taxi: <https://example.org/taxi#>

# Select two columns: the zone's name and its pickups.
SELECT ?name ?pickups WHERE {
  # For each zone, select its green pickups as pickups, from the Parquet file
  ?zone taxi:greenPickups ?pickups .
  # Select the same zone's name as name, from the CSV file
  ?zone taxi:name ?name .
  # Keep only the zones with more than 2000 pickups
  FILTER(?pickups > 2000)
}
# Sort by pickups, most first
ORDER BY DESC(?pickups)
The same seven zones, each by its name from the CSV file.

4. Keep the zones with no pickups

OPTIONAL keeps a row when its pattern matches nothing, and leaves its variable blank.

staten-island.rq
PREFIX taxi: <https://example.org/taxi#>

# Select two columns: the zone's name and its pickups, if any.
SELECT ?name ?pickups WHERE {
  # For each zone whose borough is Staten Island
  ?zone taxi:borough "Staten Island" ;
    # Select its name as name
    taxi:name ?name .
  # Select its green pickups as pickups, or leave pickups blank if it has none
  OPTIONAL { ?zone taxi:greenPickups ?pickups . }
}
# Sort by name, A to Z
ORDER BY ?name
# Keep the first ten
LIMIT 10
One row for each of the first ten Staten Island zones A to Z, with its green taxi pickups in January 2024. Most are blank: 16 of Staten Island's 20 zones had none. Without OPTIONAL, the query returns only the 4 zones with a pickup.

5. Count and add up

GROUP BY puts the rows that share a value into one row. COUNT and SUM then count the rows in each group and add up a value across them.

boroughs.rq
PREFIX taxi: <https://example.org/taxi#>

# Select three columns: the borough, its zones with pickups, and its pickups in all.
SELECT ?borough (COUNT(?zone) AS ?zones) (SUM(?pickups) AS ?totalPickups) WHERE {
  # For each zone, select its borough as borough, from the CSV file
  ?zone taxi:borough ?borough ;
    # and its green pickups as pickups, from the Parquet file
    taxi:greenPickups ?pickups .
}
# Group the rows by borough: count the zones and add up the pickups in each
GROUP BY ?borough
# Sort by total pickups, most first
ORDER BY DESC(?totalPickups)
One row for each borough: how many of its zones had a green taxi pickup in January 2024, and the pickups in all. Unknown and N/A are the boroughs TLC gives its two placeholder zones, for a trip it couldn't place and for one outside New York City.