Skip to content

Oracle DISTINCT Clause 💎

Mentor's Note: Your database might have 1,000 employees, but maybe they only live in 5 different cities. DISTINCT helps you see those unique values without the repetition. 💡


🌟 The Scenario: The City Census 📍

  • Goal: Find out which cities our company operates in.
  • Problem: If 100 people live in Surat, you don't want to see "Surat" 100 times.
  • Action: You use DISTINCT to get a clean, unique list.

💻 1. Syntax & Examples

SELECT DISTINCT column_name FROM table_name;

Example: Unique Job Titles

SELECT DISTINCT job_id FROM employees;

🏗️ Architect's Note: The Unique Sort 🛡️

When you run DISTINCT, Oracle internally performs a SORT UNIQUE or HASH UNIQUE operation. This means the database has to scan and compare every row to find duplicates. - Pro Tip: If you know a table only has unique values (like a Primary Key column), using DISTINCT is a waste of CPU power!


📈 Learning Path