<p>You can see each SQL query that is run for a page by turning <a href="https://craftcms.com/docs/config-settings#devMode" rel="nofollow noreferrer">devMode</a> on and opening your console to read the 'Profiling Summary Report'.</p>

<p>You can see a total query count and each query beings with <code>system.db.CDbCommand.query</code>.</p>

<p>I'd advise maybe isolating the twig code on its own template to avoid having to work out which query was a result of the twig code you're after.</p>

CC BY-SA 3.0
2017-01-12T15:11:25.647Z
You can also try to filter the rows returned to the console to find the right one. In this example using "homepage" as a search term.
carlcs
CC BY-SA 3.0
2017-01-13T10:42:20.850Z
Yeah this actually what i did, i isolated the twig code but instead of looking at the 'Profiling Summary Report' i used a plugin named `Query log`.The  'Profiling Summary Report' is much easier cause no extra plugin is needed.
nickGR
CC BY-SA 3.0
2017-01-12T14:58:45.673Z
2017-01-12T14:58:45.673Z
Luke Pearce
<p>In Craft 3 you can use <code>getRawSql()</code> at the end of the element query. </p>

<pre><code>{% set entries = craft.entries.section('blog').getRawSql() %}

{{ entries }}
</code></pre>

CC BY-SA 4.0
2022-03-10T13:30:50.677Z
Brilliant - thanks so much! Very useful for debugging twig api templates.
bhu Boue vidya
CC BY-SA 4.0
2018-08-28T21:08:49.587Z
2018-08-28T21:08:49.587Z
CraftQuest
<p>Is possible to dump a query made on a twig template for a statement like this one for example:</p>

<p><code>craft.entries.section('homepage')</code></p>

<p>Can we some how see the actual SQL query ?</p>

<templating><mysql>
Dump SQL query in Twig template
# Dump SQL query in Twig template

(Asked by: username_0 on 2017-01-12)

Is possible to dump a query made on a twig template for a statement like this one for example:

`craft.entries.section('homepage')`

Can we some how see the actual SQL query ?

**Tags:** templating, mysql


---

## 2 answers

### Answer by username_1 on 2017-01-12. Score: 3 | Accepted answer

You can see each SQL query that is run for a page by turning [devMode](https://craftcms.com/docs/config-settings#devMode) on and opening your console to read the 'Profiling Summary Report'.

You can see a total query count and each query beings with `system.db.CDbCommand.query`.

I'd advise maybe isolating the twig code on its own template to avoid having to work out which query was a result of the twig code you're after.

> **Comments:**
> **username_2**: You can also try to filter the rows returned to the console to find the right one. In this example using "homepage" as a search term.  
> **username_0**: Yeah this actually what i did, i isolated the twig code but instead of looking at the 'Profiling Summary Report' i used a plugin named `Query log`.The  'Profiling Summary Report' is much easier cause no extra plugin is needed.  

---

### Answer by username_3 on 2018-08-28. Score: 6

In Craft 3 you can use getRawSql() at the end of the element query. 
{% set entries = craft.entries.section('blog').getRawSql() %}

{{ entries }}

> **Comments:**
> **username_4**: Brilliant - thanks so much! Very useful for debugging twig api templates.  

---

