SQL query semantics: Which command correctly returns unique subject values (SUB) from a table named BOOK, eliminating duplicates in the result set?

Difficulty: Easy

Correct Answer: SELECT DISTINCT SUB FROM BOOK

Explanation:


Introduction / Context:
SQL provides the DISTINCT keyword to remove duplicate rows from a projection. When you need a list of unique values from a column, DISTINCT is the standard, portable approach across vendors.


Given Data / Assumptions:

  • Column name is SUB and table name is BOOK.
  • We require uniqueness in the output.
  • We assume conventional SQL syntax.


Concept / Approach:
SELECT DISTINCT column_list returns each unique combination of the listed columns exactly once. SELECT column without DISTINCT returns all rows (including duplicates). SELECT ALL is the default behavior (it does not de-duplicate). Therefore, to obtain unique subject values, we must use DISTINCT with the specific column.


Step-by-Step Solution:
Identify the target: unique values of SUB.Choose the SQL construct for uniqueness: DISTINCT.Write the minimal projection: SELECT DISTINCT SUB FROM BOOK.


Verification / Alternative check:
Running SELECT SUB FROM BOOK ORDER BY SUB would still show duplicates; wrapping with DISTINCT removes them. Some systems also support GROUP BY SUB to get unique SUB values, but DISTINCT is simpler and clearer.


Why Other Options Are Wrong:

  • SELECT ALL FROM BOOK: Returns all columns and all rows; includes duplicates.
  • SELECT SUB FROM BOOK: Returns all SUB values with duplicates.
  • All of the above: Incorrect because only one option eliminates duplicates.


Common Pitfalls:
Using DISTINCT with multiple columns inadvertently groups by the entire combination; ensure only necessary columns are projected when deduplication is intended.


Final Answer:
SELECT DISTINCT SUB FROM BOOK

Discussion & Comments

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