Purpose of SELECT DISTINCT: Choose the correct interpretation.\n“SELECT DISTINCT is used when a user wishes to see duplicate rows in a query result.”

Difficulty: Easy

Correct Answer: Incorrect

Explanation:


Introduction / Context:
SELECT DISTINCT is commonly used to eliminate duplicate rows in result sets. Misunderstanding this leads to the opposite of the desired output. This question checks whether you know the exact role of DISTINCT.



Given Data / Assumptions:

  • Standard SQL behavior is assumed.
  • Duplicates are determined across the entire SELECT list.
  • NULLs compare as equal for DISTINCT evaluation in most SQL engines for the purpose of deduplication.


Concept / Approach:
SELECT DISTINCT applies a set operation over the projected columns, removing duplicate rows and returning only unique combinations. If the goal is to see duplicates, one would use GROUP BY with HAVING COUNT() > 1 or window functions (for example, COUNT() OVER(PARTITION BY ...)) to identify duplicates, not DISTINCT. Therefore, the statement claiming DISTINCT shows duplicates is the opposite of reality.



Step-by-Step Solution:

To remove duplicates: SELECT DISTINCT col1, col2 FROM T.To find duplicates: SELECT col1, COUNT() FROM T GROUP BY col1 HAVING COUNT() > 1.To mark duplicates without filtering: SELECT col1, COUNT(*) OVER(PARTITION BY col1) AS dup_cnt FROM T.Conclusion: DISTINCT removes, not reveals, duplicates.


Verification / Alternative check:
Run sample data with repeated values; DISTINCT returns unique rows, while GROUP BY/HAVING reveals duplicates.



Why Other Options Are Wrong:

  • “Correct” is the inverse of the truth.
  • GROUP BY or ORDER BY does not change the fundamental meaning of DISTINCT.
  • NULL semantics affect grouping/uniqueness but do not invert DISTINCT’s purpose.


Common Pitfalls:
Using DISTINCT to “fix” unintended duplicates created by joins instead of correcting the join logic; assuming DISTINCT is per-column.



Final Answer:
Incorrect

More Questions from Introduction to SQL

Discussion & Comments

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