Loading...

verticapy.jupyter.extensions.sql_magic.sql_magic

verticapy.jupyter.extensions.sql_magic.sql_magic(line: str, cell: str | None = None, local_ns: dict | None = None) vDataFrame

Executes SQL queries in the Jupyter cell.

Warning

In the case of profiling (using PROFILE keywords), the query will be executed twice: once for profiling and another time to build the vDataFrame.

Parameters

-c / –commandstr, optional

SQL Command to execute.

-f / –filestr, optional

Input File. You can use this option if you want to execute the input file.

-ncolsint, optional

Maximum number of columns to display.

-nrowsint, optional

Maximum number of rows to display.

-o / –outputstr, optional

Output File. You can use this option if you want to export the result of the query to the CSV or JSON format.

-stdin: bool, optional

If set to False and you’re trying to perform a local copy, the parser will not replace the file name with STDIN, simplifying the ingestion.

Returns

vDataFrame

Result of the query

Examples

The following examples demonstrate:

  • Setting up the environment

  • Using 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 a connection, create one:

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:

# Connect using the VerticaDSN connection
vp.connect("VerticaDSN")

Load the extension:

%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:

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:

%%sql
SELECT
    survived,
    AVG(fare) AS avg_fare,
    AVG(age) AS avg_age
FROM titanic
GROUP BY 1;

Execution: 0.006s

123
survived
Integer
123
avg_fare
Float(22)
123
avg_age
Float(22)
1023.425595019157130.6420462046205
2152.300259333333329.3936572890026
Rows: 1-2 | Columns: 3

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

%sql -c 'SELECT DISTINCT Species FROM iris;'

Execution: 0.006s

Abc
Species
Varchar(30)
1Iris-virginica
2Iris-versicolor
3Iris-setosa
Rows: 1-3 | Column: Species | Type: Varchar(30)

You can use a single cell for multiple queries:

Warning

Don’t forget to include a semicolon at the end of each query.

%%sql
DROP TABLE IF EXISTS test;
CREATE TABLE test AS SELECT 'Badr Ouali' AS name;
SELECT * FROM test;

Execution: 0.05s

Abc
name
Varchar(10)
1Badr Ouali
Rows: 1-1 | Column: name | Type: Varchar(10)

To add comments to a query, use one of the following comment syntaxes:

Warning

Vertica uses ‘/’ and ‘/’ for both comments and query hints. Whenever possible, use ‘–’ to avoid conflicts.

%%sql
-- Comment Test
/* My Vertica Version */
SELECT version(); -- Select my current version

Execution: 0.005s

Abc
Varchar(128)
1
Rows: 1-1 | Column: version | Type: Varchar(128)

Get the vDataFrame of a query

Results of a SQL Magic query are stored in a vDataFrame, which is assigned to a temporary variable called ‘_’. You can assign this temporary variable to a new variable to save your results.

%%sql
SELECT
    age,
    fare,
    pclass
FROM titanic
WHERE age IS NOT NULL AND fare IS NOT NULL;

Execution: 0.007s

Assign the results to a new variable:

