Formatting SQL Queries

SQLyog has an ability to format the SQL displayed in the active QUERY tab. You may format a single query only or the complete content of a QUERY tab. Formatting will help you to understand the structure of a complex query. If you have been working on a query (editing and copying) it will not always be well-formed. That you can corrrect with a single click. This is also useful if you want to save a query (as file or 'favorite') or if you want to use the query in an application.

First you should go to  Tools -> Preferences -> SQL Formatter and define how you prefer your queries formatted. Once that is defined you will find the formatting option:

  • In the edit menu
  • Editor context menu
  • You can also format queries using keyboard shortcuts in following three ways:

-- Format Current Query: To format a particular query in a batch, just place the cursor on the query to be formatted (i.e. before the semicolon separating the query from the next query). Select Edit -> SQL Formatter -> Format Current Query (or press F12). Only the current query would be formatted.

-- Format Selected Query: To format a selected query(s) in set of query(s), select the query(s) to be formatted. Select Edit -> SQL Formatter -> Format Selected Query (or press Ctrl+F12).

-- Format All Queries: To format the whole batch of queries entered in the SQL window. Select Format -> SQL Formatter -> Format All Queries (or press Shift+F12).

Just select 'Format' or 'Format All' and the query will be formatted as you have defined it. For instance this query,

could be formatted like this,

Note: This feature is available in SQLyog Enterprise and Ultimate.