"ORDER BY <variable> <variable> ... DESC(<variable>) ASC(<variable>)\n",
"ORDER BY <variable> <variable> ... \n",
"... other statements like LIMIT ...\n",
"... other statements like LIMIT ...\n",
"```\n",
"```\n",
"\n",
"\n",
"The results can be sorted in ascending or descending order, and using several variables."
"The results can be sorted in ascending or descending order, and using several variables.\n",
"By default the results are ordered in ascending order, but you can indicate the order using an optional modifier (`ASC(<variable>)`, or `DESC(<variable>)`). \n"
]
]
},
},
{
{
@ -880,7 +881,7 @@
" rdfs:label \"Ringo Starr\" .\n",
" rdfs:label \"Ringo Starr\" .\n",
"```\n",
"```\n",
"\n",
"\n",
"Using this structure, and the SPARQL statements you already know, to get the **names** of all musicians that collaborated in at least one song.\n"
"Using this structure, and the SPARQL statements you already know, get the **names** of all musicians that collaborated in at least one song.\n"
]
]
},
},
{
{
@ -954,13 +955,13 @@
"\n",
"\n",
"Results can be aggregated using different functions.\n",
"Results can be aggregated using different functions.\n",
"One of the simplest functions is `COUNT`.\n",
"One of the simplest functions is `COUNT`.\n",
"The syntax for COUNT is:\n",
"The syntax for `COUNT` is:\n",
" \n",
" \n",
"```sparql\n",
"```sparql\n",
"SELECT (COUNT(?variable) as ?count_name)\n",
"SELECT (COUNT(?variable) as ?count_name)\n",
"```\n",
"```\n",
"\n",
"\n",
"Use `COUNT` to get the number of songs in which Ringo collaborated."
"Use `COUNT` to get the number of songs in which Ringo collaborated. Your query should return a column named `number`."
]
]
},
},
{
{
@ -1143,7 +1144,9 @@
"Now, use the same principle to get the count of **different** instruments in each song.\n",
"Now, use the same principle to get the count of **different** instruments in each song.\n",
"Some songs have several musicians playing the same instrument, but we only care about *different* instruments in each song.\n",
"Some songs have several musicians playing the same instrument, but we only care about *different* instruments in each song.\n",
"\n",
"\n",
"Use `?number` for the count."
"Use `?song` for the song and `?number` for the count.\n",
"\n",
"Take into consideration that instruments are entities of type `i:Instrument`."
]
]
},
},
{
{
@ -1153,7 +1156,7 @@
"deletable": false,
"deletable": false,
"nbgrader": {
"nbgrader": {
"cell_type": "code",
"cell_type": "code",
"checksum": "2d0633303eedd0655e9b64bb00317dba",
"checksum": "3139d9b7e620266946ffe1ae0cf67581",
"grade": false,
"grade": false,
"grade_id": "cell-ee208c762d00da9c",
"grade_id": "cell-ee208c762d00da9c",
"locked": false,
"locked": false,
@ -1173,6 +1176,8 @@
" [] a s:Song ;\n",
" [] a s:Song ;\n",
" rdfs:label ?song ;\n",
" rdfs:label ?song ;\n",
" ?instrument ?musician .\n",
" ?instrument ?musician .\n",
" \n",
"?instrument a s:Instrument .\n",
"}\n",
"}\n",
"# YOUR ANSWER HERE\n",
"# YOUR ANSWER HERE\n",
"ORDER BY DESC(?number)"
"ORDER BY DESC(?number)"
@ -1186,7 +1191,7 @@
"editable": false,
"editable": false,
"nbgrader": {
"nbgrader": {
"cell_type": "code",
"cell_type": "code",
"checksum": "301aa479241fa02534ee047cf7577eee",
"checksum": "5abf6eb7a67ebc9f7612b876105c1960",
"grade": true,
"grade": true,
"grade_id": "cell-ddeec32b8ac3d894",
"grade_id": "cell-ddeec32b8ac3d894",
"locked": true,
"locked": true,
@ -1198,7 +1203,7 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"s = solution()\n",
"s = solution()\n",
"assert s['columns']['number'][0] == '27'"
"assert s['columns']['number'][0] == '25'"
]
]
},
},
{
{
@ -1243,10 +1248,10 @@
"metadata": {},
"metadata": {},
"source": [
"source": [
"However, there are some songs that do not have a vocalist (at least, in the dataset).\n",
"However, there are some songs that do not have a vocalist (at least, in the dataset).\n",
"Those songs will not appear in the list above, because we they do not match part of the `WHERE` clause.\n",
"Those songs will not appear in the list above, because they do not match part of the `WHERE` clause.\n",
"\n",
"\n",
"In these cases, we can specify optional values in a query using the `OPTIONAL` keyword.\n",
"In these cases, we can specify optional values in a query using the `OPTIONAL` keyword.\n",
"When a set of clauses are inside an OPTIONAL group, the SPARQL endpoint will try to use them in the query.\n",
"When a set of clauses are inside an `OPTIONAL` group, the SPARQL endpoint will try to use them in the query.\n",
"If there are no results for that part of the query, the variables it specifies will not be bound (i.e. they will be empty).\n",
"If there are no results for that part of the query, the variables it specifies will not be bound (i.e. they will be empty).\n",
"\n",
"\n",
"To exemplify this, we can use a property that **does not exist in the dataset**:"
"To exemplify this, we can use a property that **does not exist in the dataset**:"
@ -1504,7 +1509,9 @@
"source": [
"source": [
"Now, count how many instruments each musician have played in a song.\n",
"Now, count how many instruments each musician have played in a song.\n",
"\n",
"\n",
"**Do not count lead (`i:vocals`) or backing vocals (`i:backingvocals`) as instruments**."
"**Do not count lead (`i:vocals`) or backing vocals (`i:backingvocals`) as instruments**.\n",
"\n",
"Use `?musician` for the musician and `?number` for the count."
]
]
},
},
{
{
@ -1770,7 +1777,9 @@
"\n",
"\n",
"Using `GROUP_CONCAT`, get a list of the instruments that each musician could play.\n",
"Using `GROUP_CONCAT`, get a list of the instruments that each musician could play.\n",
"\n",
"\n",
"You can consult how to use GROUP_CONCAT [here](https://www.w3.org/TR/sparql11-query/)."
"You can consult how to use GROUP_CONCAT [here](https://www.w3.org/TR/sparql11-query/).\n",
"\n",
"Use `?musician` for the musician and `?instruments` for the list of instruments."
]
]
},
},
{
{
@ -1815,7 +1824,9 @@
"\n",
"\n",
"You can check if a string or URI matches a regular expression with `regex(?variable, \"<regex>\", \"i\")`.\n",
"You can check if a string or URI matches a regular expression with `regex(?variable, \"<regex>\", \"i\")`.\n",
"\n",
"\n",
"The documentation for regular expressions in SPARQL is [here](https://www.w3.org/TR/rdf-sparql-query/)."
"The documentation for regular expressions in SPARQL is [here](https://www.w3.org/TR/rdf-sparql-query/).\n",
"\n",
"Use `?instrument` for the instrument and `?ins` for the url of the type."
"Let's start with a simple query. We will get a list of cities and towns in Madrid.\n",
"Let's start with a simple query. We will get a list of towns and other populated areas within the Community of Madrid.\n",
"If we take a look at the DBpedia ontology or the page of any town we already know, we discover that the property that links towns to their community is [`isPartOf`](http://dbpedia.org/ontology/isPartOf), and [the Community of Madrid is also a resource in DBpedia](http://dbpedia.org/resource/Community_of_Madrid)\n",
"If we take a look at the DBpedia ontology, or the page of any town we already know, we discover that the property that links towns to their community is [`subdivision`](http://dbpedia.org/ontology/subdivision), and [the Community of Madrid is also a resource in DBpedia](http://dbpedia.org/resource/Community_of_Madrid)\n",
"\n",
"\n",
"Since there are potentially many cities to get, we will limit our results to the first 10 results:"
"Since there are potentially many cities to get, we will limit our results to the first 10 results:"