0% found this document useful (0 votes)
8 views4 pages

Ex08-Joining Tables

Uploaded by

AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Ex08-Joining Tables

Uploaded by

AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

7/25/24, 4:26 PM ex08-Joining Tables.

ipynb - Colab

keyboard_arrow_down ex08-Joining Tables


When we design an entire database system using good design principles like normalization, different aspects of the information need to be
separated into normalized tables. Under such a case, we often require the use of joins to retrieve data from multiple tables in a single SELECT
query. Two tables can be joined by a single join operator, but the result can be joined again with other tables. There must exist a same or similar
column between the tables being joined.

To connect tables in a query, we use a JOIN ... ON statement. There are different types of SQLite joins:

INNER JOIN (or sometimes called simple join)


LEFT OUTER JOIN (or sometimes called LEFT JOIN)
CROSS JOIN

The RIGHT OUTER JOIN and FULL OUTER JOIN are not supported in SQLite.

%load_ext sql

keyboard_arrow_down 1. Connet to the given database of demo.db3


%sql sqlite:///data/demo.db3

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.

%sql SELECT name FROM sqlite_master WHERE type='table'

* 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

keyboard_arrow_down 2. INNER JOIN


The INNER JOIN allows us to merge two tables together. But if we are going to merge tables, we need to define a commonality between the two
so records from both tables line up. We need to define one or more fields they have in common and join on them.

2.1 Chek common columns

Here we take the tables of rch and sub as an example. There are three commom columns of RCH/SUB, YR and MO.

%sql SELECT * From rch LIMIT 3

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

%sql SELECT * From sub LIMIT 3

* 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

keyboard_arrow_down 2.2 Make an inner join

The syntax for the INNER JOIN in SQLite is:

SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

Join on RCH/SUB, YR and MO.

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

keyboard_arrow_down 3. LEFT JOIN


Similar to the INNER JOIN clause, the LEFT JOIN clause is an optional clause of the SELECT statement. You use the LEFT JOIN clause to query
data from multiple correlated tables. This type of join returns all rows from the LEFT-hand table specified in the ON condition and only those
rows from the other table where the joined fields are equal (join condition is met).

The syntax for the SQLite LEFT OUTER JOIN is:

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

keyboard_arrow_down 4. CROSS JOIN


Another type of join is called a SQLite CROSS JOIN. This type of join returns a combined result set with every row from the first table matched
with every row from the second table. This is also called a Cartesian Product.

The syntax for the SQLite CROSS JOIN is:

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

5. Querying Multiple Tables Using JOIN

Relational databases can be fairly complex in terms of relationships between tables. Sometimes, we have to require information from more
than two tables.

We can use the following syntax to join multiple 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.

Start coding or generate with AI.

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

You might also like