Oracle DISTINCT
Palavras-chave:
Publicado em: 05/08/2025Understanding and Using Oracle DISTINCT
The DISTINCT
keyword in Oracle SQL is used to retrieve unique values from one or more columns in a table. This article provides a comprehensive guide to understanding and using the DISTINCT
keyword, including its syntax, usage scenarios, and performance considerations.
Fundamental Concepts / Prerequisites
Before diving into the DISTINCT
keyword, it's helpful to have a basic understanding of SQL queries, tables, and columns. Familiarity with the SELECT
statement is essential. The following concepts should be understood:
- SQL syntax
- Basic querying using SELECT
- Data types in Oracle SQL
Core Implementation/Solution
The DISTINCT
keyword is used within a SELECT
statement to eliminate duplicate rows from the result set. Here's a basic example:
-- Sample table: employees
-- id | name | department
-- ----+---------+------------
-- 1 | John | Sales
-- 2 | Jane | Marketing
-- 3 | John | Sales
-- 4 | David | IT
-- 5 | Jane | Marketing
SELECT DISTINCT department
FROM employees;
-- Result:
-- department
-- ------------
-- Sales
-- Marketing
-- IT
SELECT DISTINCT name, department
FROM employees;
-- Result:
-- name | department
-- ---------+------------
-- John | Sales
-- Jane | Marketing
-- David | IT
Code Explanation
The first SELECT DISTINCT department
query retrieves all unique department names from the employees
table. Duplicate department names are removed, resulting in a list of only distinct department values.
The second SELECT DISTINCT name, department
query retrieves unique combinations of name and department. Two employees can have the same name, but if they are in different departments, both rows will be included in the result set. In the example above, both John and Jane exist in more than one row, but DISTINCT will only return a row for each unique *combination* of name and department.
Analysis
Complexity Analysis
The time complexity of a SELECT DISTINCT
query depends on the underlying database implementation and the size of the data. In general, it requires sorting and comparing rows to identify duplicates.
- Time Complexity: In the worst-case scenario, where all rows are unique, the time complexity is O(n log n) due to the sorting process. In cases with many duplicates, the complexity can be lower.
- Space Complexity: The space complexity also depends on the implementation. It typically requires storing the unique values temporarily, so it can be O(n) in the worst case, where n is the number of rows.
Alternative Approaches
An alternative approach to achieving the same result as DISTINCT
is using the GROUP BY
clause. When grouping by a set of columns, each unique combination of values in those columns will appear only once in the result set.
SELECT department
FROM employees
GROUP BY department;
SELECT name, department
FROM employees
GROUP BY name, department;
While GROUP BY
can achieve similar results, it's often used for aggregation (e.g., counting occurrences of each department). DISTINCT
is generally considered more concise and readable when the primary goal is simply to remove duplicates without performing any aggregation.
Conclusion
The DISTINCT
keyword in Oracle SQL provides a straightforward way to retrieve unique values from a table. Understanding its syntax, usage, and performance implications is crucial for writing efficient and accurate SQL queries. While alternative approaches like GROUP BY
exist, DISTINCT
is often the preferred choice when the goal is simply to eliminate duplicate rows from the result set.