0% found this document useful (0 votes)
16 views40 pages

L28 Irsw Sparql

The document discusses SPARQL queries and graph patterns. It provides examples of SELECT queries to retrieve information from an RDF graph about albums and artists. It also describes how to filter, order, slice and aggregate query results.

Uploaded by

aastha garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views40 pages

L28 Irsw Sparql

The document discusses SPARQL queries and graph patterns. It provides examples of SELECT queries to retrieve information from an RDF graph about albums and artists. It also describes how to filter, order, slice and aggregate query results.

Uploaded by

aastha garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

INFORMATION RETRIEVAL

AND SEMANTIC WEB


16B1NCI648
SPARQL
RDF GRAPH (EG : BEATLE BAND)
SELECT QUERIES

• A SELECT query has two main components: a list of selected


variables and
• a WHERE clause for specifying the graph patterns to match:
SELECT <variables>
WHERE {
<graph pattern>
}

• The result of a SELECT query is a table where there will be one


column for each selected variable and one row for each pattern
match
TRIPLE PATTERNS

• # 01a-albums.sparql
• # dataset: beatles

SELECT ?album
WHERE {
?album rdf:type :Album .
}

album
:Please_Please_Me
• There are several syntactic simplifications we can do to this query:
• the WHERE keyword is optional for SELECT queries and
can be omitted
• If all the variables are being selected, then * can be used.
• Just like in RDF, the keyword a can be used instead
of rdf:type
RDF GRAPH (EG : BEATLE BAND)
SELECT ?album
WHERE {
?album rdf:type :Album .
}
Can be written as

# 01b-albums.sparql
# dataset: beatles
SELECT * { ?album a :Album }
BASIC GRAPH PATTERN

# 02-albums-artists.sparql
# dataset: beatles
SELECT *
{
?album a :Album .
?album :artist ?artist .
}

The second triple pattern in this query will match the triples
with :artist predicate
album artist
:Please_Please_Me :The_Beatles
• Now let’s add a third triple pattern to require that the returned artists should be of
the SoloArtist type:
# 03-albums-solo-artists.sparql
# dataset: beatles
SELECT *
{
?album a :Album .
?album :artist ?artist .
?artist a :SoloArtist .
}

The third pattern matches four triples in our graph


SELECT *
{
?album a :Album .
?album :artist ?artist .
?artist a :SoloArtist .
}
• If the subject is same the query can be written as
SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date .
}
ORDERING RESULTS

If we want the results to be ordered based on a sorting


condition we can add an ORDER BY:
# 05-albums-dates-sorted.sparql
# dataset: music
SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date .
}
ORDER BY ?date
SLICING RESULTS

# 06-albums-dates-limited.sparql
# dataset: music

SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
}
ORDER BY desc(?date)
LIMIT 2

• When a query returns too many results we can limit the results by
using the LIMIT keyword:
FILTERING RESULTS

• We can filter the results returned by a query


using a FILTER expression. SPARQL supports
many built-in functions for writing such
expressions:
• comparison operators: (=, !=, <, <=, >, >=)
• logical operators (&&, ||, !)
• mathematical operators (+, -, /, *)
• Query: If we want to find the albums released in 1970 or later we
can do this with the following filter expression:

SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
FILTER (?date >= "1970-01-01"^^xsd:date)
}
ORDER BY ?date
FILTER BASED ON YEAR

SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
FILTER (year(?date) >= 1970)
}
ORDER BY ?date

If we want to extract details based on year


BIND THE OUTPUT OF YEAR(?DATE) TO NEW VARIABLE
?YEAR AND USING THAT IN FILTER EXP

SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
BIND (year(?date) as ?year)
FILTER (?year >= 1970)
}
• If we want to display the years in which album released
• SELECT ?year
{
?album a :Album ;
:artist ?artist ;
:date ?date
BIND (year(?date) as ?year)
}

We might get
1956
1956
1957
REMOVE DUPLICATES

• SELECT DISTINCT ?year


{
?album a :Album ;
:artist ?artist ;
:date ?date
BIND (year(?date) as ?year)
}
AGGREGATION

• Built-in aggregates provided in SPARQL


are COUNT, SUM, MIN, MAX, AVG, GROUP_CONCAT,
and SAMPLE.
• Query: We can find the earliest and the latest release dates of
albums.
SELECT (min(?date) as ?minDate) (max(?date) as ?maxDate)
{
?album a :Album ;
:date ?date
}
• Query: return the number of rows in the result
table

SELECT (count(?album) as ?count)


{
?album a :Album
}
GROUPING

• Find the number of albums released each year

• SELECT ?year (count(distinct ?album)


{
?album a :Album ;
:date ?date
BIND (year(?date) as ?year)
}
Group by ?year
• Find the year in which number of albums released are more than 1

• SELECT ?year (count(distinct ?album)


{
?album a :Album ;
:date ?date
BIND (year(?date) as ?year)
}
Group by ?year

Having (count(?date)>1)
PRACTICE
QUERIES OVER THIS DATA
Find the location of the Baron Way building.
Find where the Baron Way Apartment is located.
Find all the information about Baron Way Apartment in
the triple store
Find 10 entries of the information about Baron
Way Apartment
Find all the apartments which are part of a
building located in Amsterdam.
Find all the apartments that have 3 bedrooms.
find all the apartments that have greater than 2
bedrooms
QUERIES OVER THIS DATA

Add one more triple in RDF:


The regular expression for finding the string “4 Baron Way” at the
beginning of another string is “ ˆ4 Baron Way”.
Search for Baron in the URL of the resource instead of using the
label
Queries over this data
Find the apartments located in Amsterdam and return
their human-friendly label if they have one
Find the apartments ordered by the number of bedrooms
Compute the average number of bedrooms in our
dataset.
• https://medium.com/wallscope/constructing-sparql-queries-
ca63b8b9ac02

You might also like