<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic SQL to Spark Dataframe in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/sql-to-spark-dataframe/m-p/83676#M3642</link>
    <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;Have a simple question. Not sure if Databricks supports this, but I'm wondering if there's a way to store the results of a sql cell into a spark dataframe? Or vice-versa, is there a way to take a sql query in python (saved as a string variable) and run the same exact query in a sql cell.&lt;/P&gt;</description>
    <pubDate>Tue, 20 Aug 2024 19:29:11 GMT</pubDate>
    <dc:creator>ChristianRRL</dc:creator>
    <dc:date>2024-08-20T19:29:11Z</dc:date>
    <item>
      <title>SQL to Spark Dataframe</title>
      <link>https://community.databricks.com/t5/get-started-discussions/sql-to-spark-dataframe/m-p/83676#M3642</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;Have a simple question. Not sure if Databricks supports this, but I'm wondering if there's a way to store the results of a sql cell into a spark dataframe? Or vice-versa, is there a way to take a sql query in python (saved as a string variable) and run the same exact query in a sql cell.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2024 19:29:11 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/sql-to-spark-dataframe/m-p/83676#M3642</guid>
      <dc:creator>ChristianRRL</dc:creator>
      <dc:date>2024-08-20T19:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: SQL to Spark Dataframe</title>
      <link>https://community.databricks.com/t5/get-started-discussions/sql-to-spark-dataframe/m-p/83853#M3650</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/96188"&gt;@ChristianRRL&lt;/a&gt;, results of sql cell are automatically made available as a python dataframe using the _sqldf variable. You can read more about it &lt;A href="https://learn.microsoft.com/en-us/azure/databricks/notebooks/notebooks-code#--explore-sql-cell-results-in-python-notebooks-using-python" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the second part not sure why you would need it when you can simply run the query like:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;spark.sql(qry)&lt;/LI-CODE&gt;&lt;P&gt;But in case you want your query in sql cell or part of it to be set outside, you can explore &lt;A href="https://learn.microsoft.com/en-us/azure/databricks/sql/user/queries/query-parameters" target="_self"&gt;query-parameters&lt;/A&gt;. And to achieve what you have mentioned, you could do either of the following.&lt;/P&gt;&lt;P&gt;Using parameter:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;%py
query_text1 = "your query"
spark.sql(f"set param.query={query_text1}")&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;%sql
${param.query}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Or Using Widgets:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;%py
query_text2 = "your query"
dbutils.widgets.text("query", query_text2)&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;%sql
${query}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Aug 2024 21:26:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/sql-to-spark-dataframe/m-p/83853#M3650</guid>
      <dc:creator>menotron</dc:creator>
      <dc:date>2024-08-21T21:26:57Z</dc:date>
    </item>
  </channel>
</rss>

