SQL Magic

In [ ]:
%sql -c sql_command -f input_file -ncols nb_of_cols -nrows nb_of_rows -o output_file

Executes SQL queries in the Jupyter cell.

Parameters

Name Type Optional Description
-c / --command
str
✓
SQL Command to execute.
-f / --file
str
✓
Input file to execute.
-ncols
int
✓
Maximum number of columns to display.
-nrows
int
✓
Maximum number of rows to display.
-o / --output
str
✓
Output file. Use this option to export the result of the query to the CSV or JSON format.

Returns

vDataFrame : Result of the query.

Examples

These examples demonstrate the following:

  • Setting up the environment.
  • SQL Magic.
  • Getting the vDataFrame of a query.
  • Using variables inside a query.
  • Limiting the number of rows and columns.
  • Exporting a query to JSON or CSV
  • Executing SQL files

Setting up the environment

If you don't already have one, create a new connection:

In [11]:
import verticapy as vp

# Save a new connection
vp.new_connection({"host": "10.211.55.14", 
                   "port": "5433", 
                   "database": "testdb", 
                   "password": "XxX", 
                   "user": "dbadmin"},
                   name = "VerticaDSN")

If you already have a connection in a connection file, you can use it by running the following command:

In [12]:
# Connection using the VerticaDSN connection
vp.connect("VerticaDSN")

Load the extension:

In [13]:
%load_ext verticapy.sql

Load a sample dataset. These sample datasets are loaded into the public schema by default. You can specify a target schema with the 'name' and 'schema' parameters:

In [14]:
from verticapy.datasets import load_titanic, load_iris
titanic = load_titanic()
iris = load_iris()

SQL Magic

Use '%%sql' to run a query on the dataset:

In [15]:
%%sql
SELECT 
    survived,
    AVG(fare) AS avg_fare,
    AVG(age) AS avg_age
FROM titanic
GROUP BY 1;
Execution: 0.006s
Out[15]:
123
survived
Integer
123
avg_fare
Float
123
avg_age
Float
1023.425595019157130.6420462046205
2152.300259333333329.3936572890026
Rows: 1-2 | Columns: 3

You can also run queries with '%sql' and the '-c' option:

In [17]:
%sql -c 'SELECT DISTINCT Species FROM iris;'
Execution: 0.006s
Out[17]:
Abc
Species
Varchar(30)
1Iris-versicolor
2Iris-virginica
3Iris-setosa
Rows: 1-3 | Column: Species | Type: varchar(30)

You can use a single cell for multiple queries:

In [18]:
%%sql
DROP TABLE IF EXISTS test;
CREATE TABLE test AS SELECT 'Badr Ouali' AS name;
SELECT * FROM test;
DROP
CREATE
Execution: 0.05s
Out[18]:
Abc
name
Varchar(10)
1Badr Ouali
Rows: 1-1 | Column: name | Type: varchar(10)

To add comments to query:

In [19]:
%%sql
-- Comment Test
/* My Vertica Version */
SELECT version(); -- Looking at the current Vertica version.
Execution: 0.005s
Out[19]:
Abc
Varchar(128)
1
Rows: 1-1 | Column: version | Type: varchar(128)

Getting the vDataFrame of a query

Results are stored in a vDataFrame, which is assigned to a temporary variable called '_'. As shown in the following procedure, you can assign this to a variable to save your results.

In [20]:
%%sql
SELECT
    age,
    fare,
    pclass
