SPARQL Answer Lab
Unit 6 of 622 min

Solve a New Case

How many people borrowed a book by Maya? How would you know your query is right?

Try this case after practising a join, a count and a change to the data.

Sections in this chapter
  1. Turn the question into three decisions
  2. Draw the requirements for one session
  3. Count the right column
  4. Find an error that the count hides
  5. Choose a fact that tests the missing link
  6. Take the three decisions into the library
  7. Practice and check your reasoning

Turn the question into three decisions

A library asks how many people borrowed a book by Maya. You will check a query for the library later on this page. First, try the same method with activity sessions. You can work through the example or go straight to the library and return here whenever you need help.

Suppose the question is “How many people joined an art session at EastHall?” The question gives you a person, an activity they joined, and two requirements about that activity. Read it slowly and notice the link: both “art” and “at EastHall” describe the session that the person actually joined.

Break the question apart in English before writing a pattern. This makes it easier to see which parts name an exact value and which are things the graph has to find. Here, Art and EastHall are supplied by the question. The person and the session are unknown.

The same reading helps with the answer format. The question asks how many people qualify. A person with one qualifying session counts as one person. A second qualifying session gives another piece of evidence about that same person. You can keep both pieces of evidence while counting the person once.

The records tell us who joined each session and where that session took place. To decide whether a particular person qualifies, we need one participation fact tied to the two features of the session. A venue listing on its own leaves the question of who joined it open.

Three decisions to make before trusting a count
DecideIn the rehearsalCheck
Which object must stay the same?The sessionTopic and venue both describe the session joined.
What counts as one?A personOne person counts once across qualifying sessions.
What change might expose an error?A person joins an art session elsewhereThe correct result should still exclude that person.

For the final decision, imagine that a query loses a connection. The weaker query might be able to find a session at EastHall anywhere in the graph. A useful test is to give it a new person whose own session is somewhere else. In the rehearsal we will see why this reveals the defect.

You can use this table as a starting checklist. You will have a reason for each answer when you can point to the object both facts describe, the column that supplies the value to count, and the fact that lets a disconnected query pick up an extra value.

Draw the requirements for one session

Start with Ava’s participation. Ava joined two sessions, Drawing and Collage. Those are two different facts, shown below. In each, the end of the arrow is a session. The other information we need belongs to that session, so carry its name into the topic and venue checks.

  • AvajoinedDrawing
  • AvajoinedCollage
Read each participation arrow first. Carry the named session to the topic and venue checks.
Topic and venue recorded for each session
SessionTopicVenue
DrawingArtEastHall
CollageArtEastHall
SketchingArtWestHall

Follow the Drawing route first. Drawing is an Art session, so it meets the topic requirement. Drawing is also at EastHall, so it meets the venue requirement. Both claims are about Drawing itself. That route provides a complete reason for including Ava.

Repeat the checks for Collage. The topic and venue also fit. If Ava had joined a third session, you would have to check that third session’s own topic and venue. Finding an Art label somewhere in the table leaves the venue and the connection to Ava to check.

  • AvajoinedDrawing
  • Drawinghas topicArt
  • Drawingtakes place atEastHall
Three facts support a single match. Drawing is the session in all three.

Translate those checks into three lines. Use ?person for the participant and ?session for the session. Draw a boundary around the three occurrences of the session name when you read the code. At each occurrence, the match must keep the same session value.

?person ex:joined ?session .
?session ex:hasTopic ex:Art .
?session ex:takesPlaceAt ex:EastHall .

The last two arrows start at the session. They lead to the fixed values named by the question. A reader checking the answer can stop at a candidate session, find those two arrows, and say exactly which requirement each arrow supports.

Sketching gives you a useful case to think about. Its topic is Art and its venue is WestHall. If somebody only joined Sketching, the topic part would match but the EastHall part would still need a fact about Sketching. Try following the signs from that session before you check the answer.

