# Changing a query to return graph name as well

**URL:** <https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289>\
**Category:** Support\
**Created:** [March 9, 2023, 2:51pm UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289 "2023-03-09T14:51:01Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cesar](https://avatars.discourse-cdn.com/v4/letter/c/e47774/32.png) [@Cesar](https://community.stardog.com/u/Cesar)\
**Post date:** [March 9, 2023, 2:51pm UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/1 "2023-03-09T14:51:01Z")

</div>

Hi,

## Bachground

We've been interacting with stardog and executing our SELECT queries using a code snippet like below.

```try

      String sparql = "SELECT * WHERE {?s ?p ?o}"         
      Dataset dataset = Datasets.builder().defaultGraphs(listResources).build();
      SelectQuery query = connection.select(sparql).dataset(dataset);
      //query.parameter(arg.getKey(), arg.getValue());
          ArrayList<Map<String, Object>> list = new ArrayList<>();
          result = query.execute();
         while (result.hasNext()) {
           list.add(mapper.mapRow(result.next()));
         }     
 // The rest 

```

In this code `dataset` is a list of graphs that we want to execute our query against them.

## Problem

Now, we've decided to get name of the graph along with the result of our queries. For instance, for the above dummy sparql query, we're interested to get the graph name of each triple.

## Questions

- What do you suggest to do so?  
One way might be doing some text processing over all our existing queries using regex and modify them. For instance, the above dummy query is gonna be `sparql = "SELECT * WHERE { GRAPH ?g {?s ?p ?o}}"` and then execute it. Then, we're not sure whether we need to manually add `FROM NAMED` to each query or we can still use `dataset`.
- Do you think it is a good approach to do this?
- Do you have any smarter suggestion to do it (e.g., using JAVA SNARL package's potential methods)?

It would be great if you could share your thoughts and suggestions with us. Thank you.

---

<div class="post-metadata">

**Author:** ![pavel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pavel/32/18_2.png) [@pavel](https://community.stardog.com/u/pavel)\
**Post date:** [March 9, 2023, 3:07pm UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/2 "2023-03-09T15:07:34Z")

</div>

Hi Cesar,

If you want the graph IRI for each triple, then your query pattern has to be `graph ?g { ?s ?p ?o }`, as you thought. No need to add `FROM NAMED` to the query string if you change your dataset to use `namedGraphs` instead of `defaultGraphs`.

You can read more on the relationship between FROM/NAMED and GRAPH in our blog post: [FROM vs FROM NAMED in SPARQL](https://www.stardog.com/labs/blog/from-vs-from-named-in-sparql/)

Cheers,  
Pavel

---

<div class="post-metadata">

**Author:** ![Cesar](https://avatars.discourse-cdn.com/v4/letter/c/e47774/32.png) [@Cesar](https://community.stardog.com/u/Cesar)\
**Post date:** [March 10, 2023, 2:14am UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/3 "2023-03-10T02:14:08Z")

</div>

Hi @pavel

Thank you for your reply. We've implemented the suggested approach to modify our queries, but we're now encountering kind of BINDing problem for majority of our queries. For instance, we had query like this

```auto
SELECT DISTINCT ?Acc ?Held_By ?Organisation ?Cap
WHERE {    
   BIND(?this AS ?that)
    ?Cap (rdfs:subClassOf)* ?that
    OPTIONAL
      { ?Cap C:deleToOrg ?Organisation
        OPTIONAL
          { ?Pos V:manages ?Organisation .
            ?Pos V:occupyingRole ?Acc .
            ?Pos V:heldBy ?Held_By
          }
      }
  }

```

we modified it to:

```auto
SELECT DISTINCT ?graph ?Acc ?Held_By ?Organisation ?Cap

WHERE { GRAPH ?graph {
      BIND(?this AS ?that)
    ?Cap (rdfs:subClassOf)* ?that
    OPTIONAL
      { ?Cap C:deleToOrg ?Organisation
        OPTIONAL
          { ?Pos V:manages ?Organisation .
            ?Pos V:occupyingRole ?Acc .
            ?Pos V:heldBy ?Held_By
          }
      }
  }
}

```

When we use Stardog studio and a manual BIND like `BIND(<example.com/input1> AS ?this)` to it and run it, it works properly and return the expected number of results. But when we use SNARL to bind (by using query.parameter) it and execute it through our JAVA code, it is executed but BINDing is not applied. Even query explain shows that the BIND is applied but seems that it does not affect

```auto

# reused
From named <XXXX1>
From named <XXXX2>
From named <XXXX3>
From named <XXXX4>
Distinct [#271K]
`─ Projection(?graph, ?Acc, ?Held_By, ?Organisation, ?Cap) [#271K]
   `─ MergeJoinOuter(?Capability) [#271K]
      +─ Bind(<example.com/input1> AS ?this) sortedBy=?Cap [#271K]
      │ `─ PropertyPath(?Cap -> ?that in ?graph, minLength=0, sortedBy=?Cap) [#271K]
      │ `─ Scan[PSOC](?Cap, rdfs:subClassOf, ?that){?graph} [#11K]
      `─ HashJoinOuter(?Organisation) [#133]
         +─ Scan[PSOC](?Cap, <http://x.com/core/C#deleToOrg>, ?Organisation){?graph} [#133]
         `─ NaryJoin(?Pos) [#23]
            +─ Scan[PSOC](?Pos, <http://x.com/core/V#heldBy>, ?Held_By){?graph} [#55]
            +─ Scan[PSOC](?Pos, <http://x.com/core/V#manages>, ?Organisation){?graph} [#29]
            `─ Scan[PSOC](?Pos, <http://x.com/core/V#occupyingRole>, ?Acc){?graph} [#63]

```

On the other hand, Stardog studio Plan (and Profiler) is as follows

```auto
From named <XXXX1>
From named <XXXX2>
From named <XXXX3>
From named <XXXX4>
Slice(offset=0, limit=1000) [#2], results: 11, wall time: 0 ms (0.0%)
`─ Distinct [#2], memory: {total=256K (30.8%); max=224K}, results: 11, wall time: 2 ms (4.7%)
   `─ Projection(?graph, ?Acc, ?Held_By, ?Organisation, ?Cap) [#2], results: 11, wall time: 0 ms (0.0%)
      `─ MergeJoinOuter(?Cap) [#2], results: 11, wall time: 2 ms (4.7%)
         +─ Sort(?Cap) [#2], memory: {total=256K (30.8%)}, results: 11, wall time: 2 ms (4.7%)
         │ `─ MergeJoin(?that) [#2], results: 11, wall time: 1 ms (2.3%)
         │ +─ Bind(example.com/input1 AS ?this) (example.com/input1 ?that) sortedBy=?that [#1], results: 1, wall time: 0 ms (0.0%)
         │ `─ PropertyPath(?Cap -> ?that in ?graph, minLength=0, sortedBy=?that, reversed) [#11], results: 11 (with gaps), wall time: 6 ms (14.0%)
         │ +─ Bind(example.com/input1 AS ?that) sortedBy=?that [#1], results: 1 (with gaps), wall time: 0 ms (0.0%)
         │ `─ Scan[POSC](?Cap, http://www.w3.org/2000/01/rdf-schema#subClassOf, ?that){?graph} [#11K]
         `─ HashJoinOuter(?Organisation) [#133], memory: {total=320K (38.5%); max=288K}, results: 7 (with gaps), wall time: 1 ms (2.3%)
            +─ Scan[PSOC](?Cap,http://x.com/core/C#deleToOrg, ?Organisation){?graph} [#133], results: 8 (with gaps), wall time: 1 ms (2.3%)
            `─ NaryJoin(?Pos) [#23], results: 21, wall time: 7 ms (16.3%)
               +─ Scan[PSOC](?Pos, http://x.com/core/V#heldBy, ?Held_By){?graph} [#55], results: 36 (with gaps), wall time: 1 ms (2.3%)
               +─ Scan[PSOC](?Pos, http://x.com/core/V#manages, ?Organisation){?graph} [#29], results: 31 (with gaps), wall time: 0 ms (0.0%)
               `─ Scan[PSOC](?Pos, http://x.com/core/V#occupyingRole, ?Acc){?graph} [#63], results: 39 (with gaps), wall time: 1 ms (2.3%)

```

Plans are different, I'm not sure what's going on here. Maybe we're not using SNARL properly or something else. It would be appreciated if you can share your thoughts with us on this. It may worth nothing that before using named graphs, the same with the same binding approach from java worked fine. Thank you

---

<div class="post-metadata">

**Author:** ![pavel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pavel/32/18_2.png) [@pavel](https://community.stardog.com/u/pavel)\
**Post date:** [March 17, 2023, 1:32pm UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/4 "2023-03-17T13:32:15Z")

</div>

Why can't you just use this basic query (without an additional `BIND`):

```auto
SELECT DISTINCT ?graph ?Acc ?Held_By ?Organisation ?Cap

WHERE { GRAPH ?graph {
    ?Cap (rdfs:subClassOf)* ?that
    OPTIONAL
      { ?Cap C:deleToOrg ?Organisation
        OPTIONAL
          { ?Pos V:manages ?Organisation .
            ?Pos V:occupyingRole ?Acc .
            ?Pos V:heldBy ?Held_By
          }
      }
  }
}

```

and then do `.parameter("that", Values.iri("example.com/input1"))`? That should work.

Cheers,  
Pavel

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [April 15, 2023, 4:44pm UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/5 "2023-04-15T16:44:46Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [May 16, 2023, 3:58am UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/6 "2023-05-16T03:58:44Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [June 17, 2023, 5:06pm UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/7 "2023-06-17T17:06:04Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [July 20, 2023, 8:49pm UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/8 "2023-07-20T20:49:35Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [August 25, 2023, 10:41am UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/9 "2023-08-25T10:41:43Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [October 6, 2023, 6:41pm UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/10 "2023-10-06T18:41:55Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [November 22, 2023, 12:34pm UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/11 "2023-11-22T12:34:31Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [January 11, 2024, 2:16pm UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/12 "2024-01-11T14:16:14Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [March 6, 2024, 11:00am UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/13 "2024-03-06T11:00:21Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [May 5, 2024, 1:12am UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/14 "2024-05-05T01:12:32Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [October 2, 2024, 10:53pm UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/15 "2024-10-02T22:53:01Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [April 19, 2025, 9:26pm UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/16 "2025-04-19T21:26:49Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [December 3, 2025, 1:12am UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/17 "2025-12-03T01:12:27Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [August 5, 2026, 7:14am UTC](https://community.stardog.com/t/changing-a-query-to-return-graph-name-as-well/4289/18 "2026-08-05T07:14:08Z")

</div>


