Querying relational databases in Python

Introduction to Importing Data in Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Basic SQL query

SELECT * FROM Table_Name
  • Returns all columns of all rows of the table
  • Example:
SELECT * FROM Orders
  • We’ll use SQLAlchemy and pandas
Introduction to Importing Data in Python

Workflow of SQL querying

  • Import packages and functions
  • Create the database engine
  • Connect to the engine
  • Query the database
  • Save query results to a DataFrame
  • Close the connection
Introduction to Importing Data in Python

Your first SQL query

from sqlalchemy import create_engine
import pandas as pd

engine = create_engine('sqlite:///Northwind.sqlite')
con = engine.connect()
rs = con.execute("SELECT * FROM Orders")
df = pd.DataFrame(rs.fetchall())
con.close()
Introduction to Importing Data in Python

Printing your query results

print(df.head())
       0      1   2                      3                      4 
0  10248  VINET   5   7/4/1996 12:00:00 AM   8/1/1996 12:00:00 AM   
1  10251  VICTE   3   7/8/1996 12:00:00 AM   8/5/1996 12:00:00 AM   
2  10254  CHOPS   5  7/11/1996 12:00:00 AM   8/8/1996 12:00:00 AM   
3  10256  WELLI   3  7/15/1996 12:00:00 AM  8/12/1996 12:00:00 AM   
4  10258  ERNSH   1  7/17/1996 12:00:00 AM  8/14/1996 12:00:00 AM
Introduction to Importing Data in Python

Set the DataFrame column names

from sqlalchemy import create_engine
import pandas as pd
engine = create_engine('sqlite:///Northwind.sqlite')
con = engine.connect()
rs = con.execute("SELECT * FROM Orders")
df = pd.DataFrame(rs.fetchall())
df.columns = rs.keys()
con.close()
Introduction to Importing Data in Python

Set the data frame column names

print(df.head())
   OrderID CustomerID  EmployeeID              OrderDate  
0    10248      VINET           5   7/4/1996 12:00:00 AM   
1    10251      VICTE           3   7/8/1996 12:00:00 AM   
2    10254      CHOPS           5  7/11/1996 12:00:00 AM   
3    10256      WELLI           3  7/15/1996 12:00:00 AM   
4    10258      ERNSH           1  7/17/1996 12:00:00 AM
Introduction to Importing Data in Python

Using the context manager

from sqlalchemy import create_engine
import pandas as pd
engine = create_engine('sqlite:///Northwind.sqlite')
with engine.connect() as con:
    rs = con.execute("SELECT OrderID, OrderDate, ShipName FROM Orders")
    df = pd.DataFrame(rs.fetchmany(size=5))
    df.columns = rs.keys()
Introduction to Importing Data in Python

Let's practice!

Introduction to Importing Data in Python

Preparing Video For Download...