FROM titanic
WHERE age IS NOT NULL AND fare IS NOT NULL;
Execution: 0.007s
Out[20]:
123
age
Numeric(6,3)
123
fare
Numeric(10,5)
123
pclass
Integer
12.0151.551
230.0151.551
325.0151.551
439.00.01
571.049.50421
647.0227.5251
724.0247.52081
836.075.24171
925.026.01
1045.035.51
1142.026.551
1241.030.51
1348.050.49581
1445.026.551
1533.05.01
1628.047.11
1717.047.11
1849.026.01
1936.078.851
2046.061.1751
2127.0136.77921
2247.025.58751
2337.083.15831
2470.071.01
2539.071.28331
2631.052.01
2750.0106.4251
2839.029.71
2936.031.67921
3030.027.751
3119.0263.01
3264.0263.01
3337.053.11
3447.038.51
3524.079.21
3671.034.65421
3738.0153.46251
3846.079.21
3945.083.4751
4040.00.01
4155.093.51
4242.042.51
4355.050.01
4442.052.01
4550.028.71251
4646.026.01
4750.026.01
4832.5211.51
4958.029.71
5041.051.86251
5129.030.01
5230.045.51
5330.026.01
5419.053.11
5546.075.24171
5654.051.86251
5728.082.17081
5865.026.551
5944.090.01
6055.030.51
6147.042.41
6237.029.71
6358.0113.2751
6464.026.01
6565.061.97921
6628.527.72081
6745.528.51
6823.093.51
6929.066.61
7018.0108.91
7147.052.01
7238.00.01
7322.0135.63331
7431.050.49581
7536.040.1251
7655.059.41
7733.026.551
7861.0262.3751
7950.055.91
8056.026.551
8156.030.69581
8224.060.01
8357.0146.52081
8462.026.551
8567.0221.77921
8663.0221.77921
8761.032.32081
8852.079.651
8949.0110.88331
9040.027.72081
9161.033.51
9247.034.02081
9364.075.251
9460.026.551
9554.077.28751
9621.077.28751
9757.0164.86671
9850.0211.51
9927.0211.51
10051.061.37921
Rows: 1-100 | Columns: 3

Assign the results to a new variable:

In [21]:
titanic_clean = _
display(titanic_clean)
123
age
Numeric(6,3)
123
fare
Numeric(10,5)
123
pclass
Integer
12.0151.551
230.0151.551
325.0151.551
439.00.01
571.049.50421
647.0227.5251
724.0247.52081
836.075.24171
925.026.01
1045.035.51
1142.026.551
1241.030.51
1348.050.49581
1445.026.551
1533.05.01
1628.047.11
1717.047.11
1849.026.01
1936.078.851
2046.061.1751
2127.0136.77921
2247.025.58751
2337.083.15831
2470.071.01
2539.071.28331
2631.052.01
2750.0106.4251
2839.029.71
2936.031.67921
3030.027.751
3119.0263.01
3264.0263.01
3337.053.11
3447.038.51
3524.079.21
3671.034.65421
3738.0153.46251
3846.079.21
3945.083.4751
4040.00.01
4155.093.51
4242.042.51
4355.050.01
4442.052.01
4550.028.71251
4646.026.01
4750.026.01
4832.5211.51
4958.029.71
5041.051.86251
5129.030.01
5230.045.51
5330.026.01
5419.053.11
5546.075.24171
5654.051.86251
5728.082.17081
5865.026.551
5944.090.01
6055.030.51
6147.042.41
6237.029.71
6358.0113.2751
6464.026.01
6565.061.97921
6628.527.72081
6745.528.51
6823.093.51
6929.066.61
7018.0108.91
7147.052.01
7238.00.01
7322.0135.63331
7431.050.49581
7536.040.1251
7655.059.41
7733.026.551
7861.0262.3751
7950.055.91
8056.026.551
8156.030.69581
8224.060.01
8357.0146.52081
8462.026.551
8567.0221.77921
8663.0221.77921
8761.032.32081
8852.079.651
8949.0110.88331
9040.027.72081
9161.033.51
9247.034.02081
9364.075.251
9460.026.551
9554.077.28751
9621.077.28751
9757.0164.86671
9850.0211.51
9927.0211.51
10051.061.37921
Rows: 1-100 | Columns: 3

Temporary results are stored in a vDataFrame, so you can call vDataFrame methods:

In [22]:
titanic_clean.corr()
Out[22]:
"age"
"fare"
"pclass"
"age"1.00.178575164117464-0.403900427391808
"fare"0.1785751641174641.0-0.568258663041552
"pclass"-0.403900427391808-0.5682586630415521.0
Rows: 1-3 | Columns: 4

Using variables inside a query

You can use variables in a SQL query with the ':' operator. This variable can be a vDataFrame, a tablesample, a pandas.DataFrame, or any standard Python type.

In [23]:
import verticapy.stats as st

class_fare = titanic_clean.groupby("pclass", 
                                   [st.avg(titanic_clean["fare"])._as("avg_fare")])
class_fare
Out[23]:
123
pclass
Integer
123
avg_fare
Float
1193.1410288321168
2221.9666833333333
3312.8090323529412
Rows: 1-3 | Columns: 2

Use the 'class_fare' variable in a SQL query:

In [17]:
%%sql
SELECT 
    x.*, 
    y.avg_fare
