-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleaning_queries.sql
More file actions
58 lines (48 loc) · 1.54 KB
/
Copy pathcleaning_queries.sql
File metadata and controls
58 lines (48 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-- ============================================
-- DATA CLEANING PROJECT
-- ============================================
-- Crear tabla con datos sucios
CREATE TABLE clientes_raw (
id INT,
nombre VARCHAR(50),
email VARCHAR(100),
ciudad VARCHAR(50),
edad INT
);
-- Insertar datos con problemas
INSERT INTO clientes_raw VALUES
(1, 'Juan Perez', 'juan@mail.com', 'CDMX', 28),
(2, 'Ana Lopez', 'ana@mail.com', 'Guadalajara', NULL),
(3, 'Carlos Ruiz', 'carlos@mail.com', 'cdmx', 35),
(4, 'Maria Torres', 'maria@mail', 'Monterrey', 40), -- email inválido
(5, 'Luis Gomez', 'luis@mail.com', 'CDMX', 28),
(5, 'Luis Gomez', 'luis@mail.com', 'CDMX', 28), -- duplicado
(6, NULL, 'sin_nombre@mail.com', 'Puebla', 22);
-- ============================================
-- LIMPIEZA DE DATOS
-- ============================================
-- 1. Eliminar duplicados
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) AS rn
FROM clientes_raw
)
DELETE FROM cte WHERE rn > 1;
-- 2. Manejo de valores NULL (edad)
UPDATE clientes_raw
SET edad = (SELECT AVG(edad) FROM clientes_raw WHERE edad IS NOT NULL)
WHERE edad IS NULL;
-- 3. Normalizar ciudades (mayúsculas)
UPDATE clientes_raw
SET ciudad = UPPER(ciudad);
-- 4. Identificar emails inválidos
SELECT *
FROM clientes_raw
WHERE email NOT LIKE '%@%.%';
-- 5. Eliminar registros sin nombre
DELETE FROM clientes_raw
WHERE nombre IS NULL;
-- ============================================
-- RESULTADO FINAL
-- ============================================
SELECT * FROM clientes_raw;