The power of SQL lies in relationships between tables: INNER JOIN
Here, you'll perform your first INNER JOIN
! You'll be working with your favourite SQLite database, Chinook.sqlite
. For each record in the Album
table, you'll extract the Title
along with the Name
of the Artist
. The latter will come from the Artist
table and so you will need to INNER JOIN
these two tables on the ArtistID
column of both.
Recall that to INNER JOIN
the Orders
and Customers
tables from the Northwind database, Hugo executed the following SQL query:
"SELECT OrderID, CompanyName FROM Orders INNER JOIN Customers on Orders.CustomerID = Customers.CustomerID"
The following code has already been executed to import the necessary packages and to create the engine:
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('sqlite:///Chinook.sqlite')
This exercise is part of the course
Introduction to Importing Data in Python
Exercise instructions
- Assign to
rs
the results from the following query: select all the records, extracting theTitle
of the record andName
of the artist of each record from theAlbum
table and theArtist
table, respectively. To do so,INNER JOIN
these two tables on theArtistID
column of both. - In a call to
pd.DataFrame()
, apply the methodfetchall()
tors
in order to fetch all records inrs
. Store them in the DataFramedf
. - Set the DataFrame's column names to the corresponding names of the table columns.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Open engine in context manager
# Perform query and save results to DataFrame: df
with engine.connect() as con:
____
____
____
# Print head of DataFrame df
print(df.head())