Notes

Part 7 of 10

Graphs in SQL

Graph data can sit in tables and be queried with SQL, but with effort. Each edge followed is a join with the edges table. Normal SQL queries have a fixed set of joins; a graph walk may need any number of hops.

For example, LIVES_IN might point at a street, town, district, region, or country, and the continent might be one level up or five. Cypher's *0.. works like * in a regular expression: repeat as needed.

SQL handles walks of unknown length with a recursive common table expression (WITH RECURSIVE). For the example:

  1. Start a set of Asian places with the vertex named Asia.
  2. Keep adding vertices with a PART_OF edge into the set until nothing new appears.
  3. Build the African set the same way.
  4. Follow incoming STUDIED_IN edges from the Asian set to find people.
  5. Follow incoming LIVES_IN edges from the African set.
  6. Join the two to keep people in both.

A few lines of Cypher can become around thirty lines of SQL, so model and language choice matter a lot. Even then, SQL leaves details open, like guarding against cycles and choosing breadth-first or depth-first order.

GQL (Graph Query Language) is an ISO standard based on Cypher, published in 2024. It is not widely used yet but may bring graph languages together.

Triples

A triple store is mostly the property graph idea under other names, but its tools and languages are worth knowing. Every fact is a three-part statement (subject, predicate, object), like (Asha, plays, cricket).

  • The subject is a vertex.
  • If the object is a plain value (string, number), predicate and object act as a property key and value: (Asha, birthYear, 1994).
  • If the object is another vertex, the predicate is an edge label from subject (tail) to object (head): (Asha, friendOf, Ravi).

Turtle, a subset of Notation3 (N3), writes triples line by line. Names like _:asha are blank-node names that only tie together triples about one vertex within the file. The word "a" gives a vertex's type, and semicolons let you say several things about one subject compactly.

Where triples came from

Much triple store work came out of the Semantic Web: a push around 2000 to put machine-readable data on the internet in a standard form, alongside pages for people. That dream did not come true, but it left a legacy:

  • JSON-LD linked data.
  • Ontologies in biology and medicine.
  • Facebook's Open Graph protocol, behind link previews.
  • Knowledge graphs like Wikidata.
  • Schema.org's shared vocabularies.

Triples can be a good internal model even if you never care about the Semantic Web.

RDF

Turtle is one way to write RDF (Resource Description Framework), the data model designed for the Semantic Web. RDF also has a wordier XML form, and tools like Apache Jena convert between forms.

Because RDF aimed at sharing data across the internet, subjects, predicates, and objects are often URIs, like your domain plus "partOf". Then your data can merge with someone else's without a clash when one word means different things to each. The URI need not lead to a real page; it is just a namespace, and examples often use ones like urn:example:partOf. The prefix is declared once at the top of the file.

SPARQL

SPARQL queries RDF. It stands for SPARQL Protocol and RDF Query Language (the name contains itself) and is said "sparkle". It is older than Cypher, which borrowed its pattern matching, so they look alike.

The "studied in Asia, lives in Africa" question is just as short in SPARQL. Variables start with a question mark, like ?person. A path like studiedIn / partOf* / name follows one studiedIn edge, any number of partOf edges, then reads the name. Since RDF uses predicates for both properties and edges, the same syntax matches properties, like binding ?place to whatever vertex has the name "Asia".