Ex08-Joining Tables
Ex08-Joining Tables
ipynb - Colab
To connect tables in a query, we use a JOIN ... ON statement. There are different types of SQLite joins:
The RIGHT OUTER JOIN and FULL OUTER JOIN are not supported in SQLite.
%load_ext sql
u'Connected: @data/demo.db3'
If you do not remember the tables in the demo data, you can always use the following command to query.
* sqlite:///data/demo.db3
Done.
name
rch
hru
sub
sed
watershed_daily
watershed_monthly
watershed_yearly
channel_dimension
hru_info
sub_info
rch_info
ave_plant
ave_annual_hru
ave_monthly_basin
ave_annual_basin
Here we take the tables of rch and sub as an example. There are three commom columns of RCH/SUB, YR and MO.
https://colab.research.google.com/drive/1ktx-kaM0azebQ-GHqte44q9Bgf3JQouw#printMode=true 1/4
7/25/24, 4:26 PM ex08-Joining Tables.ipynb - Colab
* sqlite:///data/demo.db3
Done.
RCH YR MO FLOW_INcms FLOW_OUTcms EVAPcms TLOSScms SED_INtons SED_OUTtons SEDCONCmg_kg ORGN_INkg ORGN_OUT
2.33204616507e-
1 1981 1 146.343765259 146.252487183 0.0912808850408 0.0 61619.4648438 155.371902466 0.0160862877965 0.0
07
1.64267646596e-
2 1981 1 96.225692749 96.1828536987 0.0428212843835 0.0 0.0 0.0 0.0136315366253 0.0
07
2.03258238685e- 2.03258238685e- 6.59506094181e-
3 1981 1 11.9527187347 11.8613681793 0.0913518294692 0.0 0.0114662880078 0.0
07 07 09
* sqlite:///data/demo.db3
Done.
SUB YR MO PRECIPmm SNOMELTmm PETmm ETmm SWmm PERCmm SURQmm GW_Qmm WYLDmm S
2.37951117015e- 3.44
1 1981 1 35.6019897461 0.0 3.72074365616 0.24964235723 10.7985944748 0.0 0.0537296123803 0.298895508051
05 13
1.72378986463e- 2.07
2 1981 1 108.606071472 0.0 3.45040774345 0.457205563784 56.3250045776 0.0 28.5720500946 32.5934295654
05 13
2.4236529498e- 3.34
3 1981 1 149.308364868 0.0 10.566324234 6.0271062851 71.0020980835 0.0 3.96275544167 5.27445697784
05 13
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Note: When SELECTing the common columns, have to clearly asign a table'name. If column names or table names are too long, we can use
aliases to give them short names.
%%sql sqlite://
SELECT RCH, rch.YR, rch.MO, FLOW_INcms, FLOW_OUTcms, PRECIPmm, PETmm
FROM rch INNER JOIN sub
ON rch.RCH = sub.SUB and rch.YR=sub.YR and rch.MO=sub.MO
LIMIT 5
Done.
RCH YR MO FLOW_INcms FLOW_OUTcms PRECIPmm PETmm
1 1981 1 146.343765259 146.252487183 35.6019897461 3.72074365616
2 1981 1 96.225692749 96.1828536987 108.606071472 3.45040774345
3 1981 1 11.9527187347 11.8613681793 149.308364868 10.566324234
4 1981 1 49.486492157 49.4065132141 108.606048584 10.674993515
5 1981 1 274.066802979 272.106018066 201.311279297 27.1792430878
SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
https://colab.research.google.com/drive/1ktx-kaM0azebQ-GHqte44q9Bgf3JQouw#printMode=true 2/4
7/25/24, 4:26 PM ex08-Joining Tables.ipynb - Colab
%%sql sqlite://
SELECT RCH, rch.YR, rch.MO, FLOW_INcms, FLOW_OUTcms, PRECIPmm, PETmm
FROM rch LEFT JOIN sub
ON rch.RCH = sub.SUB and rch.YR=sub.YR and rch.MO=sub.MO
LIMIT 5
Done.
RCH YR MO FLOW_INcms FLOW_OUTcms PRECIPmm PETmm
1 1981 1 146.343765259 146.252487183 35.6019897461 3.72074365616
2 1981 1 96.225692749 96.1828536987 108.606071472 3.45040774345
3 1981 1 11.9527187347 11.8613681793 149.308364868 10.566324234
4 1981 1 49.486492157 49.4065132141 108.606048584 10.674993515
5 1981 1 274.066802979 272.106018066 201.311279297 27.1792430878
SELECT columns
FROM table1
CROSS JOIN table2;
%%sql sqlite://
SELECT RCH, rch.YR, rch.MO, FLOW_INcms, FLOW_OUTcms, PRECIPmm, PETmm
FROM rch
CROSS JOIN sub
LIMIT 5
Done.
RCH YR MO FLOW_INcms FLOW_OUTcms PRECIPmm PETmm
1 1981 1 146.343765259 146.252487183 35.6019897461 3.72074365616
1 1981 1 146.343765259 146.252487183 108.606071472 3.45040774345
1 1981 1 146.343765259 146.252487183 149.308364868 10.566324234
1 1981 1 146.343765259 146.252487183 108.606048584 10.674993515
1 1981 1 146.343765259 146.252487183 201.311279297 27.1792430878
Relational databases can be fairly complex in terms of relationships between tables. Sometimes, we have to require information from more
than two tables.
SELECT columns
FROM table1
INNER JOIN table2 ON table1.column = table2.column
INNER JOIN table3 ON table1.column = table3.column
...
INNER JOIN tablen ON table1.column = tablen.column;
There is no limit of maximum number of tables you can join according to SQL itself. However, most DBMSes have their own limits. You should
check your DBMSes docs in practical applications. In addition, the query will will slow down considerably when joining too many tables (e.g., 4
or more tables).
keyboard_arrow_down Summary
In this notebook, we practices the three major join types in SQLite: INNER, LEFT and CROSS joins. Joins allow us to take data scattered across
multiple tables and stitch it together into something more meaningful and descriptive. We can take two or more tables and join them together
into a larger table that has more context. Moreover, using aliases enables us to rename column or table names on the fly.
https://colab.research.google.com/drive/1ktx-kaM0azebQ-GHqte44q9Bgf3JQouw#printMode=true 3/4
7/25/24, 4:26 PM ex08-Joining Tables.ipynb - Colab
https://colab.research.google.com/drive/1ktx-kaM0azebQ-GHqte44q9Bgf3JQouw#printMode=true 4/4