Displaying the SQL query for a view when previewing in Drupal 7 Views

When working in the Views module, you may want to preview the SQL that is generated by the view. This is for the code savvy or the ones looking to be and would like to see the SQL generated for either debugging or further development of their views. Sometimes seeing the code can help understand how the view works in case it needs further tweaking. Showing the SQL query for a View’s preview is done with the following instructions.

Showing the SQL query for a view preview in Drupal 7 Views

  1. Log into your Drupal 7 admin dashboard.
  2. From the top menu bar, locate the Structure option and click it.
  3. Click on the Views option to move to the next screen.
  4. You are now on the Views list screen. Look to the upper right and you will see two tabs. Click on the one that is labeled Settings.
  5. Once you get to the Settings page scroll down to the bottom of the screen to the Live Preview Settings and find the checkbox labeled Show the SQL query. Click the checkbox to enable the feature.
  6. Once finished, click on the Save Configuration button at the bottom. Preview one of your views and you will now see the SQL code.

0 thoughts on “Displaying the SQL query for a view when previewing in Drupal 7 Views

  1. The query generated is not working. For example, copy and paste the sql string into PHPMyAdmin SQL input and run it, it will generate syntax errors related to, { for example, and sometimes it makes unavailable tables such as the SQL:


    SELECT node.created AS node_created, node.nid AS nid FROM {node} node LEFT JOIN {users} users_node ON node.uid = users_node.uid WHERE (( (node.status = '1') AND (node.type IN ('rai')) AND (node.uid IN ('1000')) )) ORDER BY node_created DESC LIMIT 10 OFFSET 0

    In the above example, users_node table is not found. However, the above SQL query is generated from properly working view!

    1. Hello Said Bakr,

      Sorry for the problem with the generated query. Unfortunately, the solution may involve making changes with Drupal which is a third party product. This post in the Drupal Support forums might provide some insight on the issue. Also, it is probably the best location to get free help with the problem that you’re facing. I did hunt around trying to find anything else related, but it doesn’t appear to have a precedent. So, you may need to post your question with the Drupal community for further assistance. Apologies that I can’t provide a direct answer here.

      If you have any further questions or comments, please let us know.

      Arnel C.

    1. This shows the full MySQL query that is being executed to generate the information displayed. Could you clarify what additional information you are looking for?

Was this article helpful? Join the conversation!