Data Cleaning, Preparation & Querying with Excel & SQL
An authoritative reference manual on data wrangling, missing value treatment, deduplication, string transformations, advanced Excel functions, and relational SQL queries.
Raw real-world data is notoriously messy, containing missing entries, duplicates, formatting inconsistencies, and structural errors. Data cleaning and preparation consume up to 80% of a data analyst's workflow. This chapter explores data wrangling techniques in Microsoft Excel and SQL.
1.1. 1. Data Cleaning & Preparation Tools in Excel
Definition: Data cleaning is the process of detecting and correcting corrupt, inaccurate, or incomplete records from a record set, table, or database.
Core Working Principles:
- Handling Missing Data: Identifying nulls, imputation using mean/median/mode, or removing incomplete rows based on context.
- Deduplication & String Transformations: Removing duplicate records, trimming whitespace (
TRIM), standardizing casing (PROPER,UPPER), and splitting text columns. - Lookup & Integration Formulas: Combining datasets using
XLOOKUP,VLOOKUP, andINDEX-MATCHfor dynamic data retrieval.
Raw Data: Like freshly harvested vegetables straight from the farm—covered in dirt, mixed with weeds, and containing spoiled items.
Data Cleaning: Washing off the mud (removing nulls/whitespace), discarding rotten produce (deduplication), and chopping items into neat, uniform pieces ready for cooking (standardizing formats)!
5-Marks Exam Scoring Points
- Data cleaning converts raw, dirty datasets into reliable analysis-ready tables.
- Excel wrangling uses functions like XLOOKUP, INDEX-MATCH, TRIM, and Remove Duplicates.
- Imputing missing values requires assessing data bias and domain context.
1.2. 2. SQL Data Transformation & Relational Querying
Definition: SQL (Structured Query Language) is the industry standard domain-specific language for querying and transforming data stored in relational database management systems.
Core Working Principles:
- Core Filtering & Aggregation:
SELECT,WHERE,GROUP BY,HAVING,ORDER BY. - Relational JOIN Operations:
- •
INNER JOIN: Returns matching records in both tables. - •
LEFT JOIN: Returns all records from the left table and matched records from the right table. - •
FULL OUTER JOIN: Returns all records when there is a match in left or right table. - SQL Window Functions:
ROW_NUMBER(),RANK(),DENSE_RANK()overPARTITION BYclauses for advanced ranking and windowed calculations.
graph TD
Raw[Raw CSV / Database Tables] -->|1. SELECT & JOIN| Filtered[Filtered Relational Join]
Filtered -->|2. WHERE & GROUP BY| Aggregated[Aggregated Summary Metrics]
Aggregated -->|3. Window Functions PARTITION BY| Final[Clean Analytical Dataset]
| Feature | Microsoft Excel | SQL Databases 🏆 |
|---|---|---|
| Data Capacity Limit | 1,048,576 rows per worksheet | Billions of rows / Petabytes |
| Query Execution Speed | Slow for complex multi-table joins | Ultra-fast indexed query optimization |
| Automation & Scripting | Macros / Power Query | Automated scheduled views & procedures |
-- Advanced SQL Data Wrangling Query
SELECT
c.customer_id,
c.customer_name,
SUM(o.order_amount) AS total_spent,
ROW_NUMBER() OVER (PARTITION BY c.region ORDER BY SUM(o.order_amount) DESC) AS regional_rank
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_date >= '2026-01-01'
GROUP BY c.customer_id, c.customer_name, c.region
HAVING SUM(o.order_amount) > 1000;
5-Marks Exam Scoring Points
- SQL handles massive multi-million row datasets that exceed Excel limits.
- LEFT JOIN retains all left table rows while attaching matching right table attributes.
- Window functions (ROW_NUMBER, RANK) compute analytical calculations across row partitions.
✍️ Chapter 1 Reference Exercises & Review Problems
- **`WHERE` Clause:** Filters individual rows before any grouping or aggregation takes place.
- **`HAVING` Clause:** Filters aggregated group summary results after the `GROUP BY` clause has been executed.
- **Example:** `WHERE status = 'Active'` filters active customer rows first; `HAVING SUM(order_amount) > 1000` filters only customer groups whose aggregate total spend exceeds $1,000!
- **VLOOKUP:** Looks up a key in the first column of a table array and returns a value to the right. Fragile because hardcoded column index numbers break when columns are inserted.
- **INDEX-MATCH:** Combines `INDEX` (returns value at row/col coordinate) and `MATCH` (finds position of key). Flexible because it can look up values to the left and tolerates column insertions.
- **XLOOKUP (Modern Standard):** Replaces VLOOKUP/INDEX-MATCH with simpler syntax: `XLOOKUP(lookup_value, lookup_array, return_array)`. Supports left lookups, exact matches by default, and custom missing value fallbacks.