Skip to content

Instantly share code, notes, and snippets.

@jbenner-radham
Last active July 4, 2016 00:36
Show Gist options
  • Save jbenner-radham/f1230cc01cc870e5f55fe0768aa62c3e to your computer and use it in GitHub Desktop.
Save jbenner-radham/f1230cc01cc870e5f55fe0768aa62c3e to your computer and use it in GitHub Desktop.
SPARQL queries that can be run at https://query.wikidata.org/

Search For An Item With the Label "Sega Mega Drive"

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

SELECT * WHERE {
    ?id rdfs:label "Sega Mega Drive"@en
}

Search For Items With the "platform" of "Sega Mega Drive"

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

SELECT * WHERE {
  ?x wdt:P400 wd:Q10676 # "platform" (wdt:P400), "Sega Mega Drive" (wd:Q10676)
}

Search For Games With the "platform" of "Sega Mega Drive" and Sort By English Label

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

SELECT * WHERE {
  ?game wdt:P400 wd:Q10676. # "platform" (wdt:P400), "Sega Mega Drive" (wd:Q10676)
  ?game wdt:P31 wd:Q7889.   # "instance of" (wdt:P31), "video game" (wd:Q7889)
  OPTIONAL {
    ?game rdfs:label ?label.
    FILTER((LANG(?label)) = "en")
  }
}
ORDER BY ?label

Search For Games With the "platform" of "Sega Mega Drive" and Additional Constraints

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

SELECT * WHERE {
  ?game wdt:P400 wd:Q10676.        # "platform" (wdt:P400), "Sega Mega Drive" (wd:Q10676)
  ?game wdt:P31 wd:Q7889.          # "instance of" (wdt:P31), "video game" (wd:Q7889)
  ?game rdfs:label ?label.         # label for "video game"
  # ?game wdt:P437 wd:Q633454.     # uncomment this line to only include "distribution" (wdt:P437), "ROM cartridge" (wd:Q633454)
  # ?game wdt:P123 wd:Q122741.     # uncomment this line to only include "publisher" (wdt:P123), "Sega" (wd:Q122741)
  OPTIONAL {
    ?game wdt:P852 ?esrb.          # optionally get the "ESRB rating" (wdt:P852)
    ?esrb rdfs:label ?rating.      # optionally get the label of the "ESRB rating"
    FILTER (LANG(?rating) = "en"). # require an English label
  }
  FILTER (LANG(?label) = "en").    # require an English label
  FILTER NOT EXISTS {
    ?game wdt:P437 wd:Q7982.       # must not be "distribution" (wdt:P437), "CD-ROM" (wd:Q7982)
  }
}
ORDER BY ?label

Search For Games With the "platform" of "Sega Mega Drive" and Additional Constraints With Tabular Format

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

SELECT ?game ?name ?publication_date ?rating WHERE {
  ?game wdt:P400 wd:Q10676.           # "platform" (wdt:P400), "Sega Mega Drive" (wd:Q10676)
  ?game wdt:P31 wd:Q7889.             # "instance of" (wdt:P31), "video game" (wd:Q7889)
  ?game rdfs:label ?name.             # label for "video game"
  OPTIONAL {
    ?game wdt:P852 ?esrb.             # optionally get the "ESRB rating" (wdt:P852)
    ?game wdt:P577 ?publication_date. # optionally get the "date of publication" (wdt:P577)
    ?esrb rdfs:label ?rating.         # optionally get the label of the "ESRB rating"
    FILTER (LANG(?rating) = "en").    # require an English label if a "rating" exists
  }
  FILTER (LANG(?name) = "en").        # require an English label
}
ORDER BY ?name

Search For All the ESRB Ratings

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

SELECT DISTINCT ?rating WHERE {
  ?esrb_rating_category wdt:P31 wd:Q23683568. # instance of (P31), ESRB rating category (Q23683568)
  ?esrb_rating_category rdfs:label ?rating    # label of "ESRB rating category"
  FILTER((LANG(?rating)) = "en")              # require an English label
}
ORDER BY ?rating