FROM titanic AS x LEFT JOIN (SELECT * FROM :class_fare) AS y 
ON x.pclass = y.pclass;
Execution: 0.011s
Out[17]:
123
pclass
Integer
123
survived
Integer
Abc
Varchar(164)
Abc
sex
Varchar(20)
123
age
Numeric(6,3)
123
sibsp
Integer
123
parch
Integer
Abc
ticket
Varchar(36)
123
fare
Numeric(10,5)
Abc
cabin
Varchar(30)
Abc
embarked
Varchar(20)
Abc
boat
Varchar(100)
123
body
Integer
Abc
home.dest
Varchar(100)
123
avg_fare
Float
110female2.012113781151.55C22 C26S[null][null]Montreal, PQ / Chesterville, ON93.1410288321168
210male30.012113781151.55C22 C26S[null]135Montreal, PQ / Chesterville, ON93.1410288321168
310female25.012113781151.55C22 C26S[null][null]Montreal, PQ / Chesterville, ON93.1410288321168
410male39.0001120500.0A36S[null][null]Belfast, NI93.1410288321168
510male71.000PC 1760949.5042[null]C[null]22Montevideo, Uruguay93.1410288321168
610male47.010PC 17757227.525C62 C64C[null]124New York, NY93.1410288321168
710male[null]00PC 1731825.925[null]S[null][null]New York, NY93.1410288321168
810male24.001PC 17558247.5208B58 B60C[null][null]Montreal, PQ93.1410288321168
910male36.0001305075.2417C6CA[null]Winnipeg, MN93.1410288321168
1010male25.0001390526.0[null]C[null]148San Francisco, CA93.1410288321168
1110male45.00011378435.5TS[null][null]Trenton, NJ93.1410288321168
1210male42.00011048926.55D22S[null][null]London / Winnipeg, MB93.1410288321168
1310male41.00011305430.5A21S[null][null]Pomeroy, WA93.1410288321168
1410male48.000PC 1759150.4958B10C[null]208Omaha, NE93.1410288321168
1510male[null]0011237939.6[null]C[null][null]Philadelphia, PA93.1410288321168
1610male45.00011305026.55B38S[null][null]Washington, DC93.1410288321168
1710male[null]0011379831.0[null]S[null][null][null]93.1410288321168
1810male33.0006955.0B51 B53 B55S[null][null]New York, NY93.1410288321168
1910male28.00011305947.1[null]S[null][null]Montevideo, Uruguay93.1410288321168
2010male17.00011305947.1[null]S[null][null]Montevideo, Uruguay93.1410288321168
2110male49.0001992426.0[null]S[null][null]Ascot, Berkshire / Rochester, NY93.1410288321168
2210male36.0101987778.85C46S[null]172Little Onn Hall, Staffs93.1410288321168
2310male46.010W.E.P. 573461.175E31S[null][null]Amenia, ND93.1410288321168
2410male[null]001120510.0[null]S[null][null]Liverpool, England / Belfast93.1410288321168
2510male27.01013508136.7792C89C[null][null]Los Angeles, CA93.1410288321168
2610male[null]0011046552.0A14S[null][null]Stoughton, MA93.1410288321168
2710male47.000572725.5875E58S[null][null]Victoria, BC93.1410288321168
2810male37.011PC 1775683.1583E52C[null][null]Lakewood, NJ93.1410288321168
2910male[null]0011379126.55[null]S[null][null]Roachdale, IN93.1410288321168
3010male70.011WE/P 573571.0B22S[null]269Milwaukee, WI93.1410288321168
3110male39.010PC 1759971.2833C85C[null][null]New York, NY93.1410288321168
3210male31.010F.C. 1275052.0B71S[null][null]Montreal, PQ93.1410288321168
3310male50.010PC 17761106.425C86C[null]62Deephaven, MN / Cedar Rapids, IA93.1410288321168
3410male39.000PC 1758029.7A18C[null]133Philadelphia, PA93.1410288321168
3510female36.000PC 1753131.6792A29C[null][null]New York, NY93.1410288321168
3610male[null]00PC 17483221.7792C95S[null][null][null]93.1410288321168
3710male30.00011305127.75C111C[null][null]New York, NY93.1410288321168
3810male19.03219950263.0C23 C25 C27S[null][null]Winnipeg, MB93.1410288321168
3910male64.01419950263.0C23 C25 C27S[null][null]Winnipeg, MB93.1410288321168
4010male[null]0011377826.55D34S[null][null]Westcliff-on-Sea, Essex93.1410288321168
4110male[null]001120580.0B102S[null][null][null]93.1410288321168
4210male37.01011380353.1C123S[null][null]Scituate, MA93.1410288321168
4310male47.00011132038.5E63S[null]275St Anne's-on-Sea, Lancashire93.1410288321168
4410male24.000PC 1759379.2B86C[null][null][null]93.1410288321168
4510male71.000PC 1775434.6542A5C[null][null]New York, NY93.1410288321168
4610male38.001PC 17582153.4625C91S[null]147Winnipeg, MB93.1410288321168
4710male46.000PC 1759379.2B82 B84C[null][null]New York, NY93.1410288321168
4810male[null]0011379642.4[null]S[null][null][null]93.1410288321168
4910male45.0103697383.475C83S[null][null]New York, NY93.1410288321168
5010male40.0001120590.0B94S[null]110[null]93.1410288321168
5110male55.0111274993.5B69S[null]307Montreal, PQ93.1410288321168
5210male42.00011303842.5B11S[null][null]London / Middlesex93.1410288321168
5310male[null]001746351.8625E46S[null][null]Brighton, MA93.1410288321168
5410male55.00068050.0C39S[null][null]London / Birmingham93.1410288321168
5510male42.01011378952.0[null]S[null]38New York, NY93.1410288321168
5610male[null]00PC 1760030.6958[null]C14[null]New York, NY93.1410288321168
5710female50.000PC 1759528.7125C49C[null][null]Paris, France New York, NY93.1410288321168
5810male46.00069426.0[null]S[null]80Bennington, VT93.1410288321168
5910male50.00011304426.0E60S[null][null]London93.1410288321168
6010male32.500113503211.5C132C[null]45[null]93.1410288321168
6110male58.0001177129.7B37C[null]258Buffalo, NY93.1410288321168
6210male41.0101746451.8625D21S[null][null]Southington / Noank, CT93.1410288321168
6310male[null]0011302826.55C124S[null][null]Portland, OR93.1410288321168
6410male[null]00PC 1761227.7208[null]C[null][null]Chicago, IL93.1410288321168
6510male29.00011350130.0D6S[null]126Springfield, MA93.1410288321168
6610male30.00011380145.5[null]S[null][null]London / New York, NY93.1410288321168
6710male30.00011046926.0C106S[null][null]Brockton, MA93.1410288321168
6810male19.01011377353.1D30S[null][null]New York, NY93.1410288321168
6910male46.0001305075.2417C6C[null]292Vancouver, BC93.1410288321168
7010male54.0001746351.8625E46S[null]175Dorchester, MA93.1410288321168
7110male28.010PC 1760482.1708[null]C[null][null]New York, NY93.1410288321168
7210male65.0001350926.55E38S[null]249East Bridgewater, MA93.1410288321168
7310male44.0201992890.0C78Q[null]230Fond du Lac, WI93.1410288321168
7410male55.00011378730.5C30S[null][null]Montreal, PQ93.1410288321168
7510male47.00011379642.4[null]S[null][null]Washington, DC93.1410288321168
7610male37.001PC 1759629.7C118C[null][null]Brooklyn, NY93.1410288321168
7710male58.00235273113.275D48C[null]122Lexington, MA93.1410288321168
7810male64.00069326.0[null]S[null]263Isle of Wight, England93.1410288321168
7910male65.00111350961.9792B30C[null]234Providence, RI93.1410288321168
8010male28.500PC 1756227.7208D43C[null]189?Havana, Cuba93.1410288321168
8110male[null]001120520.0[null]S[null][null]Belfast93.1410288321168
8210male45.50011304328.5C124S[null]166Surbiton Hill, Surrey93.1410288321168
8310male23.0001274993.5B24S[null][null]Montreal, PQ93.1410288321168
8410male29.01011377666.6C2S[null][null]Isleworth, England93.1410288321168
8510male18.010PC 17758108.9C65C[null][null]Madrid, Spain93.1410288321168
8610male47.00011046552.0C110S[null]207Worcester, MA93.1410288321168
8710male38.000199720.0[null]S[null][null]Rotterdam, Netherlands93.1410288321168
8810male22.000PC 17760135.6333[null]C[null]232[null]93.1410288321168
8910male[null]00PC 17757227.525[null]C[null][null][null]93.1410288321168
9010male31.000PC 1759050.4958A24S[null][null]Trenton, NJ93.1410288321168
9110male[null]0011376750.0A32S[null][null]Seattle, WA93.1410288321168
9210male36.0001304940.125A10C[null][null]Winnipeg, MB93.1410288321168
9310male55.010PC 1760359.4[null]C[null][null]New York, NY93.1410288321168
9410male33.00011379026.55[null]S[null]109London93.1410288321168
9510male61.013PC 17608262.375B57 B59 B63 B66C[null][null]Haverford, PA / Cooperstown, NY93.1410288321168
9610male50.0101350755.9E44S[null][null]Duluth, MN93.1410288321168
9710male56.00011379226.55[null]S[null][null]New York, NY93.1410288321168
9810male56.0001776430.6958A7C[null][null]St James, Long Island, NY93.1410288321168
9910male24.0101369560.0C31S[null][null]Huntington, WV93.1410288321168
10010male[null]0011305626.0A19S[null][null]Streatham, Surrey93.1410288321168
Rows: 1-100 | Columns: 15

