{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# JOINS\n",
"---\n",
"\n",
"A `join` clause in SQL is used to combine rows from two or more tables, based on a related column between them."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Connecting with the database"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load_ext sql\n",
"# replace with your own credentials\n",
"%sql ibm_db_sa://my-username:my-password@dashdb-txn-sbox-yp-dal09-03.services.dal.bluemix.net:50000/BLUDB"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tables on which all the join operations will be applied are as follows :"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * ibm_db_sa://lzv55210:***@dashdb-txn-sbox-yp-dal09-12.services.dal.bluemix.net:50000/BLUDB\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"
\n",
" \n",
" country | \n",
" first_name | \n",
" last_name | \n",
" test_score | \n",
"
\n",
" \n",
" Mexico | \n",
" Natasha | \n",
" Martinax | \n",
" 48 | \n",
"
\n",
" \n",
" Mexico | \n",
" Rachael | \n",
" Grate | \n",
" 45 | \n",
"
\n",
" \n",
" Romania | \n",
" Alexandra | \n",
" Botez | \n",
" 57 | \n",
"
\n",
" \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
"
\n",
"
"
],
"text/plain": [
"[('Mexico', 'Natasha', 'Martinax', 48),\n",
" ('Mexico', 'Rachael', 'Grate', 45),\n",
" ('Romania', 'Alexandra', 'Botez', 57),\n",
" ('Romania', 'Alina', 'Smolyar', 52)]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%sql select * from NEW_STUDENTS"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * ibm_db_sa://lzv55210:***@dashdb-txn-sbox-yp-dal09-12.services.dal.bluemix.net:50000/BLUDB\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" country | \n",
" first_name | \n",
" last_name | \n",
" test_score | \n",
"
\n",
" \n",
" Russia | \n",
" Bail | \n",
" Goodwin | \n",
" 46 | \n",
"
\n",
" \n",
" Sweden | \n",
" Emlyn | \n",
" Erricker | \n",
" 55 | \n",
"
\n",
" \n",
" Poland | \n",
" Barny | \n",
" Ingerson | \n",
" 57 | \n",
"
\n",
" \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
"
\n",
"
"
],
"text/plain": [
"[('Russia', 'Bail', 'Goodwin', 46),\n",
" ('Sweden', 'Emlyn', 'Erricker', 55),\n",
" ('Poland', 'Barny', 'Ingerson', 57),\n",
" ('Romania', 'Alina', 'Smolyar', 52)]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%sql select * from SPORTS_STUDENTS"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. INNER JOIN\n",
"Inner join creates a new result table by combining cloumn values of two tables (A and B) based upon the join-predicate. It is similar to cross join or cartesian product."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * ibm_db_sa://lzv55210:***@dashdb-txn-sbox-yp-dal09-12.services.dal.bluemix.net:50000/BLUDB\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" country | \n",
" first_name | \n",
" last_name | \n",
" test_score | \n",
" country_1 | \n",
" first_name_1 | \n",
" last_name_1 | \n",
" test_score_1 | \n",
"
\n",
" \n",
" Mexico | \n",
" Natasha | \n",
" Martinax | \n",
" 48 | \n",
" Russia | \n",
" Bail | \n",
" Goodwin | \n",
" 46 | \n",
"
\n",
" \n",
" Mexico | \n",
" Rachael | \n",
" Grate | \n",
" 45 | \n",
" Russia | \n",
" Bail | \n",
" Goodwin | \n",
" 46 | \n",
"
\n",
" \n",
" Romania | \n",
" Alexandra | \n",
" Botez | \n",
" 57 | \n",
" Russia | \n",
" Bail | \n",
" Goodwin | \n",
" 46 | \n",
"
\n",
" \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
" Russia | \n",
" Bail | \n",
" Goodwin | \n",
" 46 | \n",
"
\n",
" \n",
" Mexico | \n",
" Natasha | \n",
" Martinax | \n",
" 48 | \n",
" Sweden | \n",
" Emlyn | \n",
" Erricker | \n",
" 55 | \n",
"
\n",
" \n",
" Mexico | \n",
" Rachael | \n",
" Grate | \n",
" 45 | \n",
" Sweden | \n",
" Emlyn | \n",
" Erricker | \n",
" 55 | \n",
"
\n",
" \n",
" Romania | \n",
" Alexandra | \n",
" Botez | \n",
" 57 | \n",
" Sweden | \n",
" Emlyn | \n",
" Erricker | \n",
" 55 | \n",
"
\n",
" \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
" Sweden | \n",
" Emlyn | \n",
" Erricker | \n",
" 55 | \n",
"
\n",
" \n",
" Mexico | \n",
" Natasha | \n",
" Martinax | \n",
" 48 | \n",
" Poland | \n",
" Barny | \n",
" Ingerson | \n",
" 57 | \n",
"
\n",
" \n",
" Mexico | \n",
" Rachael | \n",
" Grate | \n",
" 45 | \n",
" Poland | \n",
" Barny | \n",
" Ingerson | \n",
" 57 | \n",
"
\n",
" \n",
" Romania | \n",
" Alexandra | \n",
" Botez | \n",
" 57 | \n",
" Poland | \n",
" Barny | \n",
" Ingerson | \n",
" 57 | \n",
"
\n",
" \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
" Poland | \n",
" Barny | \n",
" Ingerson | \n",
" 57 | \n",
"
\n",
" \n",
" Mexico | \n",
" Natasha | \n",
" Martinax | \n",
" 48 | \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
"
\n",
" \n",
" Mexico | \n",
" Rachael | \n",
" Grate | \n",
" 45 | \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
"
\n",
" \n",
" Romania | \n",
" Alexandra | \n",
" Botez | \n",
" 57 | \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
"
\n",
" \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
"
\n",
"
"
],
"text/plain": [
"[('Mexico', 'Natasha', 'Martinax', 48, 'Russia', 'Bail', 'Goodwin', 46),\n",
" ('Mexico', 'Rachael', 'Grate', 45, 'Russia', 'Bail', 'Goodwin', 46),\n",
" ('Romania', 'Alexandra', 'Botez', 57, 'Russia', 'Bail', 'Goodwin', 46),\n",
" ('Romania', 'Alina', 'Smolyar', 52, 'Russia', 'Bail', 'Goodwin', 46),\n",
" ('Mexico', 'Natasha', 'Martinax', 48, 'Sweden', 'Emlyn', 'Erricker', 55),\n",
" ('Mexico', 'Rachael', 'Grate', 45, 'Sweden', 'Emlyn', 'Erricker', 55),\n",
" ('Romania', 'Alexandra', 'Botez', 57, 'Sweden', 'Emlyn', 'Erricker', 55),\n",
" ('Romania', 'Alina', 'Smolyar', 52, 'Sweden', 'Emlyn', 'Erricker', 55),\n",
" ('Mexico', 'Natasha', 'Martinax', 48, 'Poland', 'Barny', 'Ingerson', 57),\n",
" ('Mexico', 'Rachael', 'Grate', 45, 'Poland', 'Barny', 'Ingerson', 57),\n",
" ('Romania', 'Alexandra', 'Botez', 57, 'Poland', 'Barny', 'Ingerson', 57),\n",
" ('Romania', 'Alina', 'Smolyar', 52, 'Poland', 'Barny', 'Ingerson', 57),\n",
" ('Mexico', 'Natasha', 'Martinax', 48, 'Romania', 'Alina', 'Smolyar', 52),\n",
" ('Mexico', 'Rachael', 'Grate', 45, 'Romania', 'Alina', 'Smolyar', 52),\n",
" ('Romania', 'Alexandra', 'Botez', 57, 'Romania', 'Alina', 'Smolyar', 52),\n",
" ('Romania', 'Alina', 'Smolyar', 52, 'Romania', 'Alina', 'Smolyar', 52)]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%sql select * from NEW_STUDENTS inner join SPORTS_STUDENTS on True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. OUTER JOIN"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### • Left outer join\n",
"The result of a left outer join for tables A and B always contain all the records of the `left` table (A), even if the join condition does not find any matching record in the `right` table (B). "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * ibm_db_sa://lzv55210:***@dashdb-txn-sbox-yp-dal09-12.services.dal.bluemix.net:50000/BLUDB\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" country | \n",
" first_name | \n",
" last_name | \n",
" test_score | \n",
" country_1 | \n",
" first_name_1 | \n",
" last_name_1 | \n",
" test_score_1 | \n",
"
\n",
" \n",
" Romania | \n",
" Alexandra | \n",
" Botez | \n",
" 57 | \n",
" Poland | \n",
" Barny | \n",
" Ingerson | \n",
" 57 | \n",
"
\n",
" \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
"
\n",
" \n",
" Mexico | \n",
" Natasha | \n",
" Martinax | \n",
" 48 | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" Mexico | \n",
" Rachael | \n",
" Grate | \n",
" 45 | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
"
"
],
"text/plain": [
"[('Romania', 'Alexandra', 'Botez', 57, 'Poland', 'Barny', 'Ingerson', 57),\n",
" ('Romania', 'Alina', 'Smolyar', 52, 'Romania', 'Alina', 'Smolyar', 52),\n",
" ('Mexico', 'Natasha', 'Martinax', 48, None, None, None, None),\n",
" ('Mexico', 'Rachael', 'Grate', 45, None, None, None, None)]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%sql select * from NEW_STUDENTS left outer join SPORTS_STUDENTS on NEW_STUDENTS.test_score=SPORTS_STUDENTS.test_score"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### • Right outer join\n",
"Every row from the `right` table (B) will appear in the joined table atleast once. If no matching row from the `left` table (A) exists, NULL will appear in the columns from A for those records that have no match on B."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * ibm_db_sa://lzv55210:***@dashdb-txn-sbox-yp-dal09-12.services.dal.bluemix.net:50000/BLUDB\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" country | \n",
" first_name | \n",
" last_name | \n",
" test_score | \n",
" country_1 | \n",
" first_name_1 | \n",
" last_name_1 | \n",
" test_score_1 | \n",
"
\n",
" \n",
" Romania | \n",
" Alexandra | \n",
" Botez | \n",
" 57 | \n",
" Poland | \n",
" Barny | \n",
" Ingerson | \n",
" 57 | \n",
"
\n",
" \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
"
\n",
" \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" Sweden | \n",
" Emlyn | \n",
" Erricker | \n",
" 55 | \n",
"
\n",
" \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" Russia | \n",
" Bail | \n",
" Goodwin | \n",
" 46 | \n",
"
\n",
"
"
],
"text/plain": [
"[('Romania', 'Alexandra', 'Botez', 57, 'Poland', 'Barny', 'Ingerson', 57),\n",
" ('Romania', 'Alina', 'Smolyar', 52, 'Romania', 'Alina', 'Smolyar', 52),\n",
" (None, None, None, None, 'Sweden', 'Emlyn', 'Erricker', 55),\n",
" (None, None, None, None, 'Russia', 'Bail', 'Goodwin', 46)]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%sql select * from NEW_STUDENTS right outer join SPORTS_STUDENTS on NEW_STUDENTS.test_score=SPORTS_STUDENTS.test_score"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### • Full outer join\n",
"The full outer join combines the result of both left and right outer join and returns all rows from both the tables."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * ibm_db_sa://lzv55210:***@dashdb-txn-sbox-yp-dal09-12.services.dal.bluemix.net:50000/BLUDB\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" country | \n",
" first_name | \n",
" last_name | \n",
" test_score | \n",
" country_1 | \n",
" first_name_1 | \n",
" last_name_1 | \n",
" test_score_1 | \n",
"
\n",
" \n",
" Romania | \n",
" Alexandra | \n",
" Botez | \n",
" 57 | \n",
" Poland | \n",
" Barny | \n",
" Ingerson | \n",
" 57 | \n",
"
\n",
" \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
" Romania | \n",
" Alina | \n",
" Smolyar | \n",
" 52 | \n",
"
\n",
" \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" Sweden | \n",
" Emlyn | \n",
" Erricker | \n",
" 55 | \n",
"
\n",
" \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" Russia | \n",
" Bail | \n",
" Goodwin | \n",
" 46 | \n",
"
\n",
" \n",
" Mexico | \n",
" Natasha | \n",
" Martinax | \n",
" 48 | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
" \n",
" Mexico | \n",
" Rachael | \n",
" Grate | \n",
" 45 | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
"
"
],
"text/plain": [
"[('Romania', 'Alexandra', 'Botez', 57, 'Poland', 'Barny', 'Ingerson', 57),\n",
" ('Romania', 'Alina', 'Smolyar', 52, 'Romania', 'Alina', 'Smolyar', 52),\n",
" (None, None, None, None, 'Sweden', 'Emlyn', 'Erricker', 55),\n",
" (None, None, None, None, 'Russia', 'Bail', 'Goodwin', 46),\n",
" ('Mexico', 'Natasha', 'Martinax', 48, None, None, None, None),\n",
" ('Mexico', 'Rachael', 'Grate', 45, None, None, None, None)]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%sql select * from NEW_STUDENTS full outer join SPORTS_STUDENTS on NEW_STUDENTS.test_score=SPORTS_STUDENTS.test_score"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Thanks for reading :)\n",
"Created by [Tarun Kamboj](https://www.linkedin.com/in/kambojtarun/)."
]
}
],
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}