titanic_clean = _
display(titanic_clean)
123
age
Numeric(8)
123
fare
Numeric(12)
123
pclass
Integer
171.049.50421
245.035.51
317.047.11
427.0136.77921
537.083.15831
631.052.01
750.0106.4251
836.031.67921
937.053.11
1024.079.21
1145.083.4751
1240.00.01
1342.042.51
1442.052.01
1529.030.01
1646.075.24171
1754.051.86251
1847.042.41
1958.0113.2751
2045.528.51
2129.066.61
2247.052.01
2338.00.01
2422.0135.63331
2531.050.49581
2650.055.91
2756.030.69581
2857.0146.52081
2963.0221.77921
3061.032.32081
3121.077.28751
3251.061.37921
3363.077.95831
3432.076.29171
3558.026.551
3644.027.72081
3741.0134.51
3853.027.44581
3936.0512.32921
4058.0512.32921
4111.0120.01
4276.078.851
4339.083.15831
4427.052.01
4535.0211.51
4622.059.41
4725.055.44171
4848.076.72921
4935.083.4751
5027.076.72921
5124.083.15831
5252.093.51
5344.057.97921
5415.0211.33751
5530.057.751
5631.0113.2751
5739.0108.91
5822.061.97921
5952.030.51
6043.0211.33751
6133.086.51
6245.0134.51
6340.0134.51
6448.052.01
6535.0512.32921
6660.075.251
6721.061.37921
6823.010.52
6928.026.02
7060.039.02
7144.026.02
7229.026.02
7318.073.52
7418.073.52
7554.026.02
7618.013.02
7736.013.02
7834.021.02
7921.011.52
8021.011.52
8124.013.02
8234.013.02
8330.013.02
8444.013.02
8549.065.02
8621.073.52
8721.073.52
8860.026.02
8924.031.52
9022.031.52
9135.012.352
9231.010.52
9336.012.8752
9440.016.02
9548.013.02
9623.010.52
9734.013.02
9834.021.02
9935.010.52
10041.015.04582
Rows: 1-100 | Columns: 3

Temporary results are stored in a vDataFrame, allowing you to call vDataFrame methods:

titanic_clean["age"].max()
Out[1]: 80.0

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.

import verticapy.sql.functions as vpf

class_fare = titanic_clean.groupby(
    "pclass",
    [vpf.avg(titanic_clean["fare"])._as("avg_fare")],
)
class_fare
123
pclass
Integer
123
avg_fare
Float(22)
1221.9666833333333
2312.8090323529412
3193.1410288321168
Rows: 1-3 | Columns: 2

Use the ‘class_fare’ variable in a SQL query:

%%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

