Skip to content

Examples

Daniel Heward-Mills edited this page Sep 27, 2025 · 1 revision

Examples

This document provides practical examples of SPARQL queries in SPARQLWorksβ„’, demonstrating different patterns and visualization techniques.

🎬 Movie Database Examples

Basic Movie-Creator Relationships

Endpoint: https://dbpedia.org/sparql

Basic Mode:

?movie dbo:creator ?creator;
       rdfs:label ?movieName.
FILTER(?creator = dbr:Spike_Lee)

Advanced Mode:

CONSTRUCT {
  ?movie dbo:creator ?creator;
         rdfs:label ?movieName.
}
WHERE {
  ?movie dbo:creator ?creator;
         rdfs:label ?movieName.
  FILTER(?creator = dbr:Spike_Lee)
}
LIMIT 50

Visualization: Shows Spike Lee's movies connected to their creators with title labels.

Movie Genres and Directors

Basic Mode:

?movie dbo:director ?director;
       dbo:genre ?genre;
       rdfs:label ?title.
FILTER(?genre = dbr:Drama_film)

Advanced Mode:

CONSTRUCT {
  ?movie dbo:director ?director;
         dbo:genre ?genre;
         rdfs:label ?title.
}
WHERE {
  ?movie a dbo:Film;
         dbo:director ?director;
         dbo:genre ?genre;
         rdfs:label ?title.
  FILTER(?genre = dbr:Drama_film)
}
LIMIT 100

Visualization: Drama films connected to directors and genre classifications.

πŸ‘₯ Social Network Examples

FOAF Friend Relationships

Endpoint: https://dbpedia.org/sparql

Basic Mode:

?person foaf:name ?name;
        foaf:knows ?friend.
?friend foaf:name ?friendName.

Advanced Mode:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

CONSTRUCT {
  ?person foaf:name ?name;
          foaf:knows ?friend.
  ?friend foaf:name ?friendName.
}
WHERE {
  ?person foaf:name ?name;
          foaf:knows ?friend.
  ?friend foaf:name ?friendName.
}
LIMIT 200

Visualization: Social network graph showing people and their friendships.

Organization Hierarchies

Basic Mode:

?person foaf:name ?name;
        org:memberOf ?org.
?org rdfs:label ?orgName.

Advanced Mode:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX org: <http://www.w3.org/ns/org#>

CONSTRUCT {
  ?person foaf:name ?name;
          org:memberOf ?org.
  ?org rdfs:label ?orgName.
}
WHERE {
  ?person foaf:name ?name;
          org:memberOf ?org.
  ?org rdfs:label ?orgName.
}
LIMIT 150

Visualization: Organizational structure with members connected to organizations.

πŸ—ΊοΈ Geographic Examples

Cities and Countries

Endpoint: https://query.wikidata.org/sparql

Basic Mode:

?city wdt:P31 wd:Q515;
      wdt:P17 ?country;
      rdfs:label ?cityName.
?country rdfs:label ?countryName.
FILTER(LANG(?cityName) = "en")
FILTER(LANG(?countryName) = "en")

Advanced Mode:

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

CONSTRUCT {
  ?city rdfs:label ?cityName;
         wdt:P17 ?country.
  ?country rdfs:label ?countryName.
}
WHERE {
  ?city wdt:P31 wd:Q515;
        wdt:P17 ?country;
        rdfs:label ?cityName.
  ?country rdfs:label ?countryName.
  FILTER(LANG(?cityName) = "en")
  FILTER(LANG(?countryName) = "en")
}
LIMIT 100

Visualization: Cities connected to their countries with English labels.

Location Hierarchies

Basic Mode:

?location wdt:P131 ?parent;
          rdfs:label ?name.
?parent rdfs:label ?parentName.
FILTER(?location = wd:Q84)

Advanced Mode:

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

CONSTRUCT {
  ?location wdt:P131 ?parent;
            rdfs:label ?name.
  ?parent rdfs:label ?parentName.
}
WHERE {
  ?location wdt:P131* ?parent;
            rdfs:label ?name.
  ?parent rdfs:label ?parentName.
  FILTER(?location = wd:Q84)
}
LIMIT 50

Visualization: Hierarchical location relationships using property paths.

πŸ“š Academic Examples

Research Publications

Endpoint: https://dbpedia.org/sparql

Basic Mode:

?paper dct:creator ?author;
       dct:title ?title;
       dct:subject ?subject.
?author foaf:name ?authorName.

Advanced Mode:

PREFIX dct: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

CONSTRUCT {
  ?paper dct:creator ?author;
         dct:title ?title;
         dct:subject ?subject.
  ?author foaf:name ?authorName.
}
WHERE {
  ?paper dct:creator ?author;
         dct:title ?title;
         dct:subject ?subject.
  ?author foaf:name ?authorName.
}
LIMIT 75

