Translate a db_select query into SQL in a Drupal Module

I don't know why I can never remember how to do this, so I'm writing a blog post about it that I can refer back to later.

For example, say you have Drupal 7 module that uses the `db_select` function to query the database like so:

$query = db_select('VW_ELEMENT_CLASSIFICATION_LEVEL3', 'ec3')
  ->fields('ec3', array('Classname', 'CommonName'))
  ->groupBy('Classname')
  ->groupBy('CommonName')
  ->orderBy('Classname')

The client keeps asking what the SQL query that's used to build X. Here's how to get the query. (And yes I'm very lucky to have a client that knows SQL)

Setup your debugger with a breakpoint after the query is built and set this as a watch:

(string)$query

This will translate the Drupal query into the universal, sharable SQL query:

SELECT ec3.[Classname] AS [Classname], ec3.[CommonName] AS [CommonName]
FROM
{VW_ELEMENT_CLASSIFICATION_LEVEL3} ec3
GROUP BY Classname, CommonName
ORDER BY Classname ASC

The screenshot below shows what this setup looks like in PhpStorm 9.