123
pclass
Integer
123
survived
Integer
Abc
Varchar(164)
Abc
sex
Varchar(20)
123
age
Numeric(8)
123
sibsp
Integer
123
parch
Integer
Abc
ticket
Varchar(36)
123
fare
Numeric(12)
Abc
cabin
Varchar(30)
Abc
embarked
Varchar(20)
Abc
boat
Varchar(100)
123
body
Integer
Abc
Varchar(100)
123
avg_fare
Float(22)
110male47.010PC 17757227.525C62 C64C[null]12493.1410288321168
210male[null]00PC 1731825.925[null]S[null][null]93.1410288321168
310male24.001PC 17558247.5208B58 B60C[null][null]93.1410288321168
410male25.0001390526.0[null]C[null]14893.1410288321168
510male42.00011048926.55D22S[null][null]93.1410288321168
610male45.00011305026.55B38S[null][null]93.1410288321168
710male46.010W.E.P. 573461.175E31S[null][null]93.1410288321168
810male[null]0011379126.55[null]S[null][null]93.1410288321168
910male39.000PC 1758029.7A18C[null]13393.1410288321168
1010male64.01419950263.0C23 C25 C27S[null][null]93.1410288321168
1110male[null]0011377826.55D34S[null][null]93.1410288321168
1210male71.000PC 1775434.6542A5C[null][null]93.1410288321168
1310male[null]0011379642.4[null]S[null][null]93.1410288321168
1410male32.500113503211.5C132C[null]4593.1410288321168
1510male41.0101746451.8625D21S[null][null]93.1410288321168
1610male[null]0011302826.55C124S[null][null]93.1410288321168
1710male28.010PC 1760482.1708[null]C[null][null]93.1410288321168
1810male55.00011378730.5C30S[null][null]93.1410288321168
1910male37.001PC 1759629.7C118C[null][null]93.1410288321168
2010male64.00069326.0[null]S[null]26393.1410288321168
2110male28.500PC 1756227.7208D43C[null]18993.1410288321168
2210male[null]00PC 17757227.525[null]C[null][null]93.1410288321168
2310male56.00011379226.55[null]S[null][null]93.1410288321168
2410male24.0101369560.0C31S[null][null]93.1410288321168
2510male49.01117421110.8833C68C[null][null]93.1410288321168
2610male47.0003696734.0208D46S[null][null]93.1410288321168
2710male64.01011081375.25D37C[null][null]93.1410288321168
2811male0.9212113781151.55C22 C26S11[null]93.1410288321168
2911female53.0201176951.4792C101SD[null]93.1410288321168
3011female18.010PC 17757227.525C62 C64C4[null]93.1410288321168
3111male80.0002704230.0A23SB[null]93.1410288321168
3211male37.0111175152.5542D35S5[null]93.1410288321168
3311male26.00011136930.0C148C5[null]93.1410288321168
3411female42.000PC 17757227.525[null]C4[null]93.1410288321168
3511male25.0101196791.0792B49C7[null]93.1410288321168
3611female35.000PC 17760135.6333C99S8[null]93.1410288321168
3711female45.000PC 17608262.375[null]C4[null]93.1410288321168
3811female22.00111350555.0E33S6[null]93.1410288321168
3911female60.0001181376.2917D15C8[null]93.1410288321168
4011female14.012113760120.0B96 B98S4[null]93.1410288321168
4111female[null]001777027.7208[null]C5[null]93.1410288321168
4211male45.000PC 1759429.7A9C7[null]93.1410288321168
4311female22.000113781151.55[null]S11[null]93.1410288321168
4411female64.002PC 1775683.1583E45C14[null]93.1410288321168
4511female36.002WE/P 573571.0B22S7[null]93.1410288321168
4611female27.011PC 17558247.5208B58 B60C6[null]93.1410288321168
4711female54.0103694778.2667D20C4[null]93.1410288321168
4811male43.0101776527.7208D40C5[null]93.1410288321168
4911female22.0021356849.5B39C5[null]93.1410288321168
5011female35.01011380353.1C123SD[null]93.1410288321168
5111male53.00011378028.5C51CB[null]93.1410288321168
5211female19.00011205330.0B42S3[null]93.1410288321168
5311female58.001PC 17582153.4625C125S3[null]93.1410288321168
5411male23.001PC 1775963.3583D10 D12C7[null]93.1410288321168
5511male25.0101176555.4417E50C5[null]93.1410288321168
5611male[null]001698830.0D45S3[null]93.1410288321168
5711female35.01011378952.0[null]S8[null]93.1410288321168
5811female[null]101746451.8625D21S8[null]93.1410288321168
5911male42.0101175352.5542D19S5[null]93.1410288321168
6011female45.0101175352.5542D19S5[null]93.1410288321168
6111female39.00024160211.3375[null]S2[null]93.1410288321168
6211female16.001PC 1759239.4D28S9[null]93.1410288321168
6311female21.0001350277.9583D9S10[null]93.1410288321168
6411female18.01011377353.1D30S10[null]93.1410288321168
6511male36.000PC 1747326.2875E25S7[null]93.1410288321168
6611female37.0101992890.0C78Q14[null]93.1410288321168
6711male[null]00F.C. 1299825.7417[null]C7[null]93.1410288321168
6811female22.01011377666.6C2S8[null]93.1410288321168
6911female30.0001274993.5B73S3[null]93.1410288321168
7011female33.000PC 1761327.7208A11C11[null]93.1410288321168
7111female54.010PC 1760359.4[null]C6[null]93.1410288321168
7211female18.022PC 17608262.375B57 B59 B63 B66C4[null]93.1410288321168
7311female48.013PC 17608262.375B57 B59 B63 B66C4[null]93.1410288321168
7411male35.000PC 1747526.2875E24S5[null]93.1410288321168
7511female23.0102122882.2667B45S7[null]93.1410288321168
7611female43.0101177855.4417C116C5[null]93.1410288321168
7711female39.01111041379.65E67S8[null]93.1410288321168
7811female39.01117421110.8833C68C4[null]93.1410288321168
7911female55.000PC 17760135.6333C32C8[null]93.1410288321168
8011female31.00236928164.8667C7S8[null]93.1410288321168
8120male30.010P/PP 338124.0[null]C[null][null]21.9666833333333
8220male30.00024874413.0[null]S[null][null]21.9666833333333
8320male57.00024434613.0[null]S[null][null]21.9666833333333
8420male51.000S.O.P. 116612.525[null]S[null]17421.9666833333333
8520male[null]002398530.0[null]S[null][null]21.9666833333333
8620male52.00024873113.5[null]S[null]13021.9666833333333
8720male37.010SC/AH 2903726.0[null]S[null]1721.9666833333333
8820female29.010SC/AH 2903726.0[null]S[null][null]21.9666833333333
8920male29.000W./C. 1426310.5[null]S[null][null]21.9666833333333
9020female30.00023724913.0[null]S[null][null]21.9666833333333
9120male[null]002398530.0[null]S[null][null]21.9666833333333
9220male17.000S.O.C. 1487973.5[null]S[null][null]21.9666833333333
9320male18.000C.A. 1518510.5[null]S[null][null]21.9666833333333
9420male24.00024872613.5[null]S[null]29721.9666833333333
9520male30.00025064613.0[null]S[null]30521.9666833333333
9620male52.00025064713.0[null]S[null]1921.9666833333333
9720female18.01125065013.0[null]S[null][null]21.9666833333333
9820male23.0212910411.5[null]S[null][null]21.9666833333333
9920male36.00024296313.0[null]S[null][null]21.9666833333333
10020male44.0102670726.0[null]S[null][null]21.9666833333333
Rows: 1-100 | Columns: 15

