<p>The <code>relations</code> table holds all of the information about relations between Craft elements (entries and categories in your case).</p>

<p>So you would use the <code>sourceId</code> and <code>targetId</code> columns of the <code>relations</code> table and join on the <code>id</code> columns of the <code>entries</code> and <code>categories</code> tables in your example.</p>

CC BY-SA 4.0
2020-02-14T04:31:17.237Z
2020-02-14T04:31:17.237Z
Brad Bell
<p>I have a custom product grid not associated with Craft Commerce. I am wondering, how can I query, in the SQL database, all entries and their related categories? </p>

CC BY-SA 4.0
2020-02-13T19:19:51.757Z
why would you write an SQL query instead of using craft techniques? (twig / element api)
Clark Nelson
CC BY-SA 4.0
2020-02-13T19:53:02.747Z
Automated reporting purposes.
Akuden 
<entries><categories><mysql><content>
How to query in MySQL all entries with related categories
# How to query in MySQL all entries with related categories

(Asked by: username_0 on 2020-02-12)

I have a custom product grid not associated with Craft Commerce. I am wondering, how can I query, in the SQL database, all entries and their related categories?

**Tags:** entries, categories, mysql, content

> **Comments:**
> **username_1**: why would you write an SQL query instead of using craft techniques? (twig / element api)  
> **username_0**: Automated reporting purposes.  

---

## 1 answers

### Answer by username_2 on 2020-02-14. Score: 1 | Accepted answer

The `relations` table holds all of the information about relations between Craft elements (entries and categories in your case).

So you would use the `sourceId` and `targetId` columns of the `relations` table and join on the `id` columns of the `entries` and `categories` tables in your example.


---

