Sitemap

SQL Fundamentals: DISTINCT & Advanced Filtering

4 min readMay 8, 2026

--

Learn how to remove duplicates and apply precise filtering using DISTINCT, WHERE, and HAVING — essential for clean and reliable data analysis.

Series Note:

This blog is part of the SQL Fundamentals for Data Analysts series — 4 by Shahnaz Insights.

  • In Part 1, we retrieved and filtered data using SELECT, FROM, WHERE
  • In Part 2, we sorted data using ORDER BY, LIMIT
  • In Part 3, we created insights using aggregation and GROUP BY

👉 Now in Part 4, we focus on data cleaning and refinement

Press enter or click to view image in full size

Understanding:

Raw data is often messy.

It may contain:

  • Duplicate values
  • Repeated categories
  • Irrelevant records

Stakeholders don’t want duplicates —
they want clear and accurate insights

👉 To achieve this:

  • DISTINCT → removes duplicates
  • WHERE → filters raw data
  • HAVING → filters grouped insights

What You’ll Learn:

  • Remove duplicate values using DISTINCT
  • Identify unique categories and combinations
  • Apply WHERE and HAVING correctly
  • Clean data before analysis

📊 Sample Dataset: employees:

This dataset uses appropriate data types to store different kinds of information:

  • INT → numeric values (id, salary, sales)
  • VARCHAR(25) → text values (name, department, up to 25 characters)

By using the right data types, the dataset remains well-organized, supports filtering and grouping, and ensures reliable results when applying operations like HAVING, DISTINCT, and aggregations.

We insert employee records across IT, HR, and Finance.

This dataset simulates real-world scenarios with different salaries and sales values, making it useful for applying HAVING, DISTINCT, and aggregation.

Result:

The table shows all records in raw form.
Using aggregation and GROUP BY, we can turn this data into meaningful insights.

Press enter or click to view image in full size

🔍 Examples with Observation & Analyst Interpretation:

✅ Step 1: DISTINCT — Removing Duplicates:

Press enter or click to view image in full size

Result:

What you’ll observe:
Duplicate department values are removed, leaving only unique categories

Analyst Interpretation:
Helps understand the structure of data before analysis

✅ Step 2: DISTINCT with Multiple Columns:

Result:

What you’ll observe:
Only unique combinations of department and salary are returned

Get Shahnaz Bano’s stories in your inbox

Join Medium for free to get updates from this writer.

Analyst Interpretation:
Useful for identifying unique patterns in data

✅Step 3: DISTINCT vs GROUP BY:

Result:

Result:

What you’ll observe:
Both return unique departments, but their purpose is different

Analyst Interpretation:

  • DISTINCT → removes duplicates
  • GROUP BY → prepares data for aggregation

✅Step 4: WHERE vs HAVING:

Result:

What you’ll observe:
WHERE filters rows before grouping
HAVING filters results after grouping

Analyst Interpretation:
WHERE → controls input data
HAVING → controls final output

✅Step 5: Combining DISTINCT with Filtering:

Result:

What you’ll observe:
Only unique departments meeting the salary condition are returned

Analyst Interpretation:
Removes duplicates while focusing only on relevant data

❌ Common Mistakes (Must Avoid):

  • Using DISTINCT instead of GROUP BY for calculations
  • Confusing DISTINCT with GROUP BY
  • Using HAVING without GROUP BY
  • Filtering with HAVING instead of WHERE

Analyst Insight:

Clean data = reliable insights

Before analysis:
Remove duplicates
Filter unnecessary data

After that:
👉 Apply aggregation (Part 3)

Conclusion:

Data becomes analysis-ready when you:

  • Remove duplicates (DISTINCT)
  • Filter correctly (WHERE)
  • Refine results (HAVING)

This ensures your analysis is accurate and trustworthy

➡️ What’s Next — Series 5 (Advanced SQL):

Now we move into real-world data relationships:

✅ Part 1: JOINs & Data Relationships
Learn how to combine multiple tables for deeper insights

Final Insight:

Correct data leads to correct decisions.

DISTINCT doesn’t create insights —
it ensures the insights you create are clean and accurate.

Follow for More:

Follow to master real-world data analysis step by step through the Shahnaz Insights series

And if you found this valuable, a clap 👏 helps this reach more learners 🚀

--

--

Shahnaz Bano
Shahnaz Bano

Written by Shahnaz Bano

Freelance Data Analyst - Elevating my coding from DS Vidya (https://datasciencevidya.in)