You can do the same with a TableSample:

tb = {
    "name": ["Badr", "Arash"],
    "specialty": ["Python", "C++"],
}
tb = vp.TableSample(tb)
%%sql
SELECT * FROM :tb;

Execution: 0.014s

Abc
name
Varchar(5)
Abc
specialty
Varchar(6)
1BadrPython
2ArashC++
Rows: 1-2 | Columns: 2

And with a pandas.DataFrame:

titanic_pandas = titanic.to_pandas()

titanic_pandas
Out[3]: 
      pclass  survived                                             name     sex     age  sibsp  parch    ticket       fare cabin embarked  boat  body            home.dest
0          1         0                          Artagaveytia, Mr. Ramon    male  71.000      0      0  PC 17609   49.50420  None        C  None  22.0  Montevideo, Uruguay
1          1         0                     Blackwell, Mr. Stephen Weart    male  45.000      0      0    113784   35.50000     T        S  None   NaN          Trenton, NJ
2          1         0                            Cairns, Mr. Alexander    male    None      0      0    113798   31.00000  None        S  None   NaN                 None
3          1         0                           Carrau, Mr. Jose Pedro    male  17.000      0      0    113059   47.10000  None        S  None   NaN  Montevideo, Uruguay
4          1         0                         Clark, Mr. Walter Miller    male  27.000      1      0     13508  136.77920   C89        C  None   NaN      Los Angeles, CA
...      ...       ...                                              ...     ...     ...    ...    ...       ...        ...   ...      ...   ...   ...                  ...
1229       3         1          Touma, Mrs. Darwis (Hanne Youssef Razi)  female  29.000      0      2      2650   15.24580  None        C     C   NaN                 None
1230       3         1                           Turkula, Mrs. (Hedwig)  female  63.000      0      0      4134    9.58750  None        S    15   NaN                 None
1231       3         1  Whabee, Mrs. George Joseph (Shawneene Abi-Saab)  female  38.000      0      0      2688    7.22920  None        C     C   NaN                 None
1232       3         1             de Messemaeker, Mr. Guillaume Joseph    male  36.500      1      0    345572   17.40000  None        S    15   NaN          Tampico, MT
1233       3         1     de Messemaeker, Mrs. Guillaume Joseph (Emma)  female  36.000      1      0    345572   17.40000  None        S    13   NaN          Tampico, MT