You can do the same with tablesample:

In [28]:
tb = {"name": ["Badr", "Arash"], "specialty": ["Python", "C++"]}
tb = vp.tablesample(tb)
In [27]:
%%sql
SELECT * FROM :tb;
Execution: 0.014s
Out[27]:
Abc
name
Varchar(5)
Abc
specialty
Varchar(6)
1BadrPython
2ArashC++
Rows: 1-2 | Columns: 2

Or with a pandas.DataFrame:

In [29]:
titanic_pandas = titanic.to_pandas()
titanic_pandas
Out[29]:
pclass survived name sex age sibsp parch ticket fare cabin embarked boat body home.dest
0 1 0 Allison, Miss. Helen Loraine female 2.000 1 2 113781 151.55000 C22 C26 S None NaN Montreal, PQ / Chesterville, ON
1 1 0 Allison, Mr. Hudson Joshua Creighton male 30.000 1 2 113781 151.55000 C22 C26 S None 135.0 Montreal, PQ / Chesterville, ON
2 1 0 Allison, Mrs. Hudson J C (Bessie Waldo Daniels) female 25.000 1 2 113781 151.55000 C22 C26 S None NaN Montreal, PQ / Chesterville, ON
3 1 0 Andrews, Mr. Thomas Jr male 39.000 0 0 112050 0.00000 A36 S None NaN Belfast, NI
4 1 0 Artagaveytia, Mr. Ramon male 71.000 0 0 PC 17609 49.50420 None C None 22.0 Montevideo, Uruguay
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1229 3 1 Wilkes, Mrs. James (Ellen Needs) female 47.000 1 0 363272 7.00000 None S None NaN None
1230 3 1 Yasbeck, Mrs. Antoni (Selini Alexander) female 15.000 1 0 2659 14.45420 None C None NaN None
1231 3 1 de Messemaeker, Mr. Guillaume Joseph male 36.500 1 0 345572 17.40000 None S 15 NaN Tampico, MT
1232 3 1 de Messemaeker, Mrs. Guillaume Joseph (Emma) female 36.000 1 0 345572 17.40000 None S 13 NaN Tampico, MT
1233 3 1 de Mulder, Mr. Theodore male 30.000 0 0 345774 9.50000 None S 11 NaN Belgium Detroit, MI

