L'opérateur SELECT¶


Lien : https://www.youtube.com/watch?v=bpRirgAiyhw&t=108s

Github le coin stat : https://github.com/LeCoinStat/LeCoinStat/tree/main/Bien_Debuter_SQL

Fichiers Github récupérés : https://github.com/LeCoinStat/LeCoinStat/blob/main/Bien_Debuter_SQL/Jour1/CreationTable

Syntaxe en sélectionnant plusieurs colonnes

SELECT *
FROM table;

Syntaxe en sélectionnant une seule colonne

SELECT colonne
FROM table;

Syntaxe en sélectionnant plusieurs colonnes

SELECT 
    colonne1
    , colonne2
FROM table;

In [ ]:
import duckdb
from skimpy import skim
In [7]:
# Connexion à la librairie duckdb
conn = duckdb.connect()

Fichier des produits¶

In [8]:
# Requête SQL : récupération de toutes les données du fichier .csv en DF
products_df = conn.execute(
    """
    SELECT *
    FROM read_csv_auto('data/IndexE/Produits.csv', header=True);
    """
).df()
In [9]:
# Données stats de la DF
skim(products_df)
╭──────────────────────────────────────────────── skimpy summary ─────────────────────────────────────────────────╮
│          Data Summary                Data Types                                                                 │
│ ┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┓ ┏━━━━━━━━━━━━━┳━━━━━━━┓                                                          │
│ ┃ Dataframe         ┃ Values ┃ ┃ Column Type ┃ Count ┃                                                          │
│ ┡━━━━━━━━━━━━━━━━━━━╇━━━━━━━━┩ ┡━━━━━━━━━━━━━╇━━━━━━━┩                                                          │
│ │ Number of rows    │ 100    │ │ int64       │ 3     │                                                          │
│ │ Number of columns │ 5      │ │ string      │ 2     │                                                          │
│ └───────────────────┴────────┘ └─────────────┴───────┘                                                          │
│                                                     number                                                      │
│ ┏━━━━━━━━━━━━━━━━━━━━┳━━━━━━┳━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━┳━━━━━━━━━┓  │
│ ┃ column             ┃ NA   ┃ NA %   ┃ mean    ┃ sd      ┃ p0  ┃ p25     ┃ p50    ┃ p75    ┃ p100  ┃ hist    ┃  │
│ ┡━━━━━━━━━━━━━━━━━━━━╇━━━━━━╇━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━╇━━━━━━━╇━━━━━━━━━┩  │
│ │     ProduitID      │    0 │      0 │    50.5 │   29.01 │   1 │   25.75 │   50.5 │  75.25 │   100 │ ▇▇▇▇▇▇  │  │
│ │    PrixUnitaire    │    0 │      0 │   486.7 │   326.6 │  59 │     129 │    399 │    699 │   999 │ ▇▃▂ ▇▃  │  │
│ │   FournisseurID    │    0 │      0 │   54.04 │   30.51 │   1 │      31 │     56 │     84 │   100 │ ▆▃▅▃▅▇  │  │
│ └────────────────────┴──────┴────────┴─────────┴─────────┴─────┴─────────┴────────┴────────┴───────┴─────────┘  │
│                                                     string                                                      │
│ ┏━━━━━━━━━━━┳━━━━┳━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━┓  │
│ ┃           ┃    ┃      ┃           ┃           ┃           ┃           ┃ chars per ┃ words per ┃ total      ┃  │
│ ┃ column    ┃ NA ┃ NA % ┃ shortest  ┃ longest   ┃ min       ┃ max       ┃ row       ┃ row       ┃ words      ┃  │
│ ┡━━━━━━━━━━━╇━━━━╇━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━┩  │
│ │ NomProdui │  0 │    0 │ iPhone 12 │   Ralph   │ Dell XPS  │ iPhone 12 │      13.6 │       2.7 │        266 │  │
│ │     t     │    │      │           │  Lauren   │    13     │           │           │           │            │  │
│ │           │    │      │           │   Polo    │           │           │           │           │            │  │
│ │           │    │      │           │   Shirt   │           │           │           │           │            │  │
│ │ Descripti │  0 │    0 │     A     │ Cordless  │ 13.4-inch │   Stand   │      87.3 │        14 │       1371 │  │
│ │    on     │    │      │ 6.1-inch  │  vacuum   │   FHD+    │   mixer   │           │           │            │  │
│ │           │    │      │ smartphon │  cleaner  │  laptop   │   with    │           │           │            │  │
│ │           │    │      │  e with   │   with    │ with 10th │ 10-speed  │           │           │            │  │
│ │           │    │      │    A14    │  strong   │ Gen Intel │ settings, │           │           │            │  │
│ │           │    │      │  Bionic   │  suction  │   Core    │     a     │           │           │            │  │
│ │           │    │      │   chip,   │  power,   │ processor │ tilt-head │           │           │            │  │
│ │           │    │      │ dual-came │ multiple  │  s and a  │  design,  │           │           │            │  │
│ │           │    │      │    ra     │ attachmen │ stunning  │ and comes │           │           │            │  │
│ │           │    │      │  system,  │  ts, and  │  HDR 400  │   with    │           │           │            │  │
│ │           │    │      │  and 5G   │ up to 60  │ display.  │ multiple  │           │           │            │  │
│ │           │    │      │  speed.   │  minutes  │           │ attachmen │           │           │            │  │
│ │           │    │      │           │  of run   │           │    ts.    │           │           │            │  │
│ │           │    │      │           │   time.   │           │           │           │           │            │  │
│ └───────────┴────┴──────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴────────────┘  │
╰────────────────────────────────────────────────────── End ──────────────────────────────────────────────────────╯
In [10]:
# Enregistrement de la DF en BD virtuelle (requête SQL : VIEW)
conn.register('products_db', products_df)
Out[10]:
<_duckdb.DuckDBPyConnection at 0x231f27c9670>

Fichier des clients¶

In [11]:
# Requête SQL : récupération de toutes les données du fichier .csv
customers_df = conn.execute(
    """
    SELECT *
    FROM read_csv_auto('data/IndexE/Clients.csv', header=True);
    """
).df()
In [12]:
# Données stats de la DF
skim(customers_df)
╭──────────────────────────────────────────────── skimpy summary ─────────────────────────────────────────────────╮
│          Data Summary                Data Types                                                                 │
│ ┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┓ ┏━━━━━━━━━━━━━┳━━━━━━━┓                                                          │
│ ┃ Dataframe         ┃ Values ┃ ┃ Column Type ┃ Count ┃                                                          │
│ ┡━━━━━━━━━━━━━━━━━━━╇━━━━━━━━┩ ┡━━━━━━━━━━━━━╇━━━━━━━┩                                                          │
│ │ Number of rows    │ 100    │ │ string      │ 5     │                                                          │
│ │ Number of columns │ 6      │ │ int64       │ 1     │                                                          │
│ └───────────────────┴────────┘ └─────────────┴───────┘                                                          │
│                                                     number                                                      │
│ ┏━━━━━━━━━━━━━━┳━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━┳━━━━━━┳━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━┓  │
│ ┃ column       ┃ NA   ┃ NA %    ┃ mean    ┃ sd       ┃ p0   ┃ p25      ┃ p50    ┃ p75     ┃ p100   ┃ hist    ┃  │
│ ┡━━━━━━━━━━━━━━╇━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━╇━━━━━━╇━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━┩  │
│ │   ClientID   │    0 │       0 │    50.5 │    29.01 │    1 │    25.75 │   50.5 │   75.25 │    100 │ ▇▇▇▇▇▇  │  │
│ └──────────────┴──────┴─────────┴─────────┴──────────┴──────┴──────────┴────────┴─────────┴────────┴─────────┘  │
│                                                     string                                                      │
│ ┏━━━━━━━━━━━┳━━━━┳━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━┓  │
│ ┃           ┃    ┃      ┃           ┃           ┃           ┃           ┃ chars per ┃ words per ┃ total      ┃  │
│ ┃ column    ┃ NA ┃ NA % ┃ shortest  ┃ longest   ┃ min       ┃ max       ┃ row       ┃ row       ┃ words      ┃  │
│ ┡━━━━━━━━━━━╇━━━━╇━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━┩  │
│ │    Nom    │  0 │    0 │   Pham    │ Cunningha │  Abbott   │  Wilson   │      6.37 │         1 │        100 │  │
│ │           │    │      │           │     m     │           │           │           │           │            │  │
│ │  Prenom   │  0 │    0 │    Zoe    │ Christoph │   Aaron   │    Zoe    │      6.14 │         1 │        100 │  │
│ │           │    │      │           │    er     │           │           │           │           │            │  │
│ │  Adresse  │  0 │    0 │ USS Lee,  │    925    │   00041   │ Unit 6947 │      43.5 │       7.3 │        732 │  │
│ │           │    │      │  FPO AP   │ Robertson │   Green   │ Box 2102, │           │           │            │  │
│ │           │    │      │   23687   │  Springs  │  Street,  │  DPO AP   │           │           │            │  │
│ │           │    │      │           │   Suite   │ Stephenbo │   84355   │           │           │            │  │
│ │           │    │      │           │ 489, Port │ rough, UT │           │           │           │            │  │
│ │           │    │      │           │ Jeffreybe │   34030   │           │           │           │            │  │
│ │           │    │      │           │  rg, IN   │           │           │           │           │            │  │
│ │           │    │      │           │   43983   │           │           │           │           │            │  │
│ │   Email   │  0 │    0 │ snow.zoe@ │ richardso │ abbott.kr │ wilson.vi │      23.5 │         1 │        100 │  │
│ │           │    │      │ gmail.com │ n.cheyenn │ isten@gma │ ncent@gma │           │           │            │  │
│ │           │    │      │           │ e@gmail.c │  il.com   │  il.com   │           │           │            │  │
│ │           │    │      │           │    om     │           │           │           │           │            │  │
│ │ NumeroTel │  0 │    0 │ 042516367 │ 042516367 │ 000137563 │ 099407414 │        10 │         1 │        100 │  │
│ │  ephone   │    │      │     9     │     9     │     3     │     4     │           │           │            │  │
│ └───────────┴────┴──────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴────────────┘  │
╰────────────────────────────────────────────────────── End ──────────────────────────────────────────────────────╯
In [13]:
# Enregistrement de la DF ci-avant en BD virtuelle (CTE)
conn.register('customers_db', customers_df)
Out[13]:
<_duckdb.DuckDBPyConnection at 0x231f27c9670>

Fichier des fournisseurs¶

In [14]:
# Requête SQL : récupération de toutes les données du fichier .csv en df
suppliers_df = conn.execute(
    """
    SELECT *
    FROM read_csv_auto('data/IndexE/Fournisseurs.csv', header=True);
    """
).df()
In [15]:
# Données stats de la df
skim(suppliers_df)
╭──────────────────────────────────────────────── skimpy summary ─────────────────────────────────────────────────╮
│          Data Summary                Data Types                                                                 │
│ ┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┓ ┏━━━━━━━━━━━━━┳━━━━━━━┓                                                          │
│ ┃ Dataframe         ┃ Values ┃ ┃ Column Type ┃ Count ┃                                                          │
│ ┡━━━━━━━━━━━━━━━━━━━╇━━━━━━━━┩ ┡━━━━━━━━━━━━━╇━━━━━━━┩                                                          │
│ │ Number of rows    │ 100    │ │ string      │ 4     │                                                          │
│ │ Number of columns │ 5      │ │ int64       │ 1     │                                                          │
│ └───────────────────┴────────┘ └─────────────┴───────┘                                                          │
│                                                     number                                                      │
│ ┏━━━━━━━━━━━━━━━━━━━━┳━━━━━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━━━┳━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━┳━━━━━━━┳━━━━━━━━━┓  │
│ ┃ column             ┃ NA   ┃ NA %   ┃ mean   ┃ sd      ┃ p0  ┃ p25     ┃ p50    ┃ p75     ┃ p100  ┃ hist    ┃  │
│ ┡━━━━━━━━━━━━━━━━━━━━╇━━━━━━╇━━━━━━━━╇━━━━━━━━╇━━━━━━━━━╇━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━╇━━━━━━━╇━━━━━━━━━┩  │
│ │   FournisseurID    │    0 │      0 │   50.5 │   29.01 │   1 │   25.75 │   50.5 │   75.25 │   100 │ ▇▇▇▇▇▇  │  │
│ └────────────────────┴──────┴────────┴────────┴─────────┴─────┴─────────┴────────┴─────────┴───────┴─────────┘  │
│                                                     string                                                      │
│ ┏━━━━━━━━━━━┳━━━━┳━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━┓  │
│ ┃           ┃    ┃      ┃           ┃           ┃           ┃           ┃ chars per ┃ words per ┃ total      ┃  │
│ ┃ column    ┃ NA ┃ NA % ┃ shortest  ┃ longest   ┃ min       ┃ max       ┃ row       ┃ row       ┃ words      ┃  │
│ ┡━━━━━━━━━━━╇━━━━╇━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━┩  │
│ │ NomFourni │  0 │    0 │ Diaz PLC  │ Rodriguez │  Acosta,  │   Woods   │      15.9 │       2.3 │        227 │  │
│ │   sseur   │    │      │           │ , Rivera  │  Ferrell  │   Group   │           │           │            │  │
│ │           │    │      │           │    and    │    and    │           │           │           │            │  │
│ │           │    │      │           │ Thompson  │ Mcdonald  │           │           │           │            │  │
│ │  Adresse  │  0 │    0 │    USS    │   5404    │    011    │ Unit 7388 │        45 │       7.4 │        742 │  │
│ │           │    │      │  Harris,  │ Martinez  │  Thomas   │ Box 6673, │           │           │            │  │
│ │           │    │      │  FPO AP   │   Parks   │  Island   │  DPO AE   │           │           │            │  │
│ │           │    │      │   47894   │   Suite   │ Apt. 974, │   73646   │           │           │            │  │
│ │           │    │      │           │   048,    │   Lake    │           │           │           │            │  │
│ │           │    │      │           │   South   │ Sarah, ME │           │           │           │            │  │
│ │           │    │      │           │ Brittneys │   59031   │           │           │           │            │  │
│ │           │    │      │           │ hire, LA  │           │           │           │           │            │  │
│ │           │    │      │           │   95383   │           │           │           │           │            │  │
│ │   Email   │  0 │    0 │ diazplc@g │ rodriguez │ acosta_fe │ woodsgrou │      24.7 │         1 │        100 │  │
│ │           │    │      │ mail.com  │ _riveraan │ rrellandm │ p@gmail.c │           │           │            │  │
│ │           │    │      │           │ dthompson │ cdonald@g │    om     │           │           │            │  │
│ │           │    │      │           │ @gmail.co │ mail.com  │           │           │           │            │  │
│ │           │    │      │           │     m     │           │           │           │           │            │  │
│ │ NumeroTel │  0 │    0 │ 039979111 │ 039979111 │ 000465149 │ 098900955 │        10 │         1 │        100 │  │
│ │  ephone   │    │      │     8     │     8     │     1     │     0     │           │           │            │  │
│ └───────────┴────┴──────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴────────────┘  │
╰────────────────────────────────────────────────────── End ──────────────────────────────────────────────────────╯
In [16]:
# Enregistrement de la DF ci-avant en BD virtuelle (CTE)
conn.register("suppliers_db", suppliers_df)
Out[16]:
<_duckdb.DuckDBPyConnection at 0x231f27c9670>

