In SQL querying, which clause should be used to return only one copy of each set of duplicate rows from a result set (i.e., remove duplicates in the projection)?

Difficulty: Easy

Correct Answer: SELECT DISTINCT

Explanation:


Introduction / Context:
When retrieving data, you may encounter duplicates due to joins, denormalization, or repeated values in a column set. SQL provides a standard way to collapse duplicates so that each unique combination of selected columns appears only once in the result set.



Given Data / Assumptions:

  • We are writing a SELECT query that projects columns potentially containing duplicate combinations.
  • We need the ANSI-standard clause that removes duplicates.
  • Portability across common RDBMSs is desired.


Concept / Approach:
The DISTINCT keyword is defined by the SQL standard to eliminate duplicate rows from the projection. Some vendors historically accepted UNIQUE as a synonym, but it is not universally portable. There is no standard DIFFERENT keyword for this purpose.



Step-by-Step Solution:

Identify need: collapse duplicate rows across the selected columns. Recall the standard syntax: SELECT DISTINCT col_list FROM table ... Prefer portable, standard SQL over vendor-specific synonyms. Choose SELECT DISTINCT.


Verification / Alternative check:
SQL references for major systems (PostgreSQL, SQL Server, MySQL, Oracle) document DISTINCT as the standard duplicate-elimination modifier in SELECT.



Why Other Options Are Wrong:

  • SELECT UNIQUE: works in some dialects (e.g., older Oracle), not standard everywhere.
  • SELECT DIFFERENT: not an SQL keyword.
  • All of the above: incorrect because one option is non-standard and one is invalid.
  • None: DISTINCT is the correct standard.


Common Pitfalls:
Overusing DISTINCT to mask poor joins; DISTINCT adds sorting/aggregation overhead. Consider proper join conditions and indexes before relying on DISTINCT for performance-sensitive queries.



Final Answer:
SELECT DISTINCT

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion