How can I get list of all confluence spaces, assigned users and groups using MS SQL query?
Asked by Nirmalkumar Shete:

How can I get list of all confluence spaces, their assigned users and groups using MS SQL query?


Answers:
author: Diego
body: Hello [@Nirmalkumar Shete](/t5/user/viewprofilepage/user-id/201869) .

As I understand, you need to create a SQL Query to list some specific data about your Confluence Spaces.

I would recommend you to ++*start by looking into how to connect and run SQL queries on Microsoft SQL Server.*++ Here we have their page on how to do so:
> * ++**[\[Microsoft\] Connect to and query a SQL Server instance by using SQL Server Management Studio (SSMS)](https://docs.microsoft.com/en-us/sql/ssms/tutorials/connect-query-sql-server?view=sql-server-ver15)**++

After that, we need to ++*look into the Database Schema*++. You can take a look at Atlassian own Confluence Schema here:
> * ++**[\[Confluence Server\] Confluence Data Model](https://confluence.atlassian.com/doc/confluence-data-model-127369837.html)**++

Keep in mind that ++*your database tool most likely has a feature that allows you to create your own visualization.*++

After that, you can get a taste on ++*how to work with your database with some already well-established Knowledge bases*++ for Confluence Server and how to extract data directly from your database. One such example is this:
> * ++**[\[Confluence Server\] How do I view a list of all space administrators for all spaces](https://confluence.atlassian.com/confkb/how-do-i-view-a-list-of-all-space-administrators-for-all-spaces-346325879.html)**++

**++*Fetching and analyzing data from your database requires some prior knowledge of the database itself and also the tool and methods being used.*++**

A few notes for anyone who needs or wants to dive deeper into SQL territory:
> 1. *Always be extremely careful with any query you want to send to your database*
> 2. *If you are learning, do it in a testing environment. Never try new things on your production*
> 3. *Understand the database relationship*
> 4. *Check for similar queries*
> 5. *If you plan on changing something on your database, create a backup before*
> 6. *Updating the database requires Confluence to be down. Touching the database while Confluence runs will most likely break your instance*

Some threads here in the community prove to be useful while learning:
> * ++**[\[Atlassian Community\] Need a SQL statement to find a list of all spaces not updated in X amount of time and the corresponding space administrators](https://community.atlassian.com/t5/Confluence-questions/Need-a-SQL-statement-to-find-a-list-of-all-spaces-not-updated-in/qaq-p/23218)**++
> * ++**[\[Atlassian Community\] Confluence Query](https://community.atlassian.com/t5/Confluence-questions/Confluence-Query/qaq-p/980098)**++
> * [++**\[Atlassian Community\] SQL query to get all Confluence spaces with anonymous enabled**++](https://community.atlassian.com/t5/Confluence-questions/SQL-query-to-get-all-Confluence-spaces-with-anonymous-enabled/qaq-p/25944)

I hope this guides you somewhere. Looking forward to your reply!

comments: author: Nirmalkumar Shete
body: I was looking for query to be executed, thanks for pointers though.


author: Rohan Patil
body: This query gives list of all spaces and associated users in every space

```
SELECT distinct s.SPACENAME, cu.lower_email_address
FROM SPACES AS s
JOIN SPACEPERMISSIONS AS sp ON s.spaceid = sp.spaceid
LEFT JOIN user_mapping AS u ON sp.permusername = u.user_key
LEFT JOIN cwd_user AS c ON c.lower_user_name = u.lower_username
LEFT JOIN cwd_group AS cg ON sp.permgroupname = cg.group_name
LEFT JOIN cwd_membership AS cm ON cg.id = cm.parent_id
LEFT JOIN cwd_user AS cu ON cu.id = cm.child_user_id
WHERE cu.lower_email_address is not null 
ORDER BY s.SPACENAME, cu.lower_email_address;
```


author: Judah
body: I started this query in 2020 and it's still running


author: Sam Huawey
body: [@Judah](/t5/user/viewprofilepage/user-id/1928681) I think it is time to press Ctrl+C.


Tags: confluence-server

server