Requêtes SQL¶

Donner la table complète des clients de l'entreprise¶

In [17]:
conn.execute(
    """
    -- Colonnes récupérées
    SELECT *
    -- BD récupérée
    FROM customers_db;
    """
).df()
Out[17]:
ClientID Nom Prenom Adresse Email NumeroTelephone
0 1 Thompson Shannon 384 Howard Parks, New Tony, DE 67522 thompson.shannon@gmail.com 0425163679
1 2 Pham Benjamin 15250 Anita Pine Apt. 887, New Kristenport, AR... pham.benjamin@gmail.com 0544573781
2 3 Rodriguez Bonnie 515 Henderson Coves Apt. 298, Lewismouth, OR 1... rodriguez.bonnie@gmail.com 0956054323
3 4 Perez Stephanie 619 Juan Parkways Suite 983, Tracibury, NM 13549 perez.stephanie@gmail.com 0037851314
4 5 Kramer Joyce 85731 Danny Groves Suite 513, Lake Michaelvill... kramer.joyce@gmail.com 0705010731
... ... ... ... ... ... ...
95 96 Rodriguez Tammy USS Berg, FPO AP 26777 rodriguez.tammy@gmail.com 0198156282
96 97 Hunter Kimberly 99327 Lewis Glens, West Patrickstad, FL 88083 hunter.kimberly@gmail.com 0503857599
97 98 Hess Ryan Unit 4039 Box 6603, DPO AE 40744 hess.ryan@gmail.com 0508092073
98 99 Harvey Michael 6742 Jackson Corner, South Isabel, VT 46191 harvey.michael@gmail.com 0192649531
99 100 Campbell Jordan 9418 Salazar Forks Suite 382, Lake Paul, NJ 71313 campbell.jordan@gmail.com 0025669814

100 rows × 6 columns

Donner la table complète des produits vendus par l'entreprise¶

In [18]:
conn.execute(
    """
    -- Colonnes récupéreés
    SELECT *
    -- BD récupérée
    FROM products_db;
    """
).df()
Out[18]:
ProduitID NomProduit Description PrixUnitaire FournisseurID
0 1 Samsung Galaxy S21 Featuring a 6.2-inch dynamic AMOLED display, t... 699 4
1 2 Samsung TV 55' 55-inch 4K UHD Smart TV with HDR, Crystal Disp... 129 51
2 3 Nike Air Max Running shoes with Max Air cushioning, mesh up... 85 93
3 4 Levi's Jeans Classic denim jeans with a straight leg fit, f... 299 8
4 5 Dyson Vacuum Cordless vacuum cleaner with strong suction po... 999 58
... ... ... ... ... ...
95 96 iPhone 12 A 6.1-inch smartphone with A14 Bionic chip, du... 699 42
96 97 iPhone 12 A 6.1-inch smartphone with A14 Bionic chip, du... 999 86
97 98 Dell XPS 13 13.4-inch FHD+ laptop with 10th Gen Intel Core... 799 33
98 99 Dyson Vacuum Cordless vacuum cleaner with strong suction po... 999 84
99 100 Samsung TV 55' 55-inch 4K UHD Smart TV with HDR, Crystal Disp... 299 16

100 rows × 5 columns

Donner le nom de tous les produits de la base de données¶

In [ ]:
conn.execute(
    """
    -- Colonnes récupérées
    SELECT DISTINCT("NomProduit") AS "Liste des produits"
    -- BD récupérée
    FROM products_db;
    """
).df()
Out[ ]:
Liste des produits
0 Levi's Jeans
1 Dell XPS 13
2 KitchenAid Mixer
3 Samsung Galaxy S21
4 Nike Air Max
5 Samsung TV 55'
6 Dyson Vacuum
7 iPhone 12
8 Ralph Lauren Polo Shirt

Donner le nom de tous les fournisseurs de la base de données¶

In [20]:
conn.execute(
    """
    -- Colonnes récupérées
    SELECT DISTINCT("NomFournisseur") AS "Liste des fournisseurs"
    -- BD récupérée
    FROM suppliers_db;
    """
).df()
Out[20]:
Liste des fournisseurs
0 Reyes, Flores and Le
1 Hawkins Group
2 Morgan LLC
3 Parks PLC
4 Shelton, Robinson and Lara
... ...
95 Harvey Group
96 Coleman-Vega
97 Davis, Ramirez and Jenkins
98 Nelson, Sullivan and Dixon
99 Chen, Jackson and Preston

100 rows × 1 columns

Donner le nom et l'ID de tous les fournisseurs de la base de données¶

In [21]:
conn.execute(
    """
    -- Colonnes récupérées
    SELECT 
        DISTINCT("FournisseurID")
        , "NomFournisseur"
    -- BD récupérée
    FROM suppliers_db;
    """
).df()
Out[21]:
FournisseurID NomFournisseur
0 4 Jenkins-Gray
1 6 Lee-Strickland
2 10 Reyes, Flores and Le
3 12 Jackson-Cole
4 29 Holloway and Sons
... ... ...
95 78 Jacobs PLC
96 82 Miller, Smith and Beck
97 84 Velasquez-Irwin
98 88 Davis, Ramirez and Jenkins
99 97 Avila-Thomas

100 rows × 2 columns