Querying Wikidata, IMDB, dblp and more with GQL
This service answers queries in GQL, the ISO standard Graph Query Language (ISO/IEC 39075). The queries run on butterfly, a graph query engine that is specialised in path queries.
The service holds four property graphs, each in its own database:
Wikidata, the IMDB movie database,
dblp and the Mathematics Genealogy Project, and the
German rail timetable. Choose the database with the selection next to the
Examples button; a query runs on the selected database only. The choice is kept in the
address (?db=gtfs), so links to a query include it, and choosing an example selects
its database.
Wikidata
Items such asQ42 (Douglas Adams), connected by Wikidata properties such as
P31 (instance of). Identified by their id: (:Q42).
IMDB
Nodes with the labelsPerson, Title, Movie, …
connected by edges such as ActedIn. Identified by their properties:
(:Person {name: 'Keanu Reeves'}).
dblp + Math Genealogy
Authors and their publications (Authored), and who supervised whose doctorate
(AdvisedBy), linked where an author has an entry in both:
(:Person {name: 'Wim Martens'}), (:Mathematician {name: 'Carl Friedrich Gauß'}).
German rail timetable
Every departure and arrival of a train on one day, connected by riding, staying on board and changing trains (Transfer, with the waiting time):
(:Station {name: 'Bayreuth Hbf'}).
Your first query
A GQL query describes a pattern of nodes and edges in MATCH and says what to output in RETURN:
MATCH (cat)-[:P31]->(:Q146)
RETURN cat, cat.label AS catLabel
LIMIT 100
This query finds every node cat with an edge labelled P31 (instance of) to the
node Q146 (house cat). It returns the node and its label. Write the query in the editor and press
the run button or Ctrl+Enter.
The same works for IMDB, once IMDB is selected as the database:
MATCH (actor:Person)-[role:ActedIn]->(:Movie {title: 'The Matrix'})
RETURN actor.name AS actor, role.character AS character
MATCH, WHERE and RETURN must be written in
upper case. Labels and property names are case sensitive too.
Datasets
Wikidata
The Wikidata subgraph is built from a Wikidata JSON dump.
| Wikidata | Property graph | GQL |
|---|---|---|
| Item, property or lexeme | A node. Its identity is the id, there is no separate id property. | (:Q42), (x:Q42) |
Statement (best rank only, like the wdt: prefix in SPARQL) |
An edge whose label is the property id | (x)-[:P31]->(y) |
| Qualifier | A property of the edge, keyed by the qualifier's property id | -[e:P39]-> … e.P580 |
| Label | The node property label. It holds one label per item, in English if there is one. |
x.label |
| "no value" statement | A node label wdno:P… instead of an edge |
Descriptions, aliases and sitelinks are not part of the graph. References are not loaded.
Because an item is identified by its id label, WHERE x = 'Q42' does not work:
write (x:Q42) instead.
The target of an edge is another item or a value. Values are returned as follows:
| Wikidata type | Result | Shown as |
|---|---|---|
| Item, property, lexeme | Wikidata entity URI | Link to Wikidata, with an explore button |
| Globe coordinate | WKT point Point(lon lat) | Map view |
| Time | Date and time (xsd:dateTime), without precision | Date, Timeline view |
| Quantity | Decimal number, without its unit | Number, charts |
| Commons media | URL of the file on Wikimedia Commons | Image grid view |
| Monolingual text | Text with a language tag | Text |
| URL | URI | Link |
| String, external identifier | Text | Text |
| Unknown value ("some value") | Blank node |
To find the id of an item or property, type its name after a colon in the editor and press Ctrl+Space, see Editor. You can also search on wikidata.org.
IMDB
The IMDB subgraph is built from the IMDB
non-commercial datasets. Every node has the label Person, Title or AkaTitle.
Titles have a second label for their type.
| Node label | Properties |
|---|---|
Person |
id (e.g. 'nm0000206'), name, birthYear, deathYear,
profession (several values)
|
Title, together with one of
Movie, Short, TVSeries, TVEpisode, TVMovie,
TVMiniSeries, TVSpecial, TVShort, TVPilot,
Video, VideoGame
|
id (e.g. 'tt0133093'), titleType, title, originalTitle,
isAdult, startYear, endYear, runtime (minutes),
genre (several values), rating, votes
|
AkaTitle (title of a release in a region or language) |
title, region, language, ordering,
type, attribute, isOriginalTitle
|
| Edge label | From → to | Edge properties |
|---|---|---|
ActedIn, Produced, Composed, Edited, Filmed,
Designed, Cast, ArchiveFootage, ArchiveSound,
AppearedIn (as themselves), WorkedOn (any other job)
|
Person → Title | ordering (billing order), job, character |
Directed, Wrote | Person → Title | |
KnownFor | Person → Title | |
EpisodeOf | TVEpisode → series | seasonNumber, episodeNumber |
HasAka | Title → AkaTitle |
IMDB nodes are returned as their IMDB id, e.g. tt0133093. To show names and titles, return
their properties: RETURN p.name AS name.
dblp and the Mathematics Genealogy Project
This database holds the computer science bibliography dblp (CC0) and the Mathematics Genealogy Project (MGP), which records who supervised whose doctorate, in one graph. About 11,000 people in dblp link to their MGP entry, so a path can lead from co-authors to academic ancestors.
| Node label | Properties |
|---|---|
Person (dblp) |
pid (dblp id, e.g. '95/3517'), name, alias (several values),
affiliation, orcid
|
Publication (dblp), together with one of Article, InProceedings,
Proceedings, Book, InCollection, PhdThesis,
MastersThesis, Data
|
key, title, year, venue, venueKey
(e.g. 'conf/semweb' for ISWC and its workshops), doi
|
Mathematician (MGP) |
id, name, givenName, otherNames, familyName,
mrAuthorId, descendants, and of the first doctorate degree,
year, school, country, msc, subject, thesis
|
Degree, School, Country (MGP) |
type, year, title, msc, subject,
school, country; name, country; name
|
| Edge label | From → to | Edge properties |
|---|---|---|
Authored, Edited | Person → Publication | position (1 = first author) |
MathGenealogy | Person → Mathematician (the same person in the MGP) | |
AdvisedBy | Mathematician → Mathematician (student to advisor) | year, degree, school, country |
Earned, Advisor | Mathematician → Degree, Degree → Mathematician | |
GrantedBy, In | Degree → School, School → Country |
Names are spelled as on mathgenealogy.org and dblp.org, e.g. 'Domagoj Vrgoc'; people in dblp
with the same name carry a number, e.g. 'Wei Wang 0001'. On a graph of this size, leave labels
out of a Kleene star and filter the publications: a co-author chain through ISWC papers,
( ()-[:Authored]->(p WHERE p.venueKey = 'conf/semweb')<-[:Authored]-() )*, takes about a second,
one through all of dblp about two minutes.
German rail timetable (GTFS)
The timetable of all trains in Germany (long-distance, regional and S-Bahn) on one day, Tuesday 27 October 2026, from the gtfs.de feed of DELFI e.V. (Creative Commons 4.0). It is a time-expanded graph: every departure and arrival of a train at a stop is a node, so every path through it is possible in time, and the waiting time of each change of trains is a property of its edge.
| Node label | Properties |
|---|---|
Station | id, name (e.g. 'Nürnberg Hbf'), lat, lon |
Stop (a stop of a station) | id, name, platform, lat, lon |
Route (a line) | id, line (e.g. 'RE38', 'ICE 25'), name, mode, agency |
Dep, Arr (a departure or arrival of a train at a stop) |
time (815 for 08:15, above 2400 after midnight), clock
('08:15'), line, mode ('LongDistance',
'Regional' or 'Suburban'), station, platform;
headsign on Dep
|
| Edge label | From → to | Edge properties |
|---|---|---|
Board, Alight | Stop → Dep, Arr → Stop | |
Ride | Dep → Arr at the next stop | minutes |
Stay | Arr → Dep of the same train (staying on board) | minutes |
Transfer | Arr → Dep of another train (a change) | wait, walk (minutes) |
PartOf, Footpath, Serves | Stop → Station, Stop → Stop, Route → Stop | walk on Footpath |
A trip is the pattern Board Ride (Stay Ride)* (Transfer Ride (Stay Ride)*)* Alight, with one
Kleene star for the legs of the trip and one inside it for the stops of a leg. A change leads to the first
departure of each line and direction that can be reached, after at least 4 minutes. SHORTEST
finds the trip with the fewest stops and changes, not the one that arrives first: ANY SHORTEST
returns one of several equally short trips, which may differ from run to run, and ALL SHORTEST
lists them all.
GQL syntax
butterfly implements the read-only part of GQL that is about pattern matching, with a focus on paths. This section describes what is supported. See Limitations for what is not.
Query structure
A query is a sequence of statements, ending with RETURN:
MATCH <pattern>, <pattern> [WHERE <condition>]
[OPTIONAL MATCH <pattern> …]
[MATCH …]
RETURN <expression> [AS <name>], …
[ORDER BY <expression> [ASC | DESC], …]
[SKIP <n>] [LIMIT <n>]
Patterns that share a variable are joined, whether they are separated by commas or in different
MATCH statements. Comments are written as // … or /* … */.
Node patterns
| Pattern | Matches |
|---|---|
(x) | any node, bound to the variable x |
() | any node, without a variable |
(x:Movie), (x IS Movie) | nodes with the label Movie |
(x:Title:Movie) | nodes with both labels |
(x:Movie|Short) | nodes with either label |
(:Q42), (x:Q42) | the Wikidata item Q42 |
(x {title: 'The Matrix', startYear: 1999}) | nodes with these property values |
(x WHERE x.rating > 8) | nodes that fulfil the condition |
Edge patterns
| Pattern | Matches |
|---|---|
-[e:P31]->, <-[e:P31]- | an edge with the label P31, pointing right or left |
-[e]-, - | an edge in either direction |
->, <- | any edge, without a variable |
-[:P31|P279]-> | an edge with either label |
-[e:P39 {P642: …}]->, -[e:P69 WHERE e.P580 IS NOT NULL]-> | edges with these properties (qualifiers) |
Nodes and edges alternate. A chain can be as long as needed:
// Directors Keanu Reeves worked with
MATCH (:Person {name: 'Keanu Reeves'})-[:ActedIn]->(movie:Movie)<-[:Directed]-(director:Person)
RETURN director.name AS director, movie.title AS movie
Properties
x.name reads the property name of the node or edge x.
For Wikidata, use the property id, e.g. e.P580 for the start time qualifier of the edge e.
genre, produces one row per value.
WHERE
Conditions can be written after MATCH, inside a node or edge pattern, or inside a path pattern.
| Operator | Meaning |
|---|---|
=, <> | The same node or value, e.g. m.title = 'The Matrix', a <> b |
<, <=, >, >= | Numeric comparison |
+, -, *, /, % | Arithmetic on numbers |
AND, OR, XOR, NOT | Boolean logic |
IS NULL, IS NOT NULL | Test for a missing value, e.g. after OPTIONAL MATCH |
EXISTS { <pattern> } | The pattern has a match. It may use variables of the outer query. |
x IN (MATCH … RETURN y), x NOT IN (…) | The node is (not) in the result of a subquery |
// Nobel laureates in physics who also received the Nobel Prize in Chemistry
MATCH (p)-[:P166]->(:Q38104)
WHERE EXISTS { (p)-[:P166]->(:Q44585) }
RETURN p, p.label AS pLabel
OPTIONAL MATCH
OPTIONAL MATCH keeps the rows of the preceding patterns even if the optional pattern has no match.
Its variables are then empty.
MATCH (country)-[:P31]->(:Q3624078)
OPTIONAL MATCH (country)-[:P36]->(capital)
RETURN country, country.label AS countryLabel, capital
Paths
Path queries are what butterfly is built for. A quantifier after an edge or after a parenthesised sub-pattern repeats it:
| Quantifier | Repetitions |
|---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 |
{n}, {m,n}, {,n} | exactly n, m to n, at most n |
// All subclasses of musical instrument (Q34379), like wdt:P279+ in SPARQL
MATCH (instrument)-[:P279]->+(:Q34379)
RETURN instrument, instrument.label AS instrumentLabel
// instance of a subclass of mountain, like wdt:P31/wdt:P279* in SPARQL
MATCH (mountain)-[:P31]->( ()-[:P279]-> )* (:Q8502)
RETURN mountain
A quantified sub-pattern can contain nodes with conditions and alternatives (|),
and quantifiers can be nested. A variable inside a quantified sub-pattern is bound to the
list of all its matches along the path:
// s is the list of stations on the route
MATCH ANY SHORTEST (:Q801394) ( () -[:P197]-> (s) )* (:Q742238)
RETURN [x IN s | x.label] AS stations
A prefix in front of the pattern selects which paths are returned:
| Prefix | Returns |
|---|---|
| none | one path for each pair of start and end node |
ANY SHORTEST, ANY | one shortest path for each pair |
ALL SHORTEST | all shortest paths for each pair |
ALL, WALK | all paths |
TRAIL | all paths without a repeated edge |
SIMPLE, ACYCLIC | all paths without a repeated node |
ANY SHORTEST over ALL SHORTEST and the other modes that return
all paths, which can take much longer.
RETURN
RETURN lists the columns of the result. Give a column a name with AS; without
it, n.label becomes a column named n__label. RETURN * returns all variables.
ORDER BY refers to the returned columns. It sorts nodes and properties by their internal id,
which is not alphabetical. To sort numbers by value, sort by an arithmetic expression such as
ORDER BY rating * 1 DESC.
A list comprehension turns a list variable into a list of values:
RETURN [m IN movie WHERE m.rating > 7 | m.title] AS goodMovies
Result views
Choose how the result is displayed with the menu above the result. A comment in the query selects the view that is used when the query is run:
// #defaultView:Map
MATCH (lighthouse)-[:P31]->(:Q39715), (lighthouse)-[:P17]->(:Q20), (lighthouse)-[:P625]->(coordinates)
RETURN lighthouse, lighthouse.label AS lighthouseLabel, coordinates
| View | Needs |
|---|---|
Table | anything |
Map | a coordinates column (Wikidata P625) |
ImageGrid | an image column (Wikidata P18 and other Commons media) |
Timeline | a date column (Wikidata time values) |
Graph | Wikidata item columns; each row links its items from left to right |
Tree, TreeMap | Wikidata item columns |
BarChart, LineChart, AreaChart, ScatterChart, BubbleChart, Dimensions | a text column and number columns |
For a Wikidata item in column x, a text column named xLabel is used as its label in the
table, the graph and the map. Options can follow the view name, e.g.
// #defaultView:ImageGrid{"hide": ["image"]} hides the column image.
Editor
- Ctrl+Enter runs the query. Ctrl+Space completes the word at the cursor.
-
After a colon, completion searches Wikidata and inserts the id:
(:humanbecomes(:Q5, and-[:instance ofbecomes-[:P31. It also completes the IMDB labels. - Hover over a Wikidata id like
Q42to see its label and description. - Errors in the query are highlighted in the editor and explained below it.
-
The Link menu gives a link to the query or to the result, and the Code button shows how to run
the query with
curlor Python. The service answers HTTPGETandPOSTrequests with the parameterquery, in the SPARQL JSON result format.
Limitations
- Keywords must be upper case.
-
No functions and no aggregation:
count(),sum(),DISTINCT,GROUP BY, string functions and the like are not supported. - No
UNWIND,CALL, parameters, list literals or path variables (p = (…)), and no changes to the data. ORDER BYsorts nodes and properties by internal id; sort by an arithmetic expression to sort numbers by value.- Reading a property drops rows without that property.
=compares node identity; comparisons with<and>and arithmetic only work on numbers. Wikidata quantities and times cannot be compared.- Wikidata only contains statements of the best rank, one label per item, and no descriptions, aliases or references.
SHORTESTmeans the fewest edges, not a shortest distance or time; among equally short paths,ANY SHORTESTmay return a different one on each run.
Examples
The query editor has example queries for all four databases. Open them with the Examples button; choosing an example also selects its database.
- Wikidata: Query 1 of the PathExtractor paper, the stations from Saint-Michel–Notre-Dame
to Aéroport Charles de Gaulle 1, with the
Stationlabel and as a nested regular expression. - IMDB: Query 2 of the paper, the actors connected to Kevin Bacon only through B-movies, and the same chain from Sean Bean.
- dblp and Math Genealogy: doctoral lineages, how two authors of the paper are related academically, and co-author chains through ISWC papers.
- German rail timetable: journeys from Bayreuth to München: with changes, with the Deutschlandticket, with time to spare at every change, and all equally short connections.