Search code examples
sqljupyter-notebookcode-formatting

Formatting SQL Query Inside an IPython/Jupyter Notebook


I want to show some SQL queries inside a notebook. I neither need nor want them to run. I'd just like them to be well formatted. At the very least I want them to be indented properly with new lines, though keyword highlighting would be nice too. Does a solution for this exist already?


Solution

  • 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.