[1234 rows x 14 columns]
%%sql
SELECT * FROM :titanic_pandas;
123
pclass
Integer
123
survived
Integer
Abc
Varchar(164)
Abc
sex
Varchar(20)
123
age
Numeric(10)
123
sibsp
Integer
123
parch
Integer
Abc
ticket
Varchar(36)
123
fare
Numeric(13)
Abc
cabin
Varchar(30)
Abc
embarked
Varchar(20)
Abc
boat
Varchar(100)
123
body
Numeric(9)
Abc
Varchar(100)
110female25.012113781151.55C22 C26S[null][null]
210male47.010PC 17757227.525C62 C64C[null]124.0
310male45.00011378435.5TS[null][null]
410male42.00011048926.55D22S[null][null]
510male17.00011305947.1[null]S[null][null]
610male31.010F.C. 1275052.0B71S[null][null]
710male39.000PC 1758029.7A18C[null]133.0
810male[null]0011379642.4[null]S[null][null]
910male42.01011378952.0[null]S[null]38.0
1010male50.00011304426.0E60S[null][null]
1110male58.0001177129.7B37C[null]258.0
1210male41.0101746451.8625D21S[null][null]
1310male29.00011350130.0D6S[null]126.0
1410male19.01011377353.1D30S[null][null]
1510male54.0001746351.8625E46S[null]175.0
1610male65.0001350926.55E38S[null]249.0
1710male55.00011378730.5C30S[null][null]
1810male64.00069326.0[null]S[null]263.0
1910male45.50011304328.5C124S[null]166.0
2010male[null]00PC 17757227.525[null]C[null][null]
2110male36.0001304940.125A10C[null][null]
2210male33.00011379026.55[null]S[null]109.0
2310male61.013PC 17608262.375B57 B59 B63 B66C[null][null]
2410male50.0101350755.9E44S[null][null]
2510male56.00011379226.55[null]S[null][null]
2610male[null]0011305626.0A19S[null][null]
2710male57.010PC 17569146.5208B78C[null][null]
2810female63.010PC 17483221.7792C55 C57S[null][null]
2910male64.01011081375.25D37C[null][null]
3010male21.0013528177.2875D26S[null]169.0
3110male27.002113503211.5C82C[null][null]
3211male0.9212113781151.55C22 C26S11[null]
3311male48.0001995226.55E12S3[null]
3411female32.0001181376.2917D15C8[null]
3511female47.0111175152.5542D35S5[null]
3611female59.0201176951.4792C101SD[null]
3711female53.000PC 1760627.4458[null]C6[null]
3811male36.012113760120.0B96 B98SC[null]
3911female33.01011380653.1E8S5[null]
4011female[null]0111350555.0E33S6[null]
4111female64.01111290126.55B26S7[null]
4211male51.00011305526.55E17S5 9[null]
4311female27.012F.C. 1275052.0B71S3[null]
4411female54.0113363881.8583A34S5[null]
4511female27.011PC 17558247.5208B58 B60C6[null]
4611female48.010PC 17761106.425C86C2[null]
4711female[null]00PC 1759831.6833[null]S7[null]
4811female28.03219950263.0C23 C25 C27S10[null]
4911female60.01419950263.0C23 C25 C27S10[null]
5011male50.020PC 17611133.65[null]S5[null]
5111female[null]10PC 17611133.65[null]S5[null]
5211female35.000113503211.5C130C4[null]
5311female22.00111237859.4[null]C7[null]
5411female45.00111237859.4[null]C7[null]
5511male25.0101176555.4417E50C5[null]
5611female16.00111136157.9792B18C4[null]
5711female51.0101350277.9583D11S10[null]
5811female35.01011378952.0[null]S8[null]
5911male38.0101994390.0C93SD[null]
6011female35.0101994390.0C93SD[null]
6111female45.0101175352.5542D19S5[null]
6211female49.0001746525.9292D17S8[null]
6311female16.001PC 1759239.4D28S9[null]
6411female15.00124160211.3375B5S2[null]
6511female[null]10PC 1760482.1708[null]C6[null]
6611female22.00111350961.9792B36C5[null]
6711female33.00011015286.5B77S8[null]
6811female18.022PC 17608262.375B57 B59 B63 B66C4[null]
6911male[null]0011116326.0[null]S1[null]
7011male28.00011378835.5A6S7[null]
7111female18.0101369560.0C31S6[null]
7211female40.01116966134.5E34C3[null]
7311female43.0101177855.4417C116C5[null]
7411female48.0001746625.9292D17S8[null]
7511female18.00211041379.65E68S8[null]
7611male17.00217421110.8833C70CB[null]
7711female55.000PC 17760135.6333C32C8[null]
7811female31.00236928164.8667C7S8[null]
7920male18.00023194511.5[null]S[null][null]
8020male18.0002910811.5[null]S[null][null]
8120male23.000C.A. 3103010.5[null]S[null][null]
8220male51.000S.O.P. 116612.525[null]S[null]174.0
8320male28.00024435826.0[null]S[null][null]
8420male60.0112975039.0[null]S[null][null]
8520male19.0002842413.0[null]S[null]18.0
8620male37.010SC/AH 2903726.0[null]S[null]17.0
8720female30.00023724913.0[null]S[null][null]
8820male25.000C.A. 3102931.5[null]S[null][null]
8920male42.0112822032.5[null]S[null][null]
9020male40.010292626.0[null]S[null]286.0
9120male35.00023986526.0[null]S[null]322.0
9220male34.0102866421.0[null]S[null][null]
9320male16.00023986526.0[null]S[null][null]
9420male26.0003102810.5[null]S[null][null]
9520male30.000W/C 1420810.5[null]S[null][null]
9620male24.020S.O.C. 1487973.5[null]S[null][null]
9720male36.00024296313.0[null]S[null][null]
9820male50.00025064313.0[null]S[null]149.0
9920male44.0102670726.0[null]S[null][null]
10020male24.020C.A. 3102931.5[null]S[null][null]
Rows: 1-100 | Columns: 14