1234 rows × 14 columns

In [30]:
%%sql
SELECT * FROM :titanic_pandas;
Execution: 0.008s
Out[30]:
123
pclass
Integer
123
survived
Integer
Abc
Varchar(164)
Abc
sex
Varchar(20)
123
age
Numeric(6,3)
123
sibsp
Integer
123
parch
Integer
Abc
ticket
Varchar(36)
123
fare
Numeric(10,5)
Abc
cabin
Varchar(30)
Abc
embarked
Varchar(20)
Abc
boat
Varchar(100)
123
body
Integer
Abc
home.dest
Varchar(100)
110female2.012113781151.55C22 C26S[null][null]Montreal, PQ / Chesterville, ON
210male30.012113781151.55C22 C26S[null]135Montreal, PQ / Chesterville, ON
310female25.012113781151.55C22 C26S[null][null]Montreal, PQ / Chesterville, ON
410male39.0001120500.0A36S[null][null]Belfast, NI
510male71.000PC 1760949.5042[null]C[null]22Montevideo, Uruguay
610male47.010PC 17757227.525C62 C64C[null]124New York, NY
710male[null]00PC 1731825.925[null]S[null][null]New York, NY
810male24.001PC 17558247.5208B58 B60C[null][null]Montreal, PQ
910male36.0001305075.2417C6CA[null]Winnipeg, MN
1010male25.0001390526.0[null]C[null]148San Francisco, CA
1110male45.00011378435.5TS[null][null]Trenton, NJ
1210male42.00011048926.55D22S[null][null]London / Winnipeg, MB
1310male41.00011305430.5A21S[null][null]Pomeroy, WA
1410male48.000PC 1759150.4958B10C[null]208Omaha, NE
1510male[null]0011237939.6[null]C[null][null]Philadelphia, PA
1610male45.00011305026.55B38S[null][null]Washington, DC
1710male[null]0011379831.0[null]S[null][null][null]
1810male33.0006955.0B51 B53 B55S[null][null]New York, NY
1910male28.00011305947.1[null]S[null][null]Montevideo, Uruguay
2010male17.00011305947.1[null]S[null][null]Montevideo, Uruguay
2110male49.0001992426.0[null]S[null][null]Ascot, Berkshire / Rochester, NY
2210male36.0101987778.85C46S[null]172Little Onn Hall, Staffs
2310male46.010W.E.P. 573461.175E31S[null][null]Amenia, ND
2410male[null]001120510.0[null]S[null][null]Liverpool, England / Belfast
2510male27.01013508136.7792C89C[null][null]Los Angeles, CA
2610male[null]0011046552.0A14S[null][null]Stoughton, MA
2710male47.000572725.5875E58S[null][null]Victoria, BC
2810male37.011PC 1775683.1583E52C[null][null]Lakewood, NJ
2910male[null]0011379126.55[null]S[null][null]Roachdale, IN
3010male70.011WE/P 573571.0B22S[null]269Milwaukee, WI
3110male39.010PC 1759971.2833C85C[null][null]New York, NY
3210male31.010F.C. 1275052.0B71S[null][null]Montreal, PQ
3310male50.010PC 17761106.425C86C[null]62Deephaven, MN / Cedar Rapids, IA
3410male39.000PC 1758029.7A18C[null]133Philadelphia, PA
3510female36.000PC 1753131.6792A29C[null][null]New York, NY
3610male[null]00PC 17483221.7792C95S[null][null][null]
3710male30.00011305127.75C111C[null][null]New York, NY
3810male19.03219950263.0C23 C25 C27S[null][null]Winnipeg, MB
3910male64.01419950263.0C23 C25 C27S[null][null]Winnipeg, MB
4010male[null]0011377826.55D34S[null][null]Westcliff-on-Sea, Essex
4110male[null]001120580.0B102S[null][null][null]
4210male37.01011380353.1C123S[null][null]Scituate, MA
4310male47.00011132038.5E63S[null]275St Anne's-on-Sea, Lancashire
4410male24.000PC 1759379.2B86C[null][null][null]
4510male71.000PC 1775434.6542A5C[null][null]New York, NY
4610male38.001PC 17582153.4625C91S[null]147Winnipeg, MB
4710male46.000PC 1759379.2B82 B84C[null][null]New York, NY
4810male[null]0011379642.4[null]S[null][null][null]
4910male45.0103697383.475C83S[null][null]New York, NY
5010male40.0001120590.0B94S[null]110[null]
5110male55.0111274993.5B69S[null]307Montreal, PQ
5210male42.00011303842.5B11S[null][null]London / Middlesex
5310male[null]001746351.8625E46S[null][null]Brighton, MA
5410male55.00068050.0C39S[null][null]London / Birmingham
5510male42.01011378952.0[null]S[null]38New York, NY
5610male[null]00PC 1760030.6958[null]C14[null]New York, NY
5710female50.000PC 1759528.7125C49C[null][null]Paris, France New York, NY
5810male46.00069426.0[null]S[null]80Bennington, VT
5910male50.00011304426.0E60S[null][null]London
6010male32.500113503211.5C132C[null]45[null]
6110male58.0001177129.7B37C[null]258Buffalo, NY
6210male41.0101746451.8625D21S[null][null]Southington / Noank, CT
6310male[null]0011302826.55C124S[null][null]Portland, OR
6410male[null]00PC 1761227.7208[null]C[null][null]Chicago, IL
6510male29.00011350130.0D6S[null]126Springfield, MA
6610male30.00011380145.5[null]S[null][null]London / New York, NY
6710male30.00011046926.0C106S[null][null]Brockton, MA
6810male19.01011377353.1D30S[null][null]New York, NY
6910male46.0001305075.2417C6C[null]292Vancouver, BC
7010male54.0001746351.8625E46S[null]175Dorchester, MA
7110male28.010PC 1760482.1708[null]C[null][null]New York, NY
7210male65.0001350926.55E38S[null]249East Bridgewater, MA
7310male44.0201992890.0C78Q[null]230Fond du Lac, WI
7410male55.00011378730.5C30S[null][null]Montreal, PQ
7510male47.00011379642.4[null]S[null][null]Washington, DC
7610male37.001PC 1759629.7C118C[null][null]Brooklyn, NY
7710male58.00235273113.275D48C[null]122Lexington, MA
7810male64.00069326.0[null]S[null]263Isle of Wight, England
7910male65.00111350961.9792B30C[null]234Providence, RI
8010male28.500PC 1756227.7208D43C[null]189?Havana, Cuba
8110male[null]001120520.0[null]S[null][null]Belfast
8210male45.50011304328.5C124S[null]166Surbiton Hill, Surrey
8310male23.0001274993.5B24S[null][null]Montreal, PQ
8410male29.01011377666.6C2S[null][null]Isleworth, England
8510male18.010PC 17758108.9C65C[null][null]Madrid, Spain
8610male47.00011046552.0C110S[null]207Worcester, MA
8710male38.000199720.0[null]S[null][null]Rotterdam, Netherlands
8810male22.000PC 17760135.6333[null]C[null]232[null]
8910male[null]00PC 17757227.525[null]C[null][null][null]
9010male31.000PC 1759050.4958A24S[null][null]Trenton, NJ
9110male[null]0011376750.0A32S[null][null]Seattle, WA
9210male36.0001304940.125A10C[null][null]Winnipeg, MB
9310male55.010PC 1760359.4[null]C[null][null]New York, NY
9410male33.00011379026.55[null]S[null]109London
9510male61.013PC 17608262.375B57 B59 B63 B66C[null][null]Haverford, PA / Cooperstown, NY
9610male50.0101350755.9E44S[null][null]Duluth, MN
9710male56.00011379226.55[null]S[null][null]New York, NY
9810male56.0001776430.6958A7C[null][null]St James, Long Island, NY
9910male24.0101369560.0C31S[null][null]Huntington, WV
10010male[null]0011305626.0A19S[null][null]Streatham, Surrey
Rows: 1-100 | Columns: 14

