# Instructions on Federated Queries using pystardog

**URL:** https://community.stardog.com/t/instructions-on-federated-queries-using-pystardog/4687
**Category:** Support
**Created:** [October 27, 2023, 8:06pm UTC](https://community.stardog.com/t/instructions-on-federated-queries-using-pystardog/4687 "2023-10-27T20:06:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Gloria\_ZHAO](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/gloria_zhao/32/1942_2.png) [@Gloria\_ZHAO](https://community.stardog.com/u/Gloria_ZHAO)
#### Post date: [October 27, 2023, 8:06pm UTC](https://community.stardog.com/t/instructions-on-federated-queries-using-pystardog/4687/1 "2023-10-27T20:06:14Z")

</div>

Hi,

I'm trying to query several databases from pystardog, e.g. DB\_a & DB\_b are databases in my Stardog Cloud. It's hard to find instructions how to implement it with pystardog.

I see pystardog can connect databases individually, and I was able to run SPARQL queries.

* * *

conn = stardog.Connection(DATABASE\_NAME, \*\*CONNECTION\_DETAILS)  
conn.begin()  
query\_test = """  
SELECT \*  
{  
?s ?p ?o .  
}  
"""  
csv\_results = conn.select(query\_test, content\_type='text/csv')

* * *

How can I implement the same idea as in the following blog?

> **[Querying Heterogeneous Federations with Stardog 9 Up to 30x Faster - Stardog](https://www.stardog.com/labs/blog/querying-heterogeneous-federations-with-stardog-9-up-to-30x-faster/)**
>
> In this blog post, we provide insight into our latest developments towards improving Stardog query performance with heterogeneous data sources, such as virtual graphs.

Run a query that can retrieve results from both DB\_a and DB\_b? Is there an option that I have to specify in pystardog to access all the databases rather than a single database?

Thanks

---

<div class="post-metadata">

### Author: ![StevePlace](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/steveplace/32/1989_2.png) [@StevePlace](https://community.stardog.com/u/StevePlace)
#### Post date: [October 30, 2023, 6:56pm UTC](https://community.stardog.com/t/instructions-on-federated-queries-using-pystardog/4687/2 "2023-10-30T18:56:30Z")

</div>

Hi Gloria,

All you need to do is write your query differently; your use of Pystardog is irrelevant in this case. If you change your query to:

```auto
SELECT * {
      SERVICE <db://DB_a> {
         ?s ?p ?o
      }
      SERVICE <db://DB_b> {
         ?a ?b ?c
      }  
}

```

you should get the results you want. You can see more at our [documentation for federated queries](https://docs.stardog.com/query-stardog/#querying-local-databases).

Best,  
Steve

---

<div class="post-metadata">

### Author: ![Gloria\_ZHAO](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/gloria_zhao/32/1942_2.png) [@Gloria\_ZHAO](https://community.stardog.com/u/Gloria_ZHAO)
#### Post date: [November 1, 2023, 7:54pm UTC](https://community.stardog.com/t/instructions-on-federated-queries-using-pystardog/4687/5 "2023-11-01T19:54:44Z")

</div>

Thanks Steve, it works!