You can also use a sample loop with a variable:

Note

VerticaPy will store the object in a temporary local table before executing the overall query, which facilitates integration with in-memory objects.

%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

%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
<IPython.core.display.HTML object>
CREATE
<IPython.core.display.HTML object>
INSERT
<IPython.core.display.HTML object>
INSERT
<IPython.core.display.HTML object>
INSERT
<IPython.core.display.HTML object>
INSERT
<IPython.core.display.HTML object>
%%sql
SELECT * FROM test;

Execution: 0.005s

123
id
Integer
12
23
30
41
Rows: 1-4 | Column: id | Type: Integer

Change the maximum number of rows/columns to display

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

%%sql -nrows 5 -ncols 2
SELECT * FROM public.titanic;

Execution: 0.008s

123
pclass
Integer
...
Abc
home.dest
Varchar(100)
11...Montevideo, Uruguay
21...Trenton, NJ
31...[null]
41...Montevideo, Uruguay
51...Los Angeles, CA
Rows: 1-5 | Columns: 14

Export results to a JSON or CSV file

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

%%sql -o titanic_age_clean.csv
SELECT
    *
FROM public.titanic
WHERE age IS NOT NULL LIMIT 5;

Execution: 0.008s

123
pclass
Integer
123
survived
Integer
Abc
name
Varchar(164)
Abc
sex
Varchar(20)
123
age
Numeric(8)
123
sibsp
Integer
123
parch
Integer
Abc
ticket
Varchar(36)
123
fare
Numeric(12)
Abc
cabin
Varchar(30)
Abc
embarked
Varchar(20)
Abc
boat
Varchar(100)
123
body
Integer
Abc
home.dest
Varchar(100)
110Artagaveytia, Mr. Ramonmale71.000PC 1760949.5042[null]C[null]22Montevideo, Uruguay
210Blackwell, Mr. Stephen Weartmale45.00011378435.5TS[null][null]Trenton, NJ
310Carrau, Mr. Jose Pedromale17.00011305947.1[null]S[null][null]Montevideo, Uruguay
410Clark, Mr. Walter Millermale27.01013508136.7792C89C[null][null]Los Angeles, CA
510Compton, Mr. Alexander Taylor Jrmale37.011PC 1775683.1583E52C[null][null]Lakewood, NJ
Rows: 5 | Columns: 14
file = open("titanic_age_clean.csv", "r")