Try pointing to each requirement as you read the question again. “Joined” corresponds to the arrow out of a person. “Art” and “at EastHall” both correspond to arrows out of the session at the other end. This is a useful check whenever an English sentence packs several requirements into a few words.

Reading the participation line first makes the example easy to follow. The test for a complete row is that all three lines hold with the same values in their shared slots. You can check the lines in a different order and the same complete assignments still qualify.

Check whether Sketching meets the requirements

Sketching meets the topic requirement. Its venue fact points to WestHall. A person with only this session in the graph does not have a supported match to all three lines.

Count the right column

Write one row for every complete way of making the three lines true. In the example both Drawing and Collage produce a row. Compare them by reading across. The person value is Ava on both rows. The session value changes because Ava has two qualifying participation routes.

Complete matches before choosing what to count
PersonSessionTopic and venue
AvaDrawingArt at EastHall
AvaCollageArt at EastHall

The table answers a different question depending on what you count. If you are checking how many qualifying participation matches the graph supports, you can count two. If you are reporting the number of different people who satisfy the requirements, both rows provide evidence about just Ava.

Try reading the Person column out loud. It says “Ava, Ava.” Each copy comes from its own match. Now imagine writing one name tag for every person who qualifies. You would write Ava once. That distinction is why the question needs COUNT(DISTINCT ?person).

Each person value is bound in this example. COUNT(?person) counts both appearances because it sees a value on each row. COUNT(DISTINCT ?person) counts the one different bound value. Put the unit next to the number when checking your result: two matches, one person.

Two ways to summarise the same matches
QuestionValues countedNumber
How many bound person values occur across the matches?Ava, Ava2
How many different people qualify?Ava1

A common stopping point is a report that shows the number without the evidence behind it. If somebody asks about the answer, reconstruct the Person column and show where each value came from. This is useful if the same result could have been obtained by a query using the wrong condition.

Before moving on, consider a change: another person joins Drawing. The topic and venue of Drawing stay the same. Think about whether this adds a new person value as well as another complete match, then check your reasoning.

If you needed names as well as a number, you could display the different person values themselves. The rehearsal asks for a total, so the output is a count. During checking, keep a list of those distinct values at hand to see which person was added or removed.

Check the new participation count

There would be three matching rows and two different people. The new person has one qualifying session. The Drawing and Collage rows for Ava still count as one person between them.

Find an error that the count hides

Imagine a query that asks for the topic of ?session but checks the venue of ?otherSession. The participation line finds the first session and the topic line still uses it. The misplaced name on the venue line gives the query another choice to make. That choice has no requirement tying it to the participation fact.

Same session

?session ex:takesPlaceAt ex:EastHall .

Separate session

?otherSession ex:takesPlaceAt ex:EastHall .
  • AvajoinedCollage
  • Collagehas topicArt
  • Drawingtakes place atEastHall
One match using separate names: Collage supplies the topic while Drawing supplies the venue.

In this example Collage really is at EastHall. That makes the mix easy to miss when reading only the final user count. Yet a venue fact about Drawing gives no evidence about the venue of Collage. Use the match row to ask which session the query actually checked.

For each of Ava’s two participation routes, the broken query can fill ?otherSession with Drawing or Collage. That gives four complete assignments. Each names Ava as the person. After counting different people, the broken query therefore gives the same starting number as the repaired one.

Assignments used for the different venue choices
Joined sessionAvailable venue choices in shared versionAvailable venue choices in separate version
DrawingDrawingDrawing or Collage
CollageCollageDrawing or Collage

Compare the lines with the actual question. The wording “an art session at EastHall” describes one session. There is no independent request to find another session at EastHall. The query needs to keep the condition attached to the session the person joined.

This is one more reason to keep a detailed view of the results while testing. The final count collects both matching rows for Ava into one value. A row that uses an independent venue can disappear from view even when it is available as a separate assignment underneath.

A promising test would offer the query a new person and a session that passes the topic check without meeting the venue condition. That will reveal whether a venue elsewhere can rescue a route that the question should leave out.

