When you need to remove duplicate rows from the output of a SELECT query, which SQL keyword should you include to return only unique rows?

Difficulty: Easy

Correct Answer: DISTINCT

Explanation:


Introduction / Context:
Queries that project fewer columns than the primary key may return duplicate rows. SQL provides a direct way to collapse duplicates in the result set without additional grouping logic.



Given Data / Assumptions:

  • The query uses SELECT and may retrieve repeating values.
  • We only want unique combinations of the selected columns in the output.
  • We are not aggregating; we simply want to deduplicate.


Concept / Approach:
The DISTINCT keyword modifies SELECT to return only unique rows across the selected columns. Some systems accept SELECT DISTINCT ON (columns) with special semantics, but the general keyword is DISTINCT. UNIQUE is not the standard keyword for SELECT and may be reserved for constraints; ONLY, SINGLE, or NODUP are not standard.



Step-by-Step Solution:

Determine that duplicates may occur in the projection.Add DISTINCT immediately after SELECT, for example: SELECT DISTINCT city FROM customers;Execute and observe that duplicates collapse into one row each.


Verification / Alternative check:
Compare row counts with and without DISTINCT to confirm deduplication.



Why Other Options Are Wrong:

  • UNIQUE: Usually used in constraints, not as a SELECT modifier in standard SQL.
  • ONLY / SINGLE / NODUP: Not standard SQL keywords for deduplication.


Common Pitfalls:
Using DISTINCT to mask poor join conditions. Always ensure joins are correct; DISTINCT should not be a band-aid for cartesian products.



Final Answer:
DISTINCT

Discussion & Comments

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