Using a sample loop with a variable:

In [24]:
%sql -c 'DROP TABLE IF EXISTS test;'
%sql -c 'CREATE TABLE test (id INT);'
for i in range(4):
    %sql -c 'INSERT INTO test(id) SELECT :i;'
DROP
Execution: 0.014s
CREATE
Execution: 0.008s
INSERT
Execution: 0.05s
INSERT
Execution: 0.015s
INSERT
Execution: 0.016s
INSERT
Execution: 0.013s
In [25]:
%%sql
SELECT * FROM test;
Execution: 0.005s
Out[25]:
123
id
Integer
10
21
32
43
Rows: 1-4 | Column: id | Type: integer

Changing the maximum number of rows/columns to display

Use the '-nrows' and '-ncols' option to limit the number of rows and columns displayed:

In [26]:
%%sql -nrows 5 -ncols 2
SELECT * FROM public.titanic;
Execution: 0.008s
Out[26]:
123
pclass
Integer
...
Abc
home.dest
Varchar(100)
11...Montreal, PQ / Chesterville, ON
21...Montreal, PQ / Chesterville, ON
31...Montreal, PQ / Chesterville, ON
41...Belfast, NI
51...Montevideo, Uruguay
Rows: 1-5 | Columns: 14

Exporting results to a JSON or CSV

