{"id":25,"date":"2018-05-16T09:07:00","date_gmt":"2018-05-16T09:07:00","guid":{"rendered":"http:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/?p=25"},"modified":"2020-11-04T15:43:56","modified_gmt":"2020-11-04T15:43:56","slug":"sparql-query-basics","status":"publish","type":"post","link":"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/sparql-query-basics\/","title":{"rendered":"SPARQL Query Basics"},"content":{"rendered":"<p>This will run through how to create a <strong>SPARQL<\/strong> query from scratch. SPARQL is a programming language for querying linked data stored on the web. It is essentially a set of commands that allow you to find exactly the data you want.<\/p>\n<p><strong>Watch &#8211; an <a href=\"https:\/\/www.youtube.com\/watch?v=TXdjxnjCvng\">Intro to SPARQL queries<\/a> by Dr. Chris Langley, Newman University.<\/strong><\/p>\n<p>Learning SPARQL will allow you to query the information stored in Wikidata but also the countless other data sources offering a SPARQL query service.<\/p>\n<p>e.g. Show me a list of French female writers along with their date of birth, place of birth, and the map co-ordinates of their place of birth.<\/p>\n<p>How long would it take to do this using Google?! In Wikidata it takes seconds.<\/p>\n<h2>Building a SPARQL query<\/h2>\n<p>To do this go to<strong> <a href=\"https:\/\/query.wikidata.org\/\">https:\/\/query.wikidata.org\/<\/a><\/strong><\/p>\n<p>To show how the Query Service works, we will create a query for <strong>Women Writers<\/strong>.<\/p>\n<p><strong>Step 1<\/strong> &#8211; Use a notable example to get you started. For women writers, we will go to <a href=\"https:\/\/www.wikidata.org\/wiki\/Q40909\">Virginia Woolf<\/a>.<\/p>\n<p><strong>Step 2<\/strong> &#8211; Scroll down to see how occupations (P106) are described on Virginia Woolf&#8217;s page as this will be the crux of our query. A writer is another item on Wikidata with a unique identifier of <a href=\"https:\/\/www.wikidata.org\/wiki\/Q36180\">Q36180<\/a><\/p>\n<p><strong>Step 3<\/strong> &#8211; Go to<a href=\"https:\/\/query.wikidata.org\/\"> https:\/\/query.wikidata.org\/<\/a> and type the following.<\/p>\n<p style=\"padding-left: 30px\"><strong>SELECT<\/strong>\u00a0\u00a0 ?item<\/p>\n<p style=\"padding-left: 30px\"><strong>WHERE <\/strong>{<\/p>\n<p style=\"padding-left: 30px\">?item<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>This will select an item of data on Wikidata. <strong>SELECT<\/strong> is telling you what you want to see back in your query in terms of the columns of information it will return in its results. <strong>WHERE <\/strong>is where we define what the relationships are that are going to define our query.<\/p>\n<p>But we need to define what properties we are interested in but let&#8217;s change the item name to something more relevant to our query (ie. a person) and add in the unique identifiers for <strong>occupation<\/strong> and the value of <strong>writer<\/strong>.<\/p>\n<p style=\"padding-left: 30px\"><strong>SELECT<\/strong>\u00a0\u00a0 ?person<\/p>\n<p style=\"padding-left: 30px\"><strong>WHERE <\/strong>{<\/p>\n<p style=\"padding-left: 30px\">?person wdt:<span style=\"color: #800080\">P106<\/span> wd:<span style=\"color: #3366ff\">Q36180<\/span> .<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>Holding down<strong> CTRL<\/strong> and pressing <strong>SPACE<\/strong> is a Wikidata Query Service trick that helps you find properties and values without having to look up the P number and Q number each time.<\/p>\n<p>If we hit the RUN symbol in the bottom left of the page then the query will return a list of results. 158,000 plus results. But it just returns it as a list of Q numbers. Wikidata has a label service so that we can display the list of English language labels. So we can add the label service and make sure we add <strong>?personLabel<\/strong> to the<strong> SELECT<\/strong> line too.<\/p>\n<p style=\"padding-left: 60px\"><strong>SELECT<\/strong> ?person ?personLabel<\/p>\n<p style=\"padding-left: 60px\"><strong>WHERE<\/strong> {<\/p>\n<p style=\"padding-left: 60px\">?person wdt:<span style=\"color: #800080\">P106<\/span> wd:<span style=\"color: #3366ff\">Q36180<\/span> .<\/p>\n<p style=\"padding-left: 60px\"><strong>SERVICE<\/strong> wikibase:label { bd:serviceParam wikibase:language &#8220;[AUTO_LANGUAGE],en&#8221;. }<br \/>\n}<\/p>\n<p><strong>RUN<\/strong> the query to display the results now. We should have two columns now.<\/p>\n<p>Tip: We can add in different language labels too by adding the two letter code for that language e.g. <strong>ar<\/strong> for arabic .<\/p>\n<p>e.g. <strong>SERVICE<\/strong> wikibase:label { bd:serviceParam wikibase:language &#8220;<strong>ar<\/strong>,en&#8221;. }<\/p>\n<p>We can filter the query further. To add the property of <strong>gender (P21)<\/strong> with a value of <strong>female (Q6581072)<\/strong><\/p>\n<p style=\"padding-left: 90px\"><strong>SELECT<\/strong> ?person ?personLabel<\/p>\n<p style=\"padding-left: 90px\"><strong>WHERE<\/strong> {<\/p>\n<p style=\"padding-left: 90px\">?person wdt:<span style=\"color: #800080\">P106<\/span> wd:<span style=\"color: #3366ff\">Q36180<\/span> .<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ?person wdt:<span style=\"color: #800080\">P21<\/span>\u00a0\u00a0 wd:<span style=\"color: #3366ff\">Q6581072<\/span> .<\/p>\n<p style=\"padding-left: 90px\"><strong>SERVICE<\/strong> wikibase:label { bd:serviceParam wikibase:language &#8220;en&#8221;. }<br \/>\n}<\/p>\n<p>We can display the results with images so that the query comes to life. Again making sure we add <strong>?image<\/strong> to the <strong>SELECT<\/strong> line at the top. The difference this time is we are not specifying what the <strong>value<\/strong> for the image should be.<\/p>\n<p style=\"padding-left: 90px\"><strong>SELECT<\/strong> ?person ?personLabel ?image<\/p>\n<p style=\"padding-left: 90px\"><strong>WHERE<\/strong> {<\/p>\n<p style=\"padding-left: 90px\">?person wdt:<span style=\"color: #800080\">P106<\/span> wd:<span style=\"color: #3366ff\">Q36180<\/span> .<\/p>\n<p style=\"padding-left: 90px\">?person wdt:<span style=\"color: #800080\">P21<\/span>\u00a0\u00a0 wd:<span style=\"color: #3366ff\">Q6581072<\/span> .<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ?person wdt:<span style=\"color: #800080\">P18<\/span> ?image .<\/p>\n<p style=\"padding-left: 90px\"><strong>SERVICE<\/strong> wikibase:label { bd:serviceParam wikibase:language &#8220;en&#8221;. }<br \/>\n}<\/p>\n<p>We can click RUN to display the results but we should change the display results from <strong>Table<\/strong> to <strong>Image Grid<\/strong> (using the dropdown menu in the bottom left of the screen) in order to get the images displayed.<\/p>\n<p>We can also add in their <strong>date of birth<\/strong> (P569) and <strong>place of birth (P19).<\/strong><\/p>\n<p>NB: Place of birth requires you to add an extra line if you wish to pull in the co-ordinates of their place of birth so it can be visualised on a map.<\/p>\n<p style=\"padding-left: 60px\"><strong>SELECT<\/strong> ?person ?personLabel ?image ?birthDate ?birthPlace ?coords<\/p>\n<p style=\"padding-left: 60px\"><strong>WHERE<\/strong> {<\/p>\n<p style=\"padding-left: 60px\">?person wdt:<span style=\"color: #800080\">P106<\/span> wd:<span style=\"color: #3366ff\">Q36180<\/span> .<br \/>\n?person wdt:<span style=\"color: #800080\">P21<\/span> wd:<span style=\"color: #3366ff\">Q6581072<\/span> .<br \/>\n?person wdt:<span style=\"color: #800080\">P18<\/span> ?image .<br \/>\n?person wdt:<span style=\"color: #800080\">P569<\/span> ?birthDate .<br \/>\n?person wdt:<span style=\"color: #800080\">P19<\/span> ?birthPlace .<br \/>\n?birthPlace wdt:<span style=\"color: #800080\">P625<\/span> ?coords .<\/p>\n<p style=\"padding-left: 60px\"><strong>SERVICE<\/strong> wikibase:label { bd:serviceParam wikibase:language &#8220;ar,en&#8221;. }<br \/>\n}<\/p>\n<p>Clicking <strong>RUN<\/strong> and setting the display results to <strong>MAP<\/strong> (again <a href=\"http:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-content\/uploads\/sites\/27\/2018\/05\/Screenshot_Wikidata_Query_Service_SPARQL.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-99\" src=\"http:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-content\/uploads\/sites\/27\/2018\/05\/Screenshot_Wikidata_Query_Service_SPARQL-300x150.png\" alt=\"\" width=\"300\" height=\"150\" srcset=\"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-content\/uploads\/sites\/27\/2018\/05\/Screenshot_Wikidata_Query_Service_SPARQL-300x150.png 300w, https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-content\/uploads\/sites\/27\/2018\/05\/Screenshot_Wikidata_Query_Service_SPARQL-768x384.png 768w, https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-content\/uploads\/sites\/27\/2018\/05\/Screenshot_Wikidata_Query_Service_SPARQL-1024x512.png 1024w, https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-content\/uploads\/sites\/27\/2018\/05\/Screenshot_Wikidata_Query_Service_SPARQL-1440x720.png 1440w, https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-content\/uploads\/sites\/27\/2018\/05\/Screenshot_Wikidata_Query_Service_SPARQL.png 1902w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>accessed from the bottom left dropdown menu) will show the co-ordinate locations of the places of birth of female writers in Wikidata.<\/p>\n<p>However, each line of the query further the filters the results to only those results that have a) an occupation b) gender c) an image, a date of birth AND a place of birth. Perhaps we don&#8217;t want to exclude some items of data that don&#8217;t have an image, date of birth or place of birth from our results. In that case, we can use the <strong>OPTIONAL<\/strong> command.<\/p>\n<p style=\"padding-left: 30px\"><strong>SELECT<\/strong> ?person ?personLabel ?image ?birthDate ?birthPlace ?coords<\/p>\n<p style=\"padding-left: 30px\"><strong>WHERE<\/strong> {<\/p>\n<p style=\"padding-left: 30px\">?person wdt:P106 wd:Q36180 .<br \/>\n?person wdt:P21 wd:Q6581072 .<br \/>\n<strong>OPTIONAL<\/strong> {?person wdt:P18 ?image .}<br \/>\n<strong>OPTIONAL<\/strong> {?person wdt:P569 ?birthDate .}<br \/>\n<strong>OPTIONAL<\/strong> {?person wdt:P19 ?birthPlace .<br \/>\n?birthPlace wdt:P625 ?coords .}<\/p>\n<p style=\"padding-left: 30px\"><strong>SERVICE<\/strong> wikibase:label { bd:serviceParam wikibase:language &#8220;ar,en&#8221;. }<br \/>\n}<\/p>\n<h2 style=\"padding-left: 30px\"><\/h2>\n<h2>Play around with more SPARQL queries<\/h2>\n<ol>\n<li>You can access more SPARQL queries from the <strong>Examples\u00a0<\/strong>dropdown at the top left of the <a href=\"https:\/\/query.wikidata.org\/\">Wikidata Query Service<\/a>.<\/li>\n<li><a href=\"https:\/\/www.wikidata.org\/wiki\/Wikidata:SPARQL_query_service\/queries\">https:\/\/www.wikidata.org\/wiki\/Wikidata:SPARQL_query_service\/queries<\/a><\/li>\n<li><a href=\"https:\/\/commons.wikimedia.org\/wiki\/File:Wikidata_Query_Service_in_Brief.pdf\">Pdf handout explaining the Query Service (in brief)<\/a>.<\/li>\n<li><a href=\"https:\/\/media.ed.ac.uk\/media\/Wikidata+Sparql+Query+Tutorial\/1_7v9v6s04\">Wikidata Sparql Query Service &#8211; video tutorial.<\/a><\/li>\n<li><a href=\"http:\/\/www.learningsparql.com\/\">Learning SPARQL<\/a> (book).<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This will run through how to create a SPARQL query from scratch. SPARQL is a programming language for querying linked data stored on the web. It is essentially a set of commands that allow you to find exactly the data you want. Watch &#8211; an Intro to SPARQL queries by Dr. Chris Langley, Newman University. [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":99,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-25","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":11,"href":"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-json\/wp\/v2\/posts\/25\/revisions\/178"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-json\/wp\/v2\/media\/99"}],"wp:attachment":[{"href":"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thinking.is.ed.ac.uk\/wikidata-workshop\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}