Visualization: Academic papers connected to authors and research subjects.

University Departments

Basic Mode:

?dept a dbo:University;
      dbo:city ?city;
      rdfs:label ?deptName.
?city rdfs:label ?cityName.

Advanced Mode:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

CONSTRUCT {
  ?dept a dbo:University;
         dbo:city ?city;
         rdfs:label ?deptName.
  ?city rdfs:label ?cityName.
}
WHERE {
  ?dept a dbo:University;
        dbo:city ?city;
        rdfs:label ?deptName.
  ?city rdfs:label ?cityName.
}
LIMIT 60

Visualization: Universities connected to their cities.

🎡 Music Examples

Artists and Albums

Endpoint: https://dbpedia.org/sparql

Basic Mode:

?artist a dbo:MusicalArtist;
        dbo:genre ?genre;
        rdfs:label ?artistName.
?album dbo:artist ?artist;
       rdfs:label ?albumName.

Advanced Mode:

PREFIX dbo: <http://dbpedia.org/ontology/>

CONSTRUCT {
  ?artist a dbo:MusicalArtist;
          dbo:genre ?genre;
          rdfs:label ?artistName.
  ?album dbo:artist ?artist;
         rdfs:label ?albumName.
}
WHERE {
  ?artist a dbo:MusicalArtist;
          dbo:genre ?genre;
          rdfs:label ?artistName.
  ?album dbo:artist ?artist;
         rdfs:label ?albumName.
}
LIMIT 80

Visualization: Music artists connected to their albums and genres.

Music Venues

Basic Mode:

?venue a dbo:Venue;
       dbo:location ?city;
       rdfs:label ?venueName.
?city rdfs:label ?cityName.

Advanced Mode:

PREFIX dbo: <http://dbpedia.org/ontology/>

CONSTRUCT {
  ?venue a dbo:Venue;
         dbo:location ?city;
         rdfs:label ?venueName.
  ?city rdfs:label ?cityName.
}
WHERE {
  ?venue a dbo:Venue;
         dbo:location ?city;
         rdfs:label ?venueName.
  ?city rdfs:label ?cityName.
}
LIMIT 40

Visualization: Music venues connected to their locations.

πŸ›οΈ Government Examples

Politicians and Parties

Endpoint: https://query.wikidata.org/sparql

Basic Mode:

?politician wdt:P102 ?party;
            rdfs:label ?name.
?party rdfs:label ?partyName.
FILTER(LANG(?name) = "en")
FILTER(LANG(?partyName) = "en")

Advanced Mode:

PREFIX wdt: <http://www.wikidata.org/prop/direct/>

CONSTRUCT {
  ?politician wdt:P102 ?party;
              rdfs:label ?name.
  ?party rdfs:label ?partyName.
}
WHERE {
  ?politician wdt:P102 ?party;
              rdfs:label ?name.
  ?party rdfs:label ?partyName.
  FILTER(LANG(?name) = "en")
  FILTER(LANG(?partyName) = "en")
}
LIMIT 120

Visualization: Politicians connected to their political parties.

Government Organizations

Basic Mode:

?org wdt:P31 wd:Q327333;
     wdt:P17 ?country;
     rdfs:label ?orgName.
?country rdfs:label ?countryName.
FILTER(LANG(?orgName) = "en")

Advanced Mode:

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

CONSTRUCT {
  ?org wdt:P31 wd:Q327333;
       wdt:P17 ?country;
       rdfs:label ?orgName.
  ?country rdfs:label ?countryName.
}
WHERE {
  ?org wdt:P31 wd:Q327333;
       wdt:P17 ?country;
       rdfs:label ?orgName.
  ?country rdfs:label ?countryName.
  FILTER(LANG(?orgName) = "en")
}
LIMIT 90

Visualization: Government organizations connected to countries.

πŸ”¬ Scientific Examples

Chemical Compounds

Endpoint: https://dbpedia.org/sparql

Basic Mode:

?compound a dbo:ChemicalCompound;
          dbo:molecularWeight ?weight;
          rdfs:label ?name.
FILTER(?weight > 100)

Advanced Mode:

PREFIX dbo: <http://dbpedia.org/ontology/>

CONSTRUCT {
  ?compound a dbo:ChemicalCompound;
            dbo:molecularWeight ?weight;
            rdfs:label ?name.
}
WHERE {
  ?compound a dbo:ChemicalCompound;
            dbo:molecularWeight ?weight;
            rdfs:label ?name.
  FILTER(?weight > 100)
}
LIMIT 70

