/*
Contexte
--------
Une banque possède deux tables :
- customers : référentiel des clients ;
- accounts : historique des versions des comptes.
Schéma
------
customers accounts
+-----------------+--------------+ +-----------------+--------------+
| customer_id | varchar |<-----| customer_id | varchar |
| customer_name | varchar | | account_id | varchar |
| status | varchar | | status | varchar |
+-----------------+--------------+ | updated_at | date |
+-----------------+--------------+
*/
-- ============================================================
-- 1. CRÉATION DES TABLES
-- ============================================================
CREATE TABLE customers (
customer_id VARCHAR(20),
customer_name VARCHAR(100),
status VARCHAR(20)
);
CREATE TABLE accounts (
account_id VARCHAR(20),
customer_id VARCHAR(20),
status VARCHAR(20),
updated_at DATE
);
-- ============================================================
-- 2. DONNÉES DE TEST
-- ============================================================
INSERT INTO customers (
customer_id,
customer_name,
status
)
VALUES
('C001', 'Client A', 'ACTIVE'),
('C002', 'Client B', 'ACTIVE'),
('C003', 'Client C', 'INACTIVE'),
('C004', 'Client D', 'ACTIVE');
INSERT INTO accounts (
account_id,
customer_id,
status,
updated_at
)
VALUES
('A1001', 'C001', 'ACTIVE', '2026-06-26'),
('A1002', 'C002', 'ACTIVE', '2026-06-26'),
('A1003', NULL, 'ACTIVE', '2026-06-24'),
('A1004', 'C999', 'ACTIVE', '2026-06-26'),
('A1005', 'C003', 'ACTIVE', '2026-06-26'),
('A1006', 'C004', 'CLOSED', '2026-06-25'),
('A1006', 'C004', 'ACTIVE', '2026-06-23'),
('A1007', 'C004', 'CLOSED', '2026-06-26');
/*
La table accounts peut contenir plusieurs lignes pour un même account_id.
Chaque ligne représente une version du compte.
La colonne updated_at indique la date de mise à jour.
Questions
=========
1. DERNIÈRE VERSION DE CHAQUE COMPTE
Afficher uniquement la version la plus récente de chaque account_id,
en utilisant la colonne updated_at.
Exemple de résultat attendu :
+------------+-------------+--------+------------+
| account_id | customer_id | status | updated_at |
+------------+-------------+--------+------------+
| A1001 | C001 | ACTIVE | 2026-06-26 |
| A1002 | C002 | ACTIVE | 2026-06-26 |
| A1003 | NULL | ACTIVE | 2026-06-24 |
| A1004 | C999 | ACTIVE | 2026-06-26 |
| A1005 | C003 | ACTIVE | 2026-06-26 |
| A1006 | C004 | ACTIVE | 2026-06-25 |
| A1007 | C004 | CLOSED | 2026-06-26 |
+------------+-------------+--------+------------+
2. CLASSEMENT DES VERSIONS
Ajouter une colonne version_rank en utilisant ROW_NUMBER().
Pour chaque account_id :
- la version la plus récente doit avoir version_rank = 1 ;
- les versions doivent être classées de la plus récente à la plus ancienne.
Exemple de résultat attendu :
+------------+-------------+--------+------------+--------------+
| account_id | customer_id | status | updated_at | version_rank |
+------------+-------------+--------+------------+--------------+
| A1001 | C001 | ACTIVE | 2026-06-26 | 1 |
| A1002 | C002 | ACTIVE | 2026-06-26 | 1 |
| A1003 | NULL | ACTIVE | 2026-06-24 | 1 |
| A1004 | C999 | ACTIVE | 2026-06-26 | 1 |
| A1005 | C003 | ACTIVE | 2026-06-26 | 1 |
| A1006 | C004 | ACTIVE | 2026-06-26 | 1 |
| A1006 | C004 | CLOSED | 2026-06-25 | 2 |
| A1007 | C004 | CLOSED | 2026-06-26 | 1 |
+------------+-------------+--------+------------+--------------+
*/
To embed this project on your website, copy the following code and paste it into your website's HTML: