# Data Science Pipelines: EDA, Feature Scaling, OLAP Schemas & SQL Window Functions
Exam: GATE Data Science & Artificial Intelligence (DA) (GATE DA)
Subject: Data Science, Statistics & Data Warehousing
Canonical URL: https://gateandnet.com/exams/gate-da/data-science/eda-feature-engineering-olap-and-sql-analytics

> Preventing train-test data leakage, Z-score vs Min-Max vs Robust IQR scaling, MCAR/MAR/MNAR missingness, Star vs Snowflake OLAP cube operations, and SQL ROW_NUMBER / RANK / DENSE_RANK.

## 1. Feature Engineering & The Cardinal Sin of Data Leakage

> [!TRAP]
> **Train-Test Preprocessing Leakage:** Always compute $\mu, \sigma$, imputation medians, target encodings, and PCA eigenvectors on the **Training Split ONLY** (or inside each fold of $K$-Fold Cross-Validation), then apply `scaler.transform(X_test)`! Fitting a scaler or oversampler (SMOTE) on the full dataset *before* splitting leaks test distribution statistics and inflates validation metrics!

| Missing Data Mechanism | Definition | Safe Handling Strategy |
| :--- | :--- | :--- |
| **MCAR** *(Missing Completely at Random)* | Missingness independent of both observed & unobserved values | Listwise deletion is unbiased (loses power); median/mean imputation |
| **MAR** *(Missing at Random)* | Missingness depends only on *observed* covariates (e.g. age/role) | Multiple Imputation (MICE), k-NN / iterative model imputation |
| **MNAR** *(Missing Not at Random)* | Missingness depends on the *missing value itself* (e.g. high earners hiding income) | Add explicit `is_missing` indicator flag + domain modeling |

## 2. Data Warehousing: Star vs Snowflake Schema & OLAP Operations

- **Star Schema:** Central denormalized numeric **Fact Table** surrounded by 1-hop **Dimension Tables** (in 2NF/denormalized) $\implies$ **fewer `JOIN`s, faster analytical OLAP queries**!
- **Snowflake Schema:** Normalizes dimension hierarchies into 3NF sub-tables $\implies$ saves storage & avoids update anomalies, but slower multi-join reads.
- **OLAP Cube Operations:** **Roll-Up** (aggregate up hierarchy: City $\to$ Country), **Drill-Down** (finer granularity: Year $\to$ Quarter), **Slice** (fix $1$ dimension, e.g. `Year = 2026`), **Dice** (sub-cube range across $\ge 2$ dimensions), **Pivot** (rotate axes).

## 3. SQL Analytical Window Functions (`ROW_NUMBER` vs `RANK` vs `DENSE_RANK`)

Suppose salaries in a department are `[90, 80, 80, 70]` ordered `DESC`:

```sql
SELECT emp_name, salary,
       ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rn,
       RANK()       OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rnk,
       DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS drnk
FROM employees;
```

- `ROW_NUMBER()`: **`1, 2, 3, 4`** (unique sequential integers, breaks ties arbitrarily)
- `RANK()`: **`1, 2, 2, 4`** (ties share rank, **skips** `3`)
- `DENSE_RANK()`: **`1, 2, 2, 3`** (ties share rank, **never skips** — always use `DENSE_RANK() = N` for *"Nth highest distinct salary"*!)

## Solved Practice Questions & PYQs

### [GATE DA Data Science | MCQ]
A dataset feature has quartiles $Q_1 = 20$, $\text{Median} = 32$, and $Q_3 = 50$. Under **Tukey’s $1.5 \times \text{IQR}$ rule**, any observation strictly above which upper fence is flagged as an **outlier**?
**Correct Answer:** B
**Solution:** $\text{IQR} = Q_3 - Q_1 = 50 - 20 = 30$. Upper fence $= Q_3 + 1.5 \times \text{IQR} = 50 + 1.5(30) = 50 + 45 = \mathbf{95}$ (and lower fence $= 20 - 45 = -25$).

### [GATE DA SQL & Warehousing | MCQ]
For the ordered score column `[100, 95, 95, 90]`, what sequence of integers is returned by SQL `RANK() OVER (ORDER BY score DESC)` vs `DENSE_RANK() OVER (ORDER BY score DESC)`?
**Correct Answer:** A
**Solution:** Both assign rank `2` to the two tied `95` rows; `RANK()` skips the next rank (`4`), whereas `DENSE_RANK()` leaves zero gaps (`3`)!

### [GATE DA Data Leakage | MCQ]
Which step in a Data Science pipeline introduces **train-test data leakage** and produces overly optimistic validation scores?
**Correct Answer:** A
**Solution:** Calling `fit_transform` on `X` **before** splitting leaks test-set mean/variance (and outlier bounds) into training features! Always fit preprocessors inside `Pipeline` on training folds only.