Visualization: Chemical compounds with molecular weights.

Research Datasets

Basic Mode:

?dataset dct:creator ?researcher;
         dct:subject ?topic;
         rdfs:label ?title.
?researcher foaf:name ?researcherName.

Advanced Mode:

PREFIX dct: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

CONSTRUCT {
  ?dataset dct:creator ?researcher;
           dct:subject ?topic;
           rdfs:label ?title.
  ?researcher foaf:name ?researcherName.
}
WHERE {
  ?dataset dct:creator ?researcher;
           dct:subject ?topic;
           rdfs:label ?title.
  ?researcher foaf:name ?researcherName.
}
LIMIT 55

Visualization: Research datasets connected to creators and topics.

🎭 Cultural Examples

Museum Collections

Endpoint: https://dbpedia.org/sparql

Basic Mode:

?artwork dbo:museum ?museum;
         dbo:artist ?artist;
         rdfs:label ?title.
?museum rdfs:label ?museumName.

Advanced Mode:

PREFIX dbo: <http://dbpedia.org/ontology/>

CONSTRUCT {
  ?artwork dbo:museum ?museum;
           dbo:artist ?artist;
           rdfs:label ?title.
  ?museum rdfs:label ?museumName.
}
WHERE {
  ?artwork dbo:museum ?museum;
           dbo:artist ?artist;
           rdfs:label ?title.
  ?museum rdfs:label ?museumName.
}
LIMIT 65

Visualization: Artworks connected to museums and artists.

Literary Works

Basic Mode:

?book dbo:author ?author;
      dbo:genre ?genre;
      rdfs:label ?title.
?author rdfs:label ?authorName.

Advanced Mode:

PREFIX dbo: <http://dbpedia.org/ontology/>

CONSTRUCT {
  ?book dbo:author ?author;
        dbo:genre ?genre;
        rdfs:label ?title.
  ?author rdfs:label ?authorName.
}
WHERE {
  ?book dbo:author ?author;
        dbo:genre ?genre;
        rdfs:label ?title.
  ?author rdfs:label ?authorName.
}
LIMIT 85

Visualization: Books connected to authors and literary genres.

πŸ₯ Healthcare Examples

Medical Conditions

Endpoint: https://query.wikidata.org/sparql

Basic Mode:

?condition wdt:P31 wd:Q12136;
           wdt:P1995 ?specialty;
           rdfs:label ?name.
?specialty rdfs:label ?specialtyName.
FILTER(LANG(?name) = "en")

Advanced Mode:

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

CONSTRUCT {
  ?condition wdt:P31 wd:Q12136;
             wdt:P1995 ?specialty;
             rdfs:label ?name.
  ?specialty rdfs:label ?specialtyName.
}
WHERE {
  ?condition wdt:P31 wd:Q12136;
             wdt:P1995 ?specialty;
             rdfs:label ?name.
  ?specialty rdfs:label ?specialtyName.
  FILTER(LANG(?name) = "en")
}
LIMIT 95

Visualization: Medical conditions connected to medical specialties.

Healthcare Organizations

Basic Mode:

?hospital wdt:P31 wd:Q16917;
          wdt:P131 ?city;
          rdfs:label ?name.
?city rdfs:label ?cityName.
FILTER(LANG(?name) = "en")

Advanced Mode:

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

CONSTRUCT {
  ?hospital wdt:P31 wd:Q16917;
            wdt:P131 ?city;
            rdfs:label ?name.
  ?city rdfs:label ?cityName.
}
WHERE {
  ?hospital wdt:P31 wd:Q16917;
            wdt:P131 ?city;
            rdfs:label ?name.
  ?city rdfs:label ?cityName.
  FILTER(LANG(?name) = "en")
}
LIMIT 110

Visualization: Hospitals connected to their cities.

πŸ”— Tips for Creating Examples

Query Design Principles

  1. Start Simple: Begin with basic patterns and add complexity
  2. Use LIMIT: Always include reasonable limits for performance
  3. Add Filters: Use FILTER clauses to focus results
  4. Include Labels: Ensure entities have readable labels
  5. Test Endpoints: Verify queries work on target endpoints

Visualization Optimization

  1. Node Types: Mix IRI and literal nodes for rich graphs
  2. Relationship Depth: Balance breadth and depth
  3. Label Quality: Prefer datasets with good multilingual labels
  4. Performance: Consider graph size and rendering complexity

Common Patterns

  • Entity-Attribute: Basic subject-predicate-object triples
  • Entity-Relationship: Connections between entities
  • Hierarchies: Parent-child or type-subtype relationships
  • Networks: Complex interconnected structures

← Graph Visualization | Next: Authentication β†’

Clone this wiki locally