In [1]:
import pandas as pd 
import sqlite3

Dataset

In addition, here, we use FIFA 20 Complete Player Datasetet and European Soccer Database from Kaggle.com.

The datasets that we use from the above likes are:

  • Country.csv
  • League.csv
  • players_20.csv
  • Team.csv
  • teams_and_leagues.csv

Acknowledgements

The datasets are also publicly available website sofifa.com.

SQL Joins

In this article, we provide a summary of basic SQL Server commands and syntaxes. For sake of demonstrations SQL Server syntaxes, we use SQLite3. Moreover, the following function can help us to mimic SQL Server Environment in this Jupyter notebook.

In [2]:
connection = sqlite3.connect('FIFA.db')
cursor = connection.cursor()

def mySQL(Syntax): return pd.read_sql_query(Syntax, connection)

Here we provide a summary of popular SQL Server command with examples. For full notes, please see the official SQL Server documents at docs.microsoft.com.

SQL joins are one of the important aspects of extracting information from multiple tables. We only provide a summary of joins in the forms of the following diagram. For a detailed note, please see this link.

Picture Source: codeproject.com

Inner JOIN

SELECT A.Column1, A.Column2, ...
FROM
    TableA A
INNER JOIN TableB B
    ON A.key = B.key;
[into New_Table]

This querying data returns a joining (on defined keys) of TableA and TableB using the records that are available in both Table A and TableB.

In [3]:
mySQL("""
Select
    B.url,
    A.team_long_name,
    B.league_name
from Team A
inner join teams_and_leagues B
on
    A.team_fifa_api_id = B.url
""")
Out[3]:
url team_long_name league_name
0 673 KRC Genk Belgian Jupiler Pro League
1 15005 SV Zulte-Waregem Belgian Jupiler Pro League
2 2007 Sporting Lokeren Belgian Jupiler Pro League
3 1750 KSV Cercle Brugge Belgian Jupiler Pro League
4 229 RSC Anderlecht Belgian Jupiler Pro League
... ... ... ...
316 894 FC Zürich Swiss Super League
317 898 FC St. Gallen Swiss Super League
318 1715 FC Thun Swiss Super League
319 324 Servette FC Swiss Super League
320 1862 FC Lausanne-Sports Swiss Super League

321 rows × 3 columns

Left JOIN

SELECT A.Column1, A.Column2, ...
FROM
    TableA A
Left JOIN TableB B
    ON A.key = B.key;
[into New_Table]

This querying data returns a joining (on defined keys) of TableA and TableB using all records that are available in Table A and the records in TableB that are available in TableA as well.

In [4]:
mySQL("""
Select
    B.url,
    A.team_long_name,
    B.league_name
from Team A
left join teams_and_leagues B
on
    A.team_fifa_api_id = B.url
""")
Out[4]:
url team_long_name league_name
0 673.0 KRC Genk Belgian Jupiler Pro League
1 NaN Beerschot AC None
2 15005.0 SV Zulte-Waregem Belgian Jupiler Pro League
3 2007.0 Sporting Lokeren Belgian Jupiler Pro League
4 1750.0 KSV Cercle Brugge Belgian Jupiler Pro League
... ... ... ...
350 898.0 FC St. Gallen Swiss Super League
351 1715.0 FC Thun Swiss Super League
352 324.0 Servette FC Swiss Super League
353 1862.0 FC Lausanne-Sports Swiss Super League
354 NaN Lugano None

355 rows × 3 columns

Right JOIN

SELECT A.Column1, A.Column2, ...
FROM
    TableA A
Right JOIN TableB B
    ON A.key = B.key;
[into New_Table]

This querying data returns a joining (on defined keys) of TableA and TableB using all records that are available in Table B and the records in TableA that are available in TableB as well.

Note: RIGHT and FULL OUTER JOINs are not currently supported in SQLite

FULL OUTER JOIN

SELECT A.Column1, A.Column2, ...
FROM
    TableA A
FULL OUTER JOIN TableB B
    ON A.key = B.key;
[into New_Table]

This querying data returns a joining (on defined keys) of TableA and TableB using the records that are available in Table A and TableB.

Note: RIGHT and FULL OUTER JOINs are not currently supported in SQLite

Left Excluding JOIN

SELECT A.Column1, A.Column2, ...
FROM
    TableA A
Left JOIN TableB B
    ON A.key = B.key;
Where B.key is Null

This querying data returns a joining (on defined keys) of TableA and TableB using all records that are available in Table A but not in Table B.

In [5]:
mySQL("""
Select
    B.url,
    A.team_long_name,
    B.league_name
from Team A
Left Join teams_and_leagues B
on
    A.team_fifa_api_id = B.url
where
    B.url is Null
""")
Out[5]:
url team_long_name league_name
0 None Beerschot AC None
1 None RAEC Mons None
2 None FCV Dender EH None
3 None KSV Roeselare None
4 None Tubize None
5 None KVC Westerlo None
6 None SC Bastia None
7 None De Graafschap None
8 None FC Volendam None
9 None SC Cambuur None
10 None Ruch Chorzów None
11 None P. Warszawa None
12 None Śląsk Wrocław None
13 None Widzew Łódź None
14 None Odra Wodzisław None
15 None Widzew Łódź None
16 None Zawisza Bydgoszcz None
17 None Termalica Bruk-Bet Nieciecza None
18 None Trofense None
19 None Amadora None
20 None Portimonense None
21 None Feirense None
22 None Uniao da Madeira None
23 None Tondela None
24 None Falkirk None
25 None Inverness Caledonian Thistle None
26 None Dundee United None
27 None Dunfermline Athletic None
28 None RC Recreativo None
29 None Xerez Club Deportivo None
30 None AC Bellinzona None
31 None FC Aarau None
32 None FC Vaduz None
33 None Lugano None

Right Excluding JOIN

SELECT A.Column1, A.Column2, ...
FROM
    TableA A
Left JOIN TableB B
    ON A.key = B.key;
Where A.key is Null

This querying data returns a joining (on defined keys) of TableA and TableB using all records that are available in Table B but not in Table A.

In [6]:
mySQL("""
Select
    B.url,
    A.team_long_name,
    B.league_name
from Team A
Left Join teams_and_leagues B
on
    A.team_fifa_api_id = B.url
where
    A.team_fifa_api_id is Null
""")
Out[6]:
url team_long_name league_name
0 None FCV Dender EH None
1 None Tubize None
2 None FC Volendam None
3 None Termalica Bruk-Bet Nieciecza None
4 None Trofense None
5 None Amadora None
6 None Portimonense None
7 None Feirense None
8 None Uniao da Madeira None
9 None Tondela None
10 None Lugano None

Outer Excluding JOIN

SELECT A.Column1, A.Column2, ...
FROM
    TableA A
FULL OUTER JOIN TableB B
    ON A.key = B.key;
Where
    A.key is Null or B.key is Null

This querying data returns a joining (on defined keys) of TableA and TableB using all records that are available in Table A and Table B but not in the mutual records.

Note: RIGHT and FULL OUTER JOINs are not currently supported in SQLite