Skip to content

Instantly share code, notes, and snippets.

@zgulde
Created September 23, 2021 01:48
Show Gist options
  • Save zgulde/eea0cf97ec0d07c740362744f2fe536b to your computer and use it in GitHub Desktop.
Save zgulde/eea0cf97ec0d07c740362744f2fe536b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Run SQL Inside A Notebook\n",
"\n",
"Here's an interesting alternative to sequel ace.\n",
"\n",
"Install the package with\n",
"\n",
"```\n",
"pip install ipython-sql\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# load extension and initial connection -- do this once per notebook\n",
"%load_ext sql\n",
"\n",
"import env\n",
"%sql mysql+pymysql://$env.user:$env.password@$env.host/numbers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For a one-line query:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * mysql+pymysql://zach:***@157.230.209.171/numbers\n",
"10 rows affected.\n"
]
},
{
"data": {
"text/html": [
"<table>\n",
" <tr>\n",
" <th>n</th>\n",
" <th>category</th>\n",
" <th>supergroup</th>\n",
" </tr>\n",
" <tr>\n",
" <td>10</td>\n",
" <td>a</td>\n",
" <td>one</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>b</td>\n",
" <td>two</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>c</td>\n",
" <td>one</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>a</td>\n",
" <td>two</td>\n",
" </tr>\n",
" <tr>\n",
" <td>5</td>\n",
" <td>b</td>\n",
" <td>one</td>\n",
" </tr>\n",
" <tr>\n",
" <td>6</td>\n",
" <td>c</td>\n",
" <td>two</td>\n",
" </tr>\n",
" <tr>\n",
" <td>7</td>\n",
" <td>a</td>\n",
" <td>one</td>\n",
" </tr>\n",
" <tr>\n",
" <td>8</td>\n",
" <td>b</td>\n",
" <td>two</td>\n",
" </tr>\n",
" <tr>\n",
" <td>9</td>\n",
" <td>c</td>\n",
" <td>one</td>\n",
" </tr>\n",
" <tr>\n",
" <td>10</td>\n",
" <td>a</td>\n",
" <td>two</td>\n",
" </tr>\n",
"</table>"
],
"text/plain": [
"[(10, 'a', 'one'),\n",
" (2, 'b', 'two'),\n",
" (3, 'c', 'one'),\n",
" (4, 'a', 'two'),\n",
" (5, 'b', 'one'),\n",
" (6, 'c', 'two'),\n",
" (7, 'a', 'one'),\n",
" (8, 'b', 'two'),\n",
" (9, 'c', 'one'),\n",
" (10, 'a', 'two')]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%sql SELECT * FROM numbers2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For a multiline query:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * mysql+pymysql://zach:***@157.230.209.171/numbers\n",
"10 rows affected.\n"
]
},
{
"data": {
"text/html": [
"<table>\n",
" <tr>\n",
" <th>category</th>\n",
" <th>supergroup</th>\n",
" <th>n</th>\n",
" </tr>\n",
" <tr>\n",
" <td>a</td>\n",
" <td>one</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <td>a</td>\n",
" <td>two</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <td>a</td>\n",
" <td>None</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <td>b</td>\n",
" <td>one</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <td>b</td>\n",
" <td>two</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <td>b</td>\n",
" <td>None</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <td>c</td>\n",
" <td>one</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <td>c</td>\n",
" <td>two</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <td>c</td>\n",
" <td>None</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>10</td>\n",
" </tr>\n",
"</table>"
],
"text/plain": [
"[('a', 'one', 2),\n",
" ('a', 'two', 2),\n",
" ('a', None, 4),\n",
" ('b', 'one', 1),\n",
" ('b', 'two', 2),\n",
" ('b', None, 3),\n",
" ('c', 'one', 2),\n",
" ('c', 'two', 1),\n",
" ('c', None, 3),\n",
" (None, None, 10)]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"SELECT\n",
" category,\n",
" supergroup,\n",
" count(*) AS n\n",
"FROM numbers2\n",
"GROUP BY category, supergroup WITH ROLLUP"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can even mix and match this with python code:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# grab the result of the last sql query\n",
"result = _\n",
"df = result.DataFrame()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>category</th>\n",
" <th>supergroup</th>\n",
" <th>n</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>a</td>\n",
" <td>one</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>a</td>\n",
" <td>two</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>a</td>\n",
" <td>None</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>b</td>\n",
" <td>one</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>b</td>\n",
" <td>two</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>b</td>\n",
" <td>None</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>c</td>\n",
" <td>one</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>c</td>\n",
" <td>two</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>c</td>\n",
" <td>None</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>None</td>\n",
" <td>None</td>\n",
" <td>10</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" category supergroup n\n",
"0 a one 2\n",
"1 a two 2\n",
"2 a None 4\n",
"3 b one 1\n",
"4 b two 2\n",
"5 b None 3\n",
"6 c one 2\n",
"7 c two 1\n",
"8 c None 3\n",
"9 None None 10"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment