L'opérateur LIMIT¶
Cours : SQL pour Débutant(e)s Jour 3 - Approfondissement des Requêtes SQL Lien : https://www.youtube.com/watch?v=4Ro69oqZ20s
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/tree/main/Bien_Debuter_SQL/Jour3
Syntaxe :
SELECT nom_colonne FROM nom_table WHERE nom_colonne LIMIT nombre_de_lignes;
In [ ]:
import duckdb
from skimpy import skim
In [2]:
# Connection à la librairie duckdb
conn = duckdb.connect()
Fichier des clients¶
In [3]:
# Requête SQL : récupération de toutes les données du fichier .csv en DF
customers_df = conn.execute(
"""
SELECT *
FROM read_csv_auto('data/indexE/Clients.csv', header=True);
"""
).df()
In [4]:
# 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 [5]:
# Enregistrement de la DF ci-avant en BD virtuelle (requête SQL : VIEW)
conn.register('customers_db', customers_df)
Out[5]:
<duckdb.duckdb.DuckDBPyConnection at 0x1f68f5d44f0>
Fichier des produits¶
In [6]:
# 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 [7]:
# Stats de la df
skim(products_df)
╭──────────────────────────────────────────────── skimpy summary ─────────────────────────────────────────────────╮ │ Data Summary Data Types │ │ ┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┓ ┏━━━━━━━━━━━━━┳━━━━━━━┓ │ │ ┃ dataframe ┃ Values ┃ ┃ Column Type ┃ Count ┃ │ │ ┡━━━━━━━━━━━━━━━━━━━╇━━━━━━━━┩ ┡━━━━━━━━━━━━━╇━━━━━━━┩ │ │ │ Number of rows │ 100 │ │ int32 │ 3 │ │ │ │ Number of columns │ 5 │ │ string │ 2 │ │ │ └───────────────────┴────────┘ └─────────────┴───────┘ │ │ number │ │ ┏━━━━━━━━━━━━━━━━━━━━┳━━━━━━┳━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━┳━━━━━━━━━┓ │ │ ┃ column_name ┃ 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 │ │ ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┓ │ │ ┃ column_name ┃ NA ┃ NA % ┃ words per row ┃ total words ┃ │ │ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ │ │ NomProduit │ 0 │ 0 │ 2.7 │ 266 │ │ │ │ Description │ 0 │ 0 │ 14 │ 1371 │ │ │ └───────────────────────────┴─────────┴────────────┴──────────────────────────────┴──────────────────────────┘ │ ╰────────────────────────────────────────────────────── End ──────────────────────────────────────────────────────╯
In [8]:
# Enregistrement de la DF ci-avant en BD virtuelle (requête SQL : VIEW)
conn.register('products_db', products_df)
Out[8]:
<duckdb.duckdb.DuckDBPyConnection at 0x1f68f5d44f0>
Requêtes SQL¶
Donner la liste des 10 premiers clients par ordre alphabétique
In [9]:
conn.execute(
"""
-- Colonnes à récupérer
SELECT
Nom
, Prenom
-- BD récupérée
FROM customers_df
-- Trie
ORDER BY Nom
-- Affichage limité
LIMIT 10;
"""
).df()
Out[9]:
| Nom | Prenom | |
|---|---|---|
| 0 | Abbott | Kristen |
| 1 | Anderson | Scott |
| 2 | Anderson | Aaron |
| 3 | Ballard | Andrea |
| 4 | Barber | Lauren |
| 5 | Becker | Kevin |
| 6 | Brown | Danielle |
| 7 | Campbell | Jordan |
| 8 | Carrillo | Brianna |
| 9 | Castillo | Timothy |
Donner la liste des 10 produits les plus chers
In [10]:
conn.execute(
"""
-- Colonnes récupérées
SELECT
ProduitID
, NomProduit AS "Nom du produit"
, PrixUnitaire AS "Prix Unitaire"
-- BD récupérée
FROM products_df
-- Trie
ORDER BY "Prix Unitaire" DESC
-- Affichage limité
LIMIT 10;
"""
).df()
Out[10]:
| ProduitID | Nom du produit | Prix Unitaire | |
|---|---|---|---|
| 0 | 5 | Dyson Vacuum | 999 |
| 1 | 11 | Dyson Vacuum | 999 |
| 2 | 24 | Dell XPS 13 | 999 |
| 3 | 31 | Dell XPS 13 | 999 |
| 4 | 43 | KitchenAid Mixer | 999 |
| 5 | 65 | Dell XPS 13 | 999 |
| 6 | 77 | KitchenAid Mixer | 999 |
| 7 | 87 | Samsung Galaxy S21 | 999 |
| 8 | 88 | iPhone 12 | 999 |
| 9 | 91 | Samsung Galaxy S21 | 999 |