1

I'm working on a python assignment and I am facing an issue. My question is the following:

"Write a query to select all rows from the sales table. Merge with the table returns by INNER JOIN on the Order ID. Put the result in a variable QUERY."


So I wrote this code:

sales_row = pd.read_sql("SELECT * FROM sales", mydb)
QUERY = pd.merge(sales_row, returns, on = "Order ID", how = "inner")

It works, but then I'm asked to perform a query on the database so I wrote the following:

pd.read_sql_query(QUERY, mydb)

And I get the following error:

ObjectNotExecutableError: Not an executable object

What can I do to fix this issue?

GooDeeJAY
  • 1,681
  • 2
  • 20
  • 27
Dylan
  • 11
  • 2

2 Answers2

0

I think the question is asking you to write a query which selects all rows and merges it. so your query would be something like

query = "Select * from sales inner join returns on order id"

If you see the read_sql_query documentation, you'll see the first argument must be a string, not a dataframe.

so after running the previous line I wrote, you can write

pd.read_sql_query(query, mydb)

where "query" is the string containing the SQL query

Shubham Periwal
  • 2,198
  • 2
  • 8
  • 26
0

There seems to be a misunderstanding. pd.read_sql and pd.read_sql_query are (roughly) the same thing. From the doc of read_sql: This function is a convenience wrapper around read_sql_table and read_sql_query (for backward compatibility).

The output of pd.read_sq is a Pandas DataFrame. You don't explicit what returns is, but I guess it also is a DataFrame. So you could simply do

sales_row = pd.read_sql("SELECT * FROM sales", mydb)
QUERY = sales_row.join(returns, on="Order ID", how="inner")

Note the use of join instead of merge.

Battleman
  • 392
  • 2
  • 12