To check any one row in the separate version, write the chosen session and the chosen venue session next to each other. When they differ, ask for the missing evidence: is EastHall recorded for the joined session itself? A mismatch tells you which line to inspect, even before the reported count changes.

Choose a fact that tests the missing link

Consider two possible additions to the starting graph. Either Mina joined Drawing or Mina joined Sketching. Try one addition at a time. Predict which choice would make the displayed counts disagree. Check whether the joined session itself meets both requirements before looking for an unrelated venue.

Get a hint for choosing a test fact

Check the room on Mina’s own session. The two queries behave differently when the separate version can use an EastHall fact about a different session.

If Mina joins Drawing, Drawing meets both requirements. The new participant is eligible under both queries. The correct count increases from one to two people, and so does the count from the query with separate names. That addition cannot demonstrate a difference in the displayed count.

Now add Mina’s participation in Sketching to the starting graph instead. Sketching’s topic fits. Its venue is WestHall. Read the bottom arrow and decide if the repaired query has an EastHall fact about the session that Mina actually joined.

  • Minajoined · addedSketching
  • Sketchinghas topicArt
  • Sketchingtakes place atWestHall
The new participation leads to an Art session, but its venue is WestHall.

The repaired query excludes Mina. It still finds Ava through Drawing and Collage. The separate version can pair Mina’s Sketching route with an EastHall venue on Drawing or on Collage. Both of those extra assignments have Mina in the Person column. The new person appears only in the broken count.

Results when the only added fact is that Mina joined Sketching
GraphSame sessionSeparate sessions
Starting facts2 matches, 1 person4 matches, 1 person
With Mina’s participation2 matches, 1 person6 matches, 2 people

An explanation of the test should name both the added fact and the borrowed condition. Mina joined Sketching, which is at WestHall. The separate query lets another session supply EastHall. The faulty result would be harder to explain if you only reported that one count increased.

The best of these two tests introduces a person who should fail the particular condition that was disconnected. You get a meaningful comparison because both queries see the same new fact and run with the same other data. The graph itself lets another reader verify why the answer changes.

Changing an existing participant can also leave some errors hidden. If Ava joins Sketching, the separate query finds extra assignments for Ava, whose name was already counted. Selecting a new participant puts the faulty route in a better position to change the answer about distinct people.

Take the three decisions into the library

The library challenge gives you a new graph and asks you to make each decision yourself. This time the question talks about books and their authors. Start by reading the question for its counting unit, then look at the two types of fact in the graph. The activity names and room checks from the rehearsal can stay behind.

For the first decision, say in plain English which object must be shared by two lines. Point to that object on the graph. When you type the variable, check both occurrences. A different name would make a separate choice available, which may let the query use a fact about another object.

For the second decision, list the person values from the matches, even if a value occurs more than once. Ask what one match means before deciding how to summarise it. The report should name its unit so somebody can tell whether the number is about matches or people.

Use these questions while you check the library query
Ask yourselfReturn to the method
What object must two lines refer to?Match two conditions in Unit 3
Which values should the answer count?Choose a count in Unit 4
What single new fact could expose a wrong match?Compare the graphs in Unit 5

For the third decision, describe what the repaired query should do with an added fact before you run the test. Then see whether the other query has a route to include something that the repaired query excludes. Look for a difference in the actual question’s count, rather than in a column the question never asks to see.

Write a short explanation after comparing the two values. Name the fact that the repaired query checks, the reason the additional value is excluded, and the separate value that allowed the other query to include it. You can use the full worked explanation for a final check of your own reasoning.

When an explanation feels hard to write, try replacing each variable in the relevant match with its value from the graph. Read the corresponding facts back in English. This can reveal whether the two statements are about the same object and give you the words you need for the explanation.

At the end, try retelling the test without the example names. The process should still make sense: find the shared object, choose the counting unit, and identify a fact that exposes a false positive. You can check that description against a fresh graph about families and adoption after finishing the library.