To export the results of a query to a CSV file:

In [27]:
%%sql -o titanic_age_clean.csv
SELECT
    * 
FROM public.titanic
WHERE age IS NOT NULL LIMIT 5;
Execution: 0.008s
Out[27]:
123
pclass
Integer
123
survived
Integer
Abc
Varchar(164)
Abc
sex
Varchar(20)
123
age
Numeric(6,3)
123
sibsp
Integer
123
parch
Integer
Abc
ticket
Varchar(36)
123
fare
Numeric(10,5)
Abc
cabin
Varchar(30)
Abc
embarked
Varchar(20)
Abc
boat
Varchar(100)
123
body
Integer
Abc
home.dest
Varchar(100)
110female2.012113781151.55C22 C26S[null][null]Montreal, PQ / Chesterville, ON
210male30.012113781151.55C22 C26S[null]135Montreal, PQ / Chesterville, ON
310female25.012113781151.55C22 C26S[null][null]Montreal, PQ / Chesterville, ON
410male39.0001120500.0A36S[null][null]Belfast, NI
510male71.000PC 1760949.5042[null]C[null]22Montevideo, Uruguay
Rows: 5 | Columns: 14
In [28]:
file = open("titanic_age_clean.csv", "r")
print(file.read())
file.close()
pclass,survived,name,sex,age,sibsp,parch,ticket,fare,cabin,embarked,boat,body,home.dest
1,0,"Allison, Miss. Helen Loraine","female",2.000,1,2,"113781",151.55000,"C22 C26","S",,,"Montreal, PQ / Chesterville, ON"
1,0,"Allison, Mr. Hudson Joshua Creighton","male",30.000,1,2,"113781",151.55000,"C22 C26","S",,135,"Montreal, PQ / Chesterville, ON"
1,0,"Allison, Mrs. Hudson J C (Bessie Waldo Daniels)","female",25.000,1,2,"113781",151.55000,"C22 C26","S",,,"Montreal, PQ / Chesterville, ON"
1,0,"Andrews, Mr. Thomas Jr","male",39.000,0,0,"112050",0.00000,"A36","S",,,"Belfast, NI"
1,0,"Artagaveytia, Mr. Ramon","male",71.000,0,0,"PC 17609",49.50420,,"C",,22,"Montevideo, Uruguay"

