{"id":526,"date":"2020-01-19T14:59:06","date_gmt":"2020-01-19T14:59:06","guid":{"rendered":"http:\/\/wikidata.wwwnlsrc4.supercp.com\/?p=526"},"modified":"2020-11-18T14:32:43","modified_gmt":"2020-11-18T14:32:43","slug":"optional-clause","status":"publish","type":"post","link":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/simple-queries\/adding-variables\/optional-clause\/","title":{"rendered":"The OPTIONAL clause"},"content":{"rendered":"\n<h2>Missing values and the OPTIONAL clause<\/h2>\n\n\n\n<p><a href=\"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/simple-queries\/adding-match-patterns\/two-or-more-match-patterns\/\">As we\u2019ve seen<\/a>, when there is more than one pattern to match in the WHERE clause, there is an implicit AND between the statements, such that only patterns that match&nbsp;<strong>all<\/strong>&nbsp;statements will be returned by the query.&nbsp;For example, in the <a href=\"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/simple-queries\/adding-variables\/another-exercise\/\">last exercise<\/a>, there were three matching patterns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror cm-s-eclipse\" data-setting=\"{&quot;mode&quot;:&quot;sparql&quot;,&quot;mime&quot;:&quot;application\/sparql-query&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true}\">#Star Wars films\n\nSELECT ?item  ?itemLabel ?directorLabel ?cost\nWHERE \n{ \n  ?item wdt:P179 wd:Q22092344.       # item is part of the series Star Wars (film series)\n  ?item wdt:P57 ?director.           # item\u2019s director property\u2019s value is collected by the director variable\n  ?item wdt:P2130 ?cost.  \t\t\t # item's cost property's value is collected by the cost variable\n  SERVICE wikibase:label { bd:serviceParam wikibase:language &quot;[AUTO_LANGUAGE]&quot;. }\n}<\/pre><\/div>\n\n\n\n<p>The statements on line 6, 7, and 8 have an AND relation between them. Therefore, an item will be considered a match only if it has a P179 property (part of a series) with a value of Q22092344 (Star Wars film series), as well as a P57 (director) property and a P2130 (cost) property. If an item doesn&#8217;t match one of these statements (e.g., doesn\u2019t have the property cost) it is ignored by the query.&nbsp;&nbsp;<\/p>\n\n\n\n<p>SPARQL is a <em>pattern-matching<\/em> query language. SPARQL queries will only return data when the pattern in the WHERE clause <strong>exactly <\/strong>matches the pattern in the data you&#8217;re querying.\u00a0But many datasets have missing values, and data is only a match for the query if there is valid data in each piece of the statement declared within WHERE. This means SPARQL will not return an item that is missing property name or value requested in WHERE.<br><br>The OPTIONAL keyword within the WHERE clause denotes optional patterns you&#8217;d like to match in the data. OPTIONAL allows searching for data that may or may not be there.<br><\/p>\n\n\n\n<iframe class=\"\" src=\"https:\/\/query.wikidata.org\/#%23Star%20Wars%20films%0A%0ASELECT%20%3Fitem%20%20%3FitemLabel%20%3FdirectorLabel%20%3Fcost%0AWHERE%20%0A%7B%20%0A%20%20%3Fitem%20wdt%3AP179%20wd%3AQ22092344.%20%20%20%20%20%20%20%23%20item%20is%20part%20of%20the%20series%20Star%20Wars%20%28film%20series%29%0A%20%20%3Fitem%20wdt%3AP57%20%3Fdirector.%20%20%20%20%20%20%20%20%20%20%20%23%20item%E2%80%99s%20director%20property%E2%80%99s%20value%20is%20collected%20by%20the%20director%20variable%0A%20%20OPTIONAL%20%7B%3Fitem%20wdt%3AP2130%20%3Fcost.%7D%20%20%23%20item%27s%20cost%20property%27s%20value%2C%20if%20available%2C%20is%20collected%20by%20the%20cost%20variable%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%22.%20%7D%0A%7D\" style=\"width:100%;max-width:100%;height:450px\" frameborder=\"0\"><\/iframe>\n\n\n\n<p>Run the query again with cost as an optional pattern.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Missing values and the OPTIONAL clause As we\u2019ve seen, when there is more than one pattern to match in the WHERE clause, there is an implicit AND between the statements, such that only patterns that match&nbsp;all&nbsp;statements will be returned by the query.&nbsp;For example, in the last exercise, there were three matching patterns: The statements on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":""},"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/posts\/526"}],"collection":[{"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/comments?post=526"}],"version-history":[{"count":16,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/posts\/526\/revisions"}],"predecessor-version":[{"id":1585,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/posts\/526\/revisions\/1585"}],"wp:attachment":[{"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/media?parent=526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/categories?post=526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/tags?post=526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}