Open the library challenge · Choose another way to start

How many people borrowed a book by Maya?

Work through the three decisions in order. If one feels unfamiliar, pick the matching worked step and return to the challenge.

Lee borrowed BookA and BookB. Both were written by Maya. The graph also includes BookC, written by Sam.

First point to one pair of edges that lets the graph count Lee. Then see if you can find a second pair. Write down the object on which each pair connects. This gives you a concrete choice of variable when you fill the blank.

Recall: why reuse a variable?

The same variable name makes the two lines refer to the same thing.

1. Check the same book

The second line must check the author of the book from the first line. Fill the blank.

?reader ex:borrowed ?book .
ex:writtenBy ex:Maya .

2. Count people

Lee borrowed two books by Maya. How should the answer count Lee?

Which expression counts different readers?

3. Reveal the broken link

Compare the repaired query with one that matches the author using a separate name, ?otherBook. Try adding one fact at a time to the starting graph.

For each proposed addition, ask whether the borrower is already counted and whether the new borrowing itself meets the author condition. Try the additions independently. The displayed comparison updates with the selected addition, so you can check each prediction on an equal starting footing.

Which fact makes the two queries count different numbers of people?

Show worked answer

The repaired query checks the author of the same book each person borrowed. The disconnected query can pair Noor’s borrowing of BookC with a separate book written by Maya. It then counts Noor even though Sam wrote BookC.

  • Did you identify which two lines need the same book?
  • Did you count each person once?
  • Did you point to the added fact and explain why the repaired query excludes Noor?
See the full repaired query
PREFIX ex: <https://example.org/>
SELECT (COUNT(DISTINCT ?reader) AS ?readers) WHERE {
  ?reader ex:borrowed ?book .
  ?book ex:writtenBy ex:Maya .
}

Check the same reasoning on a different graph

An adoption centre asks, “How many different families adopted an animal certified by VetA?” Use the starting facts below. VetB certified Cat3. There is no adoption fact for Cat3 in the starting graph. Say what object has to connect the certification and adoption records, and what the question asks you to count.

  • VetAcertifiedCat1
  • VetAcertifiedCat2
  • VetBcertifiedCat3
  • Cat1was adopted byFamilyA
  • Cat2was adopted byFamilyA
VetA’s two certified animals both have an adoption record naming FamilyA. Read the certifier on the Cat3 arrow.
  1. Find the name that should be shared by the two lines. It stands for an animal.
  2. Count matching adoptions and different families on the starting graph.
  3. Add only “Cat3 was adopted by FamilyB”. Predict the two family counts if the second line uses the same animal or a separate animal.

Use a sentence to justify your last answer. Name a fact from each line that the separate query could use together. Then compare that pair with the question about VetA’s own certified animals.

Get a hint

Begin at VetA and keep the animal value that you found. In the second query, that first animal does not have to supply the adoption fact. Think about which families then show up after one new adoption.

Check the adoption reasoning
Different families after each query is evaluated
GraphShared animalSeparate animals
Starting facts2 matches, 1 family4 matches, 1 family
Cat3 adopted by FamilyB2 matches, 1 family6 matches, 2 families

Reuse ?pet between ex:VetA ex:certified ?pet . and ?pet ex:adoptedBy ?family .. The adoption line must describe one of the animals that VetA certified. The counting expression is COUNT(DISTINCT ?family). There are two supported matches on the starting data and both name FamilyA.

With a separate animal on the adoption line, either certified animal can be paired with either starting adoption fact. That makes four matches. They still all name FamilyA. After the addition, either certified animal can also be paired with Cat3’s adoption. Those two extra matches introduce FamilyB once to the count.

The shared query stays at one family because VetA has no certification edge to Cat3. The broken query can use a VetA certification of Cat1 or Cat2 and an adoption fact about Cat3. This is the evidence behind its extra family.

Check your reasoning

Use your own words to explain what the query shares, who it counts, and why the added fact exposes a mistake.