Search For the Game Name, Game Mode, Genre, and Rating For "Sega Mega Drive" Which Are Also Published By Sega

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT ?_game ?name ?game_mode ?genre ?rating WHERE {
  ?_game wdt:P400 wd:Q10676.   # "platform" (wdt:P400), "Sega Mega Drive" (wd:Q10676)
  ?_game wdt:P123 wd:Q122741.  # "publisher" (wdt:P123), "Sega" (wd:Q122741)
  ?_game wdt:P31 wd:Q7889.     # "instance of" (wdt:P31), "video game" (wd:Q7889)
  ?_game rdfs:label ?name.     # label for "video game"
  OPTIONAL {
    ?_game wdt:P404 ?_game_mode.
    ?_game_mode rdfs:label ?game_mode.
    ?_game wdt:P136 ?_genre.
    ?_genre rdfs:label ?genre.
    ?_game wdt:P852 ?_rating.
    ?_rating rdfs:label ?rating.
    FILTER((LANG(?game_mode)) = "en")
    FILTER((LANG(?genre)) = "en")
    FILTER((LANG(?rating)) = "en")
  }
  FILTER((LANG(?name)) = "en") # require an English label
}
ORDER BY ?name

Schema.org "VideoGame" Mapping

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

SELECT ?_game ?name ?publisher ?developer ?platform ?game_mode ?genre ?rating WHERE {
  ?_game wdt:P400 wd:Q10676.                 # "platform" (wdt:P400), "Sega Mega Drive" (wd:Q10676)
  ?_game wdt:P400 ?_platform.                # get the "platform" (wdt:P400)
  ?_platform rdfs:label ?platform.           # label for "platform"
  ?_game wdt:P123 wd:Q122741.                # "publisher" (wdt:P123), "Sega" (wd:Q122741)
  ?_game wdt:P123 ?_publisher.               # get the "publisher" (wdt:P123)
  ?_publisher rdfs:label ?publisher.         # label for "publisher"
  ?_game wdt:P31 wd:Q7889.                   # "instance of" (wdt:P31), "video game" (wd:Q7889)
  ?_game rdfs:label ?name.                   # label for "video game"
  OPTIONAL {
    ?_game wdt:P178 ?_developer.             # get the "developer" (wdt:P178)
    ?_developer rdfs:label ?developer.       # label for "developer"
    ?_game wdt:P404 ?_game_mode.             # get the "game mode" (wdt:P404)
    ?_game_mode rdfs:label ?game_mode.       # label for "game mode"
    ?_game wdt:P136 ?_genre.                 # get the "genre" (wdt:P136)
    ?_genre rdfs:label ?genre.               # label for "genre"
    ?_game wdt:P852 ?_rating.                # get the "rating" (wdt:P852)
    ?_rating rdfs:label ?rating.             # label for "rating"
    FILTER((LANG(?developer)) = "en")        # require an English label
    FILTER((LANG(?game_mode)) = "en")        # require an English label
    FILTER((LANG(?genre)) = "en")            # require an English label
    FILTER((LANG(?rating)) = "en")           # require an English label
  }
  MINUS {
    ?_game wdt:P400 wd:Q1063978.             # minus "platform" (wdt:P400), "Sega 32X" (wd:Q1063978)
  }
  FILTER((LANG(?name)) = "en")               # require an English label
  FILTER((LANG(?platform)) = "en")           # require an English label
  FILTER((LANG(?publisher)) = "en")          # require an English label
  FILTER regex(?publisher, "Sega")           # filter out any of the non-Sega published releases
  FILTER regex(?platform, "Sega Mega Drive") # filter out any of the non-Sega Mega Drive releases
  FILTER NOT EXISTS {
    ?_game wdt:P437 wd:Q7982.                # filter out "distribution" (wdt:P437), "CD-ROM" (wd:Q7982) 
  }
}
ORDER BY ?name
@jbenner-radham
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment