Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for writing SQL queries in Jupyter

Tags:

I am doing some analysis using Jupyter notebooks. I usually use pandas.read_sql() to write SQL queries in Jupyter. Recently I have written a relatively big query with multiple joins. Its about a 25 line query. What is the best practice when writing such queries in Jupyter? For example, writing a query like this is no biggie -

pd.read_sql('select cs1.CLIENT as ClientName from central cs1', db.connect_win())

It is easy to read and understand, but what about larger queries? I want them to have indention and such so they are easier to read and understand.

like image 298
Doe Avatar asked Dec 08 '18 00:12

Doe


People also ask

What is the best way to write SQL queries?

Provide Correct Formatting for the Query Some of the rules for formatting a query are given below: Put each statement in the query in a new line. Put SQL keywords in the query in uppercase. Use CamelCase capitalization in the query and avoid underscore(Write ProductName and not Product_Name).


1 Answers

I would do something like this.

sql_query = """
  SELECT first_name, last_name
  FROM actor
  WHERE actor_id IN
  (
    SELECT actor_id
    FROM film_actor
    WHERE film_id IN
    (
      SELECT film_id
      FROM film
      WHERE title = 'ALTER VICTORY'
    )
  );
"""
actor = pd.read_sql(sql_query, db.connect_win())

There are some great VS Code Extensions, like SQL Server (mssql). It will do automatic formatting for you. Sometimes I like using VS Code first, as a linting tool, and pasting the resulting query back into my Jupyter notebook.

like image 167
David Rinck Avatar answered Sep 22 '22 20:09

David Rinck