SPARQL basic mode query

Keywords: SQL Apache

The last one is a brief introduction to SPARQL. This article introduces the basic schema query of SPARQL

Still use the example from the previous article:

  <rdf:Description rdf:about="http://somewhere/JohnSmith/">  
    <vCard:FN>John Smith</vCard:FN>  
    <vCard:N rdf:parseType="Resource">  
  <rdf:Description rdf:about="http://somewhere/RebeccaSmith/">  
    <vCard:FN>Becky Smith</vCard:FN>  
    <vCard:N rdf:parseType="Resource">  
  <rdf:Description rdf:about="http://somewhere/SarahJones/">  
    <vCard:FN>Sarah Jones</vCard:FN>  
    <vCard:N rdf:parseType="Resource">  
  <rdf:Description rdf:about="http://somewhere/MattJones/">  
    <vCard:FN>Matt Jones</vCard:FN>  
The picture shows:

RDF is known as some triples. For the previous example, if you want to find the Subject and Object of all FullName (predicate)

Use the following statement to find:

SELECT ?x ?name
WHERE {?x  <>  ?name}

We learned from the previous article that Select is used to find the required content, and Where statement is the search condition, which is similar to Sql statement.

Where {? x < ා FN >? Name} in this statement, "x" is the Subject to be searched, "name" is the Object to be searched, " ා FN > is the required condition.

The results are as follows:

| x                                | name          |
| <http://somewhere/RebeccaSmith/> | "Becky Smith" |
| <http://somewhere/SarahJones/>   | "Sarah Jones" |
| <http://somewhere/JohnSmith/>    | "John Smith"  |
| <http://somewhere/MattJones/>    | "Matt Jones"  |

Isn't it so simple up to now. Come on, keep looking down...

What if we want to do two-step query? If you want to know that FamilyName is Smith's, who is GivenName? The answer is Rebecca and John

The query statement is as follows:

SELECT ?givenName
  { ?y  <>  "Smith" .
    ?y  <>  ?givenName .

| givenName |
| "John"    |
| "Rebecca" |
We can also search for blank nodes: for example, to find a blank node whose FamilyName is Smith:

Query code:

SELECT ?y ?givenName
 { ?y vcard:Family "Smith" .
   ?y vcard:Given  ?givenName .

| y    | givenName |
| _:b0 | "John"    |
| _:b1 | "Rebecca" |
By the way, I forgot to use SparQL with Jena package, which can be downloaded from

Posted by Levan on Fri, 01 May 2020 22:27:02 -0700