To export query results to a JSON file:

In [29]:
%%sql -o titanic_age_clean.json
SELECT 
    * 
FROM public.titanic
WHERE age IS NOT NULL LIMIT 5;
Execution: 0.008s
Out[29]:
123
pclass
Integer
123
survived
Integer
Abc
Varchar(164)
Abc
sex
Varchar(20)
123
age
Numeric(6,3)
123
sibsp
Integer
123
parch
Integer
Abc
ticket
Varchar(36)
123
fare
Numeric(10,5)
Abc
cabin
Varchar(30)
Abc
embarked
Varchar(20)
Abc
boat
Varchar(100)
123
body
Integer
Abc
home.dest
Varchar(100)
110female2.012113781151.55C22 C26S[null][null]Montreal, PQ / Chesterville, ON
210male30.012113781151.55C22 C26S[null]135Montreal, PQ / Chesterville, ON
310female25.012113781151.55C22 C26S[null][null]Montreal, PQ / Chesterville, ON
410male39.0001120500.0A36S[null][null]Belfast, NI
510male71.000PC 1760949.5042[null]C[null]22Montevideo, Uruguay
Rows: 5 | Columns: 14
In [30]:
file = open("titanic_age_clean.json", "r")
print(file.read())
file.close()
[
{"pclass": 1, "survived": 0, "name": "Allison, Miss. Helen Loraine", "sex": "female", "age": 2.000, "sibsp": 1, "parch": 2, "ticket": "113781", "fare": 151.55000, "cabin": "C22 C26", "embarked": "S", "home.dest": "Montreal, PQ / Chesterville, ON"},
{"pclass": 1, "survived": 0, "name": "Allison, Mr. Hudson Joshua Creighton", "sex": "male", "age": 30.000, "sibsp": 1, "parch": 2, "ticket": "113781", "fare": 151.55000, "cabin": "C22 C26", "embarked": "S", "body": 135, "home.dest": "Montreal, PQ / Chesterville, ON"},
{"pclass": 1, "survived": 0, "name": "Allison, Mrs. Hudson J C (Bessie Waldo Daniels)", "sex": "female", "age": 25.000, "sibsp": 1, "parch": 2, "ticket": "113781", "fare": 151.55000, "cabin": "C22 C26", "embarked": "S", "home.dest": "Montreal, PQ / Chesterville, ON"},
{"pclass": 1, "survived": 0, "name": "Andrews, Mr. Thomas Jr", "sex": "male", "age": 39.000, "sibsp": 0, "parch": 0, "ticket": "112050", "fare": 0.00000, "cabin": "A36", "embarked": "S", "home.dest": "Belfast, NI"},
{"pclass": 1, "survived": 0, "name": "Artagaveytia, Mr. Ramon", "sex": "male", "age": 71.000, "sibsp": 0, "parch": 0, "ticket": "PC 17609", "fare": 49.50420, "embarked": "C", "body": 22, "home.dest": "Montevideo, Uruguay"},
]

Executing SQL files

Executing commands from a SQL file:

In [31]:
file = open("query.sql", "w+")
file.write("SELECT version();")
file.close()
In [32]:
%sql -f query.sql
Execution: 0.006s
Out[32]:
Abc
Varchar(128)
1
Rows: 1-1 | Column: version | Type: varchar(128)

Connect to External Database

Since v0.12.0, it is possible to connect to external Databases using the connection symbol. Detailled examples are available in this notebook.