<?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 Re: How to access the result of a %sql cell from python in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28722#M20499</link>
    <description>&lt;P&gt;Results from an SQL cell are available as a Python DataFrame. The Python DataFrame name is _sqldf.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To save the DataFrame, run this code in a Python cell:&lt;/P&gt;&lt;P&gt;df = _sqldf&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Keep in mind that the value in _sqldf is held in memory and will be replaced with the most recent results of each SQL cell run. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/notebooks/notebooks-use.html#explore-sql-cell-results-in-python-notebooks-natively-using-python" target="test_blank"&gt;https://docs.databricks.com/notebooks/notebooks-use.html#explore-sql-cell-results-in-python-notebooks-natively-using-python&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 05 Apr 2023 13:33:31 GMT</pubDate>
    <dc:creator>dogwoodlx</dc:creator>
    <dc:date>2023-04-05T13:33:31Z</dc:date>
    <item>
      <title>How to access the result of a %sql cell from python</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28713#M20490</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I see the way to move from &lt;PRE&gt;&lt;CODE&gt;python&lt;/CODE&gt;&lt;/PRE&gt; to &lt;PRE&gt;&lt;CODE&gt;sql&lt;/CODE&gt;&lt;/PRE&gt; is to &lt;I&gt;create&lt;/I&gt; a temp view, and then access that dataframe from sql, and in a sql cell.&lt;/P&gt;
&lt;P&gt;Now the question is, how can I have a &lt;PRE&gt;&lt;CODE&gt;%sql&lt;/CODE&gt;&lt;/PRE&gt; cell with a &lt;PRE&gt;&lt;CODE&gt;select&lt;/CODE&gt;&lt;/PRE&gt; statement in it, and assign the result of that statement to a dataframe variable which I can then use in the next &lt;PRE&gt;&lt;CODE&gt;python&lt;/CODE&gt;&lt;/PRE&gt; cell? &lt;/P&gt;
&lt;P&gt;&lt;B&gt;EDIT:&lt;/B&gt; I'm aware that I can run a SQL query in python and store the result in a dataframe. But what happens in reality is that I develop the SQL code in a &lt;PRE&gt;&lt;CODE&gt;%sql&lt;/CODE&gt;&lt;/PRE&gt; cell, and once it works I have to cut and paste the query back into a python cell and put it in quotes and loose the highlighting and all. This happens very frequently when I'm doing some data analysis where most of my code involves some SQL queries.&lt;/P&gt;
&lt;P&gt;To me, it makes sense to add an option to the &lt;PRE&gt;&lt;CODE&gt;select&lt;/CODE&gt;&lt;/PRE&gt; statement such as &lt;PRE&gt;&lt;CODE&gt;into dataframe_name&lt;/CODE&gt;&lt;/PRE&gt;, which would then register that dataframe in the python space, without actually running the query.&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jul 2018 14:11:48 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28713#M20490</guid>
      <dc:creator>adrin</dc:creator>
      <dc:date>2018-07-19T14:11:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to access the result of a %sql cell from python</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28714#M20491</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;@adrin, why not just run the query in a python cell? Do something like below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;df = sqlContext.sql("select * from table limit 10")&lt;/CODE&gt;&lt;/PRE&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jul 2018 16:13:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28714#M20491</guid>
      <dc:creator>john_lynch</dc:creator>
      <dc:date>2018-07-19T16:13:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to access the result of a %sql cell from python</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28715#M20492</link>
      <description>&lt;P&gt;&lt;A href="https://users/28477/jlynch.html" target="_blank"&gt;@jlynch&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;I'm aware of this, but my question is not how I can do it in python, my question is how I can do it the other way around. Please see the edit.&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jul 2018 08:19:42 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28715#M20492</guid>
      <dc:creator>adrin</dc:creator>
      <dc:date>2018-07-20T08:19:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to access the result of a %sql cell from python</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28716#M20493</link>
      <description>&lt;P&gt;@adrin​&amp;nbsp;it may not be a parameter to select and not in one sql statement, but it is possible without having to copy and paste:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;df1 = spark.createDataFrame([(None, None), (1, None), (None, 2)], ("a", "b"))
