Retrieve df from spark.sql : [PARSE_SYNTAX_ERROR] Syntax error at or near 'SELECT'

I'm using a databricks notebook and I'd like to retrieve a dataframe from an SQL execution in Spark. I have:

statement = f""" USER {db}; SELECT * FROM {table}
"""
df = spark.sql(statement)
display(df) 

However, unlike when I fire off the same statement in an SQL cell in the notebook, I get the following error:

[PARSE_SYNTAX_ERROR] Syntax error at or near 'SELECT': extra input 'SELECT'(line 1...

Where am I going wrong?

1 Answer

I tried to reproduce the same in my environment and got below results:

This my sample demo table Persons.

enter image description here

Create dataframe by using this code as shown in the below image.

df = sqlContext.sql("select * from Persons")
display(df)

enter image description here

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like