際際滷

際際滷Share a Scribd company logo
An Introduction to SPARQL Optionals
Julian Dolby
Kavitha Srinivas
Why Do We Need OPTIONALS?
Deals with sparsity in graph data. A simple example where
this is useful:
John age 42
John lastName Smith
John spouse Mary
John homeState New_York
New_York isIn NorthEast
Todd age 42
Todd lastName Doe
Todd homeState Connecticut
Connecticut isIn NorthEast
Tom age 42
Say we want to 鍖nd all the people who are aged 42 and live in
the NorthEast, and optionally their spouses names, if its
known. OPTIONAL allows you to express that.
What does it mean?
The best way to understand what an optional means
is to think of it as extending the immediately
preceding pattern.
And extending it means that it can bind variables
that have not been bound in its preceding pattern;
for variables that have been bound by the preceding
pattern, the optional must match the value
A working dataset
AS mailbox mailto:alice@example.net
AS name Alice
AS nick WhoMe?
BD mailbox mailto:bert@example.net
BD name Bert
EF mailbox mailto:eve@example.net
EF nick DuckSoup
Many of the examples here are from Jenas ARQ suite
The SIMPLEST CASE
Simplest case: optional pattern extends bindings from the
preceding pattern, and provides binding for 'name' in the
OPTIONAL pattern.
ResultSet:
SELECT ?x ?mbox ?name
{
?x mailbox ?mbox .
OPTIONAL { ?x name ?name } .
}
x mbox name
AS mailto:alice@example.net Alice
BD mailto:bert@example.net Bert
EF mailto:eve@example.net NULL
Multiple OPTIONALS
Optional left-associative (like minus), so a optional b
optional c is really ((a optional b) optional c).
ResultSet:
SELECT ?x ?mailbox ?name ?nick
{
?x mailbox ?mbox .
OPTIONAL { ?x name ?name } .
OPTIONAL { ?x nick ?nick }.
}
x mbox name nick
AS mailto:alice@example.net Alice WhoMe?
BD mailto:bert@example.net Bert NULL
EF mailto:eve@example.net NULL DuckSoup
Multiple OPTIONALS Contd.
Note that the second optional binds values to a variable bound by another optional. Thus,
the inner pattern will bind x, mbox and optionally label. If it binds label, then the outer
pattern has to match it and cannot extend it. So result will have all mbox properties, and
all name properties that have an mbox. The outer pattern extends these results. If a
subject has no name property, then any nick property for it will be returned. If it does
have a name property, then a nick property will be returned only if its target matches a
name property for the same subject.
However, only label is projected. So then, if there is a name property, label will be bound
to that. If not, then it will bind any nick property. Thus, for Alice, we should see the
name and no nickname.
ResultSet
SELECT ?label
{
?x mailbox ?mbox .
OPTIONAL { ?x name ?label } .
OPTIONAL { ?x nick ?label } .
}
Label
Alice
Bert
DuckSoup
AS, BD, EF all have mailboxes. All should match for main.
Optional 1: Extends with name for AS and BD, null for EF.
Optional 2: Extends optional 1 result set with nickname for EF, since
for the AS and BD, nick HAS to match bindings of label in optional 1.
Multiple OPTIONALS Contd.
SELECT ?label
{
?x mailbox ?mbox .
OPTIONAL { ?x name ?label } .
OPTIONAL { ?x nick ?label } .
}
SELECT ?label
{
?x mailbox ?mbox .
OPTIONAL {
{ ?x name ?label } UNION
{ ?x nick ?label } .
}
}
Multiple OPTIONAL clauses binding the same variable is similar to yet subtly different
from a UNION inside the OPTIONAL. The lefthand query gets nick labels for
mailboxes that have no name; the righthand query gets all nick and name labels for any
mailbox.
Label
Alice
Bert
DuckSoup
Label
Alice
Bert
DuckSoup
WhoMe?
Multiple OPTIONAL clauses binding the same variable is rarely what you want
OPTIONAL WITH EMPTY
Here, no pattern to extend (i.e., the pattern is empty). An
empty pattern binds no variables, so the optional extends
the empty result set with any of its variables. Note this is
equivalent to dropping the OPTIONAL altogether.
ResultSet:
SELECT ?x ?nick
{
OPTIONAL { ?x nick ?nick }.
}
x nick
AS WhoMe?
EF DuckSoup
NESTING OPTIONALS
In the nested optional case, we have (a OPTIONAL (b OPTIONAL c)). Note this
differs from the case where we have two optionals that are not nested. In this case, the
main pattern once again matches all three individuals, but the optionals being nested
means the second optional has the result of the 鍖rst optional as its main pattern. Thus, the
results have mbox, and optionally nick, and possibly name if it has nick too. In particular,
the name of bert is not returned, since it has no nick property. Note that b 鍖rst extends a,
and then c will extend b.
ResultSet
SELECT ?x ?mbox ?name ?nick
{
?x mailbox ?mbox .
OPTIONAL {
?x nick ?nick
OPTIONAL { ?x name ?name }
}
}
x mbox name nick
AS mailto:alice@example.net Alice WhoMe?
BD mailto:bert@example.net NULL NULL
EF mailto:eve@example.net NULL DuckSoup
NO-OP OPTIONALS
In this case, the optional does not really have any new variables to bind. ?nick is already
bound to Who Me, so the optional is a no-op.
ResultSet
SELECT ?nick
{
AS nick ?nick .
OPTIONAL {AS name ?nick}
}
nick
WhoMe?
More complex optionals
The optional pattern here contains an AND. That is, it can
extend bindings from the preceding pattern for ?x only
when ?x has both nick and name. Only Alice will have
bindings for ?nick and ?name.
ResultSet:
SELECT ?x ?mbox ? nick ?name
{
?x mailbox ?mbox .
OPTIONAL {
?x nick ?nick .
?x name ?name } .
}
x mbox name nick
AS mailto:alice@example.net Alice WhoMe?
BD mailto:bert@example.net NULL NULL
EF mailto:eve@example.net NULL NULL
OPTIONAL and FILTER
FILTER applies only to pattern containing it
OPTIONAL only vs. main pattern
SELECT ?x ?name
{
?x mailbox ?mbox .
OPTIONAL {
?x name ?name .
FILTER (?name = Bert)
} .
}
SELECT ?x ?name
{
?x mailbox ?mbox .
OPTIONAL {
?x name ?name .
}
FILTER (?name = Bert)
}
x name
AS NULL
EF NULL
BD Bert
x name
BD Bert
Disconnected Optionals
The optional pattern extends bindings from the preceding
pattern but shares no variables in common. Basically this
will be a cross product.
ResultSet:
SELECT ?x ?nick ?name
{
?x nick ?nick .
OPTIONAL { ?y name ?name } .
}
x nick y Name
AS WhoMe? AS Alice
AS WhoMe? BD Bert
EF DuckSoup AS Alice
EF DuckSoup BD Bert
Cross products generally perform terribly; avoid them
Disconnected Optionals
And filters
The optional pattern extends bindings from the preceding
pattern but shares no variables in common. Basically this
will be a cross product.
ResultSet:
SELECT ?x ?nick ?name
{
?x nick ?nick .
OPTIONAL { ?y name ?name FILTER (?x=AS) } .
}
x nick y Name
AS WhoMe? AS Alice
AS WhoMe? BD Bert
EF DuckSoup NULL NULL
OPTIONAL and negation
OPTIONAL can test for absence of triples
Uses FILTER and not(bound(?v))
FILTER for ?v to be NULL, i.e. not exist
SELECT ?x ?mbox
{
?x mailbox ?mbox .
OPTIONAL { ?x name ?name }
FILTER (not(bound(?name))
}
x mbox
EF mailto:eve@example.net
EF is the only entity with no name property
SPARQL 1.1 Negation
SPARQL 1.1 added explicit negation
obviates not(bound(?v))
SELECT ?x ?mbox
{
?x mailbox ?mbox .
FILTER NOT EXISTS {
?x name ?name }
}
x mbox
EF mailto:eve@example.net
EF is the only entity with no name property
SPARQL 1.1 Minus
MINUS computes set difference
expresses some forms of negation
SELECT ?x ?mbox
{
?x mailbox ?mbox .
MINUS {
?x name ?name
}
}
x mbox
EF mailto:eve@example.net
subtract entities with name from entities with mailbox
Ad

Recommended

Data Engineer's Lunch #81: Reverse ETL Tools for Modern Data Platforms
Data Engineer's Lunch #81: Reverse ETL Tools for Modern Data Platforms
Anant Corporation
Les outils OLAP
nadia sassi
BigData_Chp1: Introduction a la Big Data
Lilia Sfaxi
Base de donn辿es NoSQL
Oussama ARBI
6.hive
6.hive
Prashant Gupta
Data Platform Architecture Principles and Evaluation Criteria
Data Platform Architecture Principles and Evaluation Criteria
ScyllaDB
Bases de Donn辿es non relationnelles, NoSQL (Introduction) 1er cours
Hatim CHAHDI
Data Visualisation & Analytics with Tableau (Beginner) - by Maria Koumandraki
Data Visualisation & Analytics with Tableau (Beginner) - by Maria Koumandraki
Outreach Digital
Data Engineering Efficiency @ Netflix - Strata 2017
Data Engineering Efficiency @ Netflix - Strata 2017
Michelle Ufford
Domain Driven Design Made Functional with Python
Domain Driven Design Made Functional with Python
Jean Carlo Machado
Les Base de Donn辿es NOSQL -Presentation -
IliasAEA
Big Data and Machine Learning with FIWARE
Big Data and Machine Learning with FIWARE
Fernando Lopez Aguilar
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 Way
Debasish Ghosh
Intro to HBase
Intro to HBase
alexbaranau
Data Lakehouse, Data Mesh, and Data Fabric (r2)
Data Lakehouse, Data Mesh, and Data Fabric (r2)
James Serra
BigData_Chp2: Hadoop & Map-Reduce
Lilia Sfaxi
Data Warehouse Design and Best Practices
Data Warehouse Design and Best Practices
Ivo Andreev
SQL to Hive Cheat Sheet
SQL to Hive Cheat Sheet
Hortonworks
Les interface graphiques sous android
Houssem Lahiani
Introduction to HiveQL
Introduction to HiveQL
kristinferrier
Data Catalog for Better Data Discovery and Governance
Data Catalog for Better Data Discovery and Governance
Denodo
DEVOPS - La synth竪se
COMPETENSIS
Introduction to MongoDB.pptx
Introduction to MongoDB.pptx
Surya937648
Why Data Vault?
Why Data Vault?
Kent Graziano
Introduction Neo4j
Neo4j
Data Analysis Expressions (DAX) Training
Data Analysis Expressions (DAX) Training
Arocom IT Solutions Pvt. Ltd
Building an Enterprise Eventing Framework (Bryan Zelle, Centene; Neil Buesing...
Building an Enterprise Eventing Framework (Bryan Zelle, Centene; Neil Buesing...
confluent
Yarn by default (Spark on YARN)
Yarn by default (Spark on YARN)
Ferran Gal鱈 Reniu
Home Theater: Surround Sound Formats
Home Theater: Surround Sound Formats
Curt Robbins
Surround sount system
Surround sount system
Chetan Gakhare

More Related Content

What's hot (20)

Data Engineering Efficiency @ Netflix - Strata 2017
Data Engineering Efficiency @ Netflix - Strata 2017
Michelle Ufford
Domain Driven Design Made Functional with Python
Domain Driven Design Made Functional with Python
Jean Carlo Machado
Les Base de Donn辿es NOSQL -Presentation -
IliasAEA
Big Data and Machine Learning with FIWARE
Big Data and Machine Learning with FIWARE
Fernando Lopez Aguilar
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 Way
Debasish Ghosh
Intro to HBase
Intro to HBase
alexbaranau
Data Lakehouse, Data Mesh, and Data Fabric (r2)
Data Lakehouse, Data Mesh, and Data Fabric (r2)
James Serra
BigData_Chp2: Hadoop & Map-Reduce
Lilia Sfaxi
Data Warehouse Design and Best Practices
Data Warehouse Design and Best Practices
Ivo Andreev
SQL to Hive Cheat Sheet
SQL to Hive Cheat Sheet
Hortonworks
Les interface graphiques sous android
Houssem Lahiani
Introduction to HiveQL
Introduction to HiveQL
kristinferrier
Data Catalog for Better Data Discovery and Governance
Data Catalog for Better Data Discovery and Governance
Denodo
DEVOPS - La synth竪se
COMPETENSIS
Introduction to MongoDB.pptx
Introduction to MongoDB.pptx
Surya937648
Why Data Vault?
Why Data Vault?
Kent Graziano
Introduction Neo4j
Neo4j
Data Analysis Expressions (DAX) Training
Data Analysis Expressions (DAX) Training
Arocom IT Solutions Pvt. Ltd
Building an Enterprise Eventing Framework (Bryan Zelle, Centene; Neil Buesing...
Building an Enterprise Eventing Framework (Bryan Zelle, Centene; Neil Buesing...
confluent
Yarn by default (Spark on YARN)
Yarn by default (Spark on YARN)
Ferran Gal鱈 Reniu
Data Engineering Efficiency @ Netflix - Strata 2017
Data Engineering Efficiency @ Netflix - Strata 2017
Michelle Ufford
Domain Driven Design Made Functional with Python
Domain Driven Design Made Functional with Python
Jean Carlo Machado
Les Base de Donn辿es NOSQL -Presentation -
IliasAEA
Big Data and Machine Learning with FIWARE
Big Data and Machine Learning with FIWARE
Fernando Lopez Aguilar
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 Way
Debasish Ghosh
Intro to HBase
Intro to HBase
alexbaranau
Data Lakehouse, Data Mesh, and Data Fabric (r2)
Data Lakehouse, Data Mesh, and Data Fabric (r2)
James Serra
BigData_Chp2: Hadoop & Map-Reduce
Lilia Sfaxi
Data Warehouse Design and Best Practices
Data Warehouse Design and Best Practices
Ivo Andreev
SQL to Hive Cheat Sheet
SQL to Hive Cheat Sheet
Hortonworks
Les interface graphiques sous android
Houssem Lahiani
Introduction to HiveQL
Introduction to HiveQL
kristinferrier
Data Catalog for Better Data Discovery and Governance
Data Catalog for Better Data Discovery and Governance
Denodo
DEVOPS - La synth竪se
COMPETENSIS
Introduction to MongoDB.pptx
Introduction to MongoDB.pptx
Surya937648
Introduction Neo4j
Neo4j
Building an Enterprise Eventing Framework (Bryan Zelle, Centene; Neil Buesing...
Building an Enterprise Eventing Framework (Bryan Zelle, Centene; Neil Buesing...
confluent
Yarn by default (Spark on YARN)
Yarn by default (Spark on YARN)
Ferran Gal鱈 Reniu

Viewers also liked (8)

Home Theater: Surround Sound Formats
Home Theater: Surround Sound Formats
Curt Robbins
Surround sount system
Surround sount system
Chetan Gakhare
Dolby Laboratories Pitch
Dolby Laboratories Pitch
Matthew R. Thomas
Dolby atmos-cinema-technical-guidelines
Dolby atmos-cinema-technical-guidelines
Digital Innovation Groupware - Industries Techniques
Top Indian Brands on Social Media - June 2013
Top Indian Brands on Social Media - June 2013
Unmetric
Dolby atmos explained
Dolby atmos explained
Waqar Zahoor
Oscars 2016: Winners and Highlights
Oscars 2016: Winners and Highlights
maditabalnco
際際滷share ppt
際際滷share ppt
Mandy Suzanne
Home Theater: Surround Sound Formats
Home Theater: Surround Sound Formats
Curt Robbins
Surround sount system
Surround sount system
Chetan Gakhare
Top Indian Brands on Social Media - June 2013
Top Indian Brands on Social Media - June 2013
Unmetric
Dolby atmos explained
Dolby atmos explained
Waqar Zahoor
Oscars 2016: Winners and Highlights
Oscars 2016: Winners and Highlights
maditabalnco
際際滷share ppt
際際滷share ppt
Mandy Suzanne
Ad

Recently uploaded (20)

Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Josef Weingand
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Josef Weingand
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
Ad

Using SPARQL Optionals

  • 1. An Introduction to SPARQL Optionals Julian Dolby Kavitha Srinivas
  • 2. Why Do We Need OPTIONALS? Deals with sparsity in graph data. A simple example where this is useful: John age 42 John lastName Smith John spouse Mary John homeState New_York New_York isIn NorthEast Todd age 42 Todd lastName Doe Todd homeState Connecticut Connecticut isIn NorthEast Tom age 42 Say we want to 鍖nd all the people who are aged 42 and live in the NorthEast, and optionally their spouses names, if its known. OPTIONAL allows you to express that.
  • 3. What does it mean? The best way to understand what an optional means is to think of it as extending the immediately preceding pattern. And extending it means that it can bind variables that have not been bound in its preceding pattern; for variables that have been bound by the preceding pattern, the optional must match the value
  • 4. A working dataset AS mailbox mailto:alice@example.net AS name Alice AS nick WhoMe? BD mailbox mailto:bert@example.net BD name Bert EF mailbox mailto:eve@example.net EF nick DuckSoup Many of the examples here are from Jenas ARQ suite
  • 5. The SIMPLEST CASE Simplest case: optional pattern extends bindings from the preceding pattern, and provides binding for 'name' in the OPTIONAL pattern. ResultSet: SELECT ?x ?mbox ?name { ?x mailbox ?mbox . OPTIONAL { ?x name ?name } . } x mbox name AS mailto:alice@example.net Alice BD mailto:bert@example.net Bert EF mailto:eve@example.net NULL
  • 6. Multiple OPTIONALS Optional left-associative (like minus), so a optional b optional c is really ((a optional b) optional c). ResultSet: SELECT ?x ?mailbox ?name ?nick { ?x mailbox ?mbox . OPTIONAL { ?x name ?name } . OPTIONAL { ?x nick ?nick }. } x mbox name nick AS mailto:alice@example.net Alice WhoMe? BD mailto:bert@example.net Bert NULL EF mailto:eve@example.net NULL DuckSoup
  • 7. Multiple OPTIONALS Contd. Note that the second optional binds values to a variable bound by another optional. Thus, the inner pattern will bind x, mbox and optionally label. If it binds label, then the outer pattern has to match it and cannot extend it. So result will have all mbox properties, and all name properties that have an mbox. The outer pattern extends these results. If a subject has no name property, then any nick property for it will be returned. If it does have a name property, then a nick property will be returned only if its target matches a name property for the same subject. However, only label is projected. So then, if there is a name property, label will be bound to that. If not, then it will bind any nick property. Thus, for Alice, we should see the name and no nickname. ResultSet SELECT ?label { ?x mailbox ?mbox . OPTIONAL { ?x name ?label } . OPTIONAL { ?x nick ?label } . } Label Alice Bert DuckSoup AS, BD, EF all have mailboxes. All should match for main. Optional 1: Extends with name for AS and BD, null for EF. Optional 2: Extends optional 1 result set with nickname for EF, since for the AS and BD, nick HAS to match bindings of label in optional 1.
  • 8. Multiple OPTIONALS Contd. SELECT ?label { ?x mailbox ?mbox . OPTIONAL { ?x name ?label } . OPTIONAL { ?x nick ?label } . } SELECT ?label { ?x mailbox ?mbox . OPTIONAL { { ?x name ?label } UNION { ?x nick ?label } . } } Multiple OPTIONAL clauses binding the same variable is similar to yet subtly different from a UNION inside the OPTIONAL. The lefthand query gets nick labels for mailboxes that have no name; the righthand query gets all nick and name labels for any mailbox. Label Alice Bert DuckSoup Label Alice Bert DuckSoup WhoMe? Multiple OPTIONAL clauses binding the same variable is rarely what you want
  • 9. OPTIONAL WITH EMPTY Here, no pattern to extend (i.e., the pattern is empty). An empty pattern binds no variables, so the optional extends the empty result set with any of its variables. Note this is equivalent to dropping the OPTIONAL altogether. ResultSet: SELECT ?x ?nick { OPTIONAL { ?x nick ?nick }. } x nick AS WhoMe? EF DuckSoup
  • 10. NESTING OPTIONALS In the nested optional case, we have (a OPTIONAL (b OPTIONAL c)). Note this differs from the case where we have two optionals that are not nested. In this case, the main pattern once again matches all three individuals, but the optionals being nested means the second optional has the result of the 鍖rst optional as its main pattern. Thus, the results have mbox, and optionally nick, and possibly name if it has nick too. In particular, the name of bert is not returned, since it has no nick property. Note that b 鍖rst extends a, and then c will extend b. ResultSet SELECT ?x ?mbox ?name ?nick { ?x mailbox ?mbox . OPTIONAL { ?x nick ?nick OPTIONAL { ?x name ?name } } } x mbox name nick AS mailto:alice@example.net Alice WhoMe? BD mailto:bert@example.net NULL NULL EF mailto:eve@example.net NULL DuckSoup
  • 11. NO-OP OPTIONALS In this case, the optional does not really have any new variables to bind. ?nick is already bound to Who Me, so the optional is a no-op. ResultSet SELECT ?nick { AS nick ?nick . OPTIONAL {AS name ?nick} } nick WhoMe?
  • 12. More complex optionals The optional pattern here contains an AND. That is, it can extend bindings from the preceding pattern for ?x only when ?x has both nick and name. Only Alice will have bindings for ?nick and ?name. ResultSet: SELECT ?x ?mbox ? nick ?name { ?x mailbox ?mbox . OPTIONAL { ?x nick ?nick . ?x name ?name } . } x mbox name nick AS mailto:alice@example.net Alice WhoMe? BD mailto:bert@example.net NULL NULL EF mailto:eve@example.net NULL NULL
  • 13. OPTIONAL and FILTER FILTER applies only to pattern containing it OPTIONAL only vs. main pattern SELECT ?x ?name { ?x mailbox ?mbox . OPTIONAL { ?x name ?name . FILTER (?name = Bert) } . } SELECT ?x ?name { ?x mailbox ?mbox . OPTIONAL { ?x name ?name . } FILTER (?name = Bert) } x name AS NULL EF NULL BD Bert x name BD Bert
  • 14. Disconnected Optionals The optional pattern extends bindings from the preceding pattern but shares no variables in common. Basically this will be a cross product. ResultSet: SELECT ?x ?nick ?name { ?x nick ?nick . OPTIONAL { ?y name ?name } . } x nick y Name AS WhoMe? AS Alice AS WhoMe? BD Bert EF DuckSoup AS Alice EF DuckSoup BD Bert Cross products generally perform terribly; avoid them
  • 15. Disconnected Optionals And filters The optional pattern extends bindings from the preceding pattern but shares no variables in common. Basically this will be a cross product. ResultSet: SELECT ?x ?nick ?name { ?x nick ?nick . OPTIONAL { ?y name ?name FILTER (?x=AS) } . } x nick y Name AS WhoMe? AS Alice AS WhoMe? BD Bert EF DuckSoup NULL NULL
  • 16. OPTIONAL and negation OPTIONAL can test for absence of triples Uses FILTER and not(bound(?v)) FILTER for ?v to be NULL, i.e. not exist SELECT ?x ?mbox { ?x mailbox ?mbox . OPTIONAL { ?x name ?name } FILTER (not(bound(?name)) } x mbox EF mailto:eve@example.net EF is the only entity with no name property
  • 17. SPARQL 1.1 Negation SPARQL 1.1 added explicit negation obviates not(bound(?v)) SELECT ?x ?mbox { ?x mailbox ?mbox . FILTER NOT EXISTS { ?x name ?name } } x mbox EF mailto:eve@example.net EF is the only entity with no name property
  • 18. SPARQL 1.1 Minus MINUS computes set difference expresses some forms of negation SELECT ?x ?mbox { ?x mailbox ?mbox . MINUS { ?x name ?name } } x mbox EF mailto:eve@example.net subtract entities with name from entities with mailbox