display(df1)&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;df1.createOrReplaceTempView('table1')&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;%sql
CREATE OR REPLACE TEMPORARY VIEW table2 AS SELECT * FROM table1&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;%sql
SELECT * FROM table2&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;df2 = spark.table('table2')
display(df2)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jul 2018 12:48:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28716#M20493</guid>
      <dc:creator>dmbaker</dc:creator>
      <dc:date>2018-07-20T12:48:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to access the result of a %sql cell from python</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28717#M20494</link>
      <description>&lt;P&gt;I don't have an answer, but I have the same question. I am used to Mathematica notebooks where the result of evaluating input [1] is always available in the variable %1, etc. Is something similar available in databricks? I don't think any of the answers above really answers that question. @adrin​, have you found a better answer in the meantime? &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jun 2022 12:46:54 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28717#M20494</guid>
      <dc:creator>jlammens</dc:creator>
      <dc:date>2022-06-01T12:46:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to access the result of a %sql cell from python</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28718#M20495</link>
      <description>&lt;P&gt;Variables can't be accessed between API's this way. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;See: &lt;A href="https://stackoverflow.com/questions/67272464/switch-between-python-variables-and-sql-variables-in-databricks" alt="https://stackoverflow.com/questions/67272464/switch-between-python-variables-and-sql-variables-in-databricks" target="_blank"&gt;Switch between %python variables and %sql variables in Databricks - Stack Overflow&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my opinion, the best way is to use the recommended answer above and create/update a tempview, or just run the query in sqlContext.sql()/spark.sql(). Creating a tempview from pyspark or capturing the result of a sql query in a dataframe isn't the same as accessing a python variable in SQL. The link above has some pretty good info in it.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jun 2022 21:28:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28718#M20495</guid>
      <dc:creator>Zachary_Higgins</dc:creator>
      <dc:date>2022-06-01T21:28:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to access the result of a %sql cell from python</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28719#M20496</link>
      <description>&lt;P&gt;Very an informative post! This post has a lot of information here that can help many students. However, TFTH is also one of the most reliable and professional academic help providers. They cover complete programming subjects whether it's computer science assignments or finance assignments. Students who are unable to manage time for assignments can get &lt;A href="https://www.thanksforthehelp.com/au/" alt="https://www.thanksforthehelp.com/au/" target="_blank"&gt;&lt;B&gt;Assignment help online&lt;/B&gt;&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jun 2022 12:10:07 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28719#M20496</guid>
      <dc:creator>Sjackson3289</dc:creator>
      <dc:date>2022-06-06T12:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to access the result of a %sql cell from python</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28720#M20497</link>
      <description>&lt;P&gt;&lt;A href="https://docs.databricks.com/notebooks/notebooks-use.html#explore-sql-cell-results-in-python-notebooks-natively-using-python" target="test_blank"&gt;https://docs.databricks.com/notebooks/notebooks-use.html#explore-sql-cell-results-in-python-notebooks-natively-using-python&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 21:58:09 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28720#M20497</guid>
      <dc:creator>rjdagner</dc:creator>
      <dc:date>2022-08-03T21:58:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to access the result of a %sql cell from python</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28721#M20498</link>
      <description>&lt;P&gt;&lt;A href="https://docs.databricks.com/notebooks/notebooks-use.html#explore-sql-cell-results-in-python-notebooks-natively-using-python" target="test_blank"&gt;https://docs.databricks.com/notebooks/notebooks-use.html#explore-sql-cell-results-in-python-notebooks-natively-using-python&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 21:58:44 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28721#M20498</guid>
      <dc:creator>rjdagner</dc:creator>
      <dc:date>2022-08-03T21:58:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to access the result of a %sql cell from python</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28722#M20499</link>
      <description>&lt;P&gt;Results from an SQL cell are available as a Python DataFrame. The Python DataFrame name is _sqldf.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To save the DataFrame, run this code in a Python cell:&lt;/P&gt;&lt;P&gt;df = _sqldf&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Keep in mind that the value in _sqldf is held in memory and will be replaced with the most recent results of each SQL cell run. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/notebooks/notebooks-use.html#explore-sql-cell-results-in-python-notebooks-natively-using-python" target="test_blank"&gt;https://docs.databricks.com/notebooks/notebooks-use.html#explore-sql-cell-results-in-python-notebooks-natively-using-python&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 13:33:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-access-the-result-of-a-sql-cell-from-python/m-p/28722#M20499</guid>
      <dc:creator>dogwoodlx</dc:creator>
      <dc:date>2023-04-05T13:33:31Z</dc:date>
    </item>
  </channel>
</rss>

