Formatting SQL Query Inside an IPython/Jupyter Notebook

If you set the cell as Markdown one you can write the sql query as code specifying the language (e.g. mysql)

``` mysql
SELECT *
FROM table_a AS a
LIMIT 10; 
```

This produces:

sql in notebook

It highlights the keywords. Unfortunately, it doesn't seem to deal with indentation which seems to be the main issue you are trying to deal with but maybe this helps.


If you - like me - find yourself here because you want to highlight (and run) the %%sql magic, you're best of with the technique of this answer. Posting it here cause it took me quite some time before I found the correct keywords to my answer :)

require(['notebook/js/codecell'], function(codecell) {
  codecell.CodeCell.options_default.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ;
  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){
  Jupyter.notebook.get_cells().map(function(cell){
      if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
  });
});

I found that this fixed the issue I was having.

``` sql

Produced styled code in edit mode but not when the cell was run.

``` mysql

Produced correct styling