print(file.read())
"pclass","survived","name","sex","age","sibsp","parch","ticket","fare","cabin","embarked","boat","body","home.dest"
1,0,"Artagaveytia, Mr. Ramon","male",71.000,0,0,"PC 17609",49.50420,,"C",,22,"Montevideo, Uruguay"
1,0,"Blackwell, Mr. Stephen Weart","male",45.000,0,0,"113784",35.50000,"T","S",,,"Trenton, NJ"
1,0,"Carrau, Mr. Jose Pedro","male",17.000,0,0,"113059",47.10000,,"S",,,"Montevideo, Uruguay"
1,0,"Clark, Mr. Walter Miller","male",27.000,1,0,"13508",136.77920,"C89","C",,,"Los Angeles, CA"
1,0,"Compton, Mr. Alexander Taylor Jr","male",37.000,1,1,"PC 17756",83.15830,"E52","C",,,"Lakewood, NJ"

file.close()

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

%%sql -o titanic_age_clean.json
SELECT
    *
FROM public.titanic
WHERE age IS NOT NULL LIMIT 5;

Execution: 0.008s

123
pclass
Integer
123
survived
Integer
Abc
name
Varchar(164)
Abc
sex
Varchar(20)
123
age
Numeric(8)
123
sibsp
Integer
123
parch
Integer
Abc
ticket
Varchar(36)
123
fare
Numeric(12)
Abc
cabin
Varchar(30)
Abc
embarked
Varchar(20)
Abc
boat
Varchar(100)
123
body
Integer
Abc
home.dest
Varchar(100)
110Artagaveytia, Mr. Ramonmale71.000PC 1760949.5042[null]C[null]22Montevideo, Uruguay
210Blackwell, Mr. Stephen Weartmale45.00011378435.5TS[null][null]Trenton, NJ
310Carrau, Mr. Jose Pedromale17.00011305947.1[null]S[null][null]Montevideo, Uruguay
410Clark, Mr. Walter Millermale27.01013508136.7792C89C[null][null]Los Angeles, CA
510Compton, Mr. Alexander Taylor Jrmale37.011PC 1775683.1583E52C[null][null]Lakewood, NJ
Rows: 5 | Columns: 14
file = open("titanic_age_clean.json", "r")

print(file.read())
[
{"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"},
{"pclass": 1, "survived": 0, "name": "Blackwell, Mr. Stephen Weart", "sex": "male", "age": 45.000, "sibsp": 0, "parch": 0, "ticket": "113784", "fare": 35.50000, "cabin": "T", "embarked": "S", "home.dest": "Trenton, NJ"},
{"pclass": 1, "survived": 0, "name": "Carrau, Mr. Jose Pedro", "sex": "male", "age": 17.000, "sibsp": 0, "parch": 0, "ticket": "113059", "fare": 47.10000, "embarked": "S", "home.dest": "Montevideo, Uruguay"},
{"pclass": 1, "survived": 0, "name": "Clark, Mr. Walter Miller", "sex": "male", "age": 27.000, "sibsp": 1, "parch": 0, "ticket": "13508", "fare": 136.77920, "cabin": "C89", "embarked": "C", "home.dest": "Los Angeles, CA"},
{"pclass": 1, "survived": 0, "name": "Compton, Mr. Alexander Taylor Jr", "sex": "male", "age": 37.000, "sibsp": 1, "parch": 1, "ticket": "PC 17756", "fare": 83.15830, "cabin": "E52", "embarked": "C", "home.dest": "Lakewood, NJ"}
]

file.close()

Execute SQL files

To execute commands from a SQL file, use the following syntax:

file = open("query.sql", "w+")

file.write("SELECT version();")
Out[12]: 17

file.close()

Using the -f option, we can easily read SQL files:

%sql -f query.sql

Execution: 0.006s

Abc
Varchar(128)
1
Rows: 1-1 | Column: version | Type: Varchar(128)

Connect to an external database

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