Use of DISTINCT and TOP Clauses in SQL

πŸ’‘ Concept Name

DISTINCT removes duplicate rows from the result set, while TOP limits the number of rows returned by a query.

πŸ“˜ Quick Intro

DISTINCT ensures uniqueness in selected columns, while TOP fetches only the first 'n' rows based on order. Together, they improve readability, performance, and control in SQL results.

🧠 Analogy / Short Story

Imagine you’re sorting a stack of business cards. DISTINCT helps remove all the duplicate cards so you only keep one from each person. TOP is like picking only the first 5 cards from the sorted stackβ€”giving you just a quick sample of the collection.

πŸ”§ Technical Explanation

  • DISTINCT filters out duplicate rows in the result set.
  • TOP(n) limits the number of rows returned, usually combined with ORDER BY.
  • DISTINCT applies to all selected columnsβ€”if all are the same, it's considered duplicate.
  • TOP can be used with PERCENT and WITH TIES for more flexibility.
  • Both clauses help optimize performance when only specific data is needed.

🎯 Purpose & Use Case

  • βœ… Eliminate duplicate entries (e.g., unique customer cities).
  • βœ… Limit output for previews or dashboards.
  • βœ… Improve query efficiency by reducing rows returned.
  • βœ… Combine with ORDER BY to get top-ranking values.

πŸ’» Real Code Example

-- Remove duplicate cities
SELECT DISTINCT City FROM Customers;

-- Get top 5 highest-paid employees
SELECT TOP 5 FirstName, Salary
FROM Employees
ORDER BY Salary DESC;

-- Get top 10% most expensive products
SELECT TOP 10 PERCENT ProductName, Price
FROM Products
ORDER BY Price DESC;

❓ Interview Q&A

Q1: What does the DISTINCT clause do?
A: It removes duplicate rows from the result set based on selected columns.

Q2: Can DISTINCT be applied to multiple columns?
A: Yes, it applies to the combination of all selected columns.

Q3: What is the purpose of the TOP clause?
A: To limit the number of rows returned in a query.

Q4: How does TOP PERCENT work?
A: It returns the top x percent of rows based on the ordering clause.

Q5: What is TOP WITH TIES used for?
A: It includes additional rows that tie for the last position based on ORDER BY.

Q6: How does DISTINCT affect performance?
A: It adds overhead due to sorting or hashing but can reduce the final dataset size.

Q7: Can we use DISTINCT and TOP together?
A: Yes, but the order of clauses and intent must be clear to avoid confusion.

Q8: Is TOP standard SQL?
A: No, it's T-SQL specific. Standard SQL uses LIMIT or FETCH FIRST.

Q9: What happens if TOP is used without ORDER BY?
A: The results are nondeterministicβ€”any random top rows could be returned.

Q10: Does DISTINCT sort the data automatically?
A: Not alwaysβ€”it filters duplicates but does not sort unless combined with ORDER BY.

πŸ“ MCQs

Q1. What is the main function of DISTINCT in SQL?

  • Remove nulls
  • Remove duplicate rows
  • Sort data
  • Limit results

Q2. What does TOP 5 mean in SQL?

  • Return last 5 rows
  • Return first 5 rows
  • Return 5 columns
  • Return 5 duplicates

Q3. Can DISTINCT be used with multiple columns?

  • No
  • Only one column
  • Yes
  • Only with ORDER BY

Q4. Which keyword limits result set size in SQL Server?

  • LIMIT
  • TOP
  • DISTINCT
  • ROWNUM

Q5. What is needed for TOP clause to be meaningful?

  • GROUP BY
  • JOIN
  • HAVING
  • ORDER BY

Q6. Which clause helps fetch unique cities?

  • UNION
  • TOP
  • DISTINCT
  • SELECT ALL

Q7. What does TOP 10 PERCENT return?

  • Top 10 rows
  • Top 10% of ordered rows
  • Every 10th row
  • 10 percent of columns

Q8. Can TOP and ORDER BY be used together?

  • No
  • Yes
  • Only in MySQL
  • Only with subquery

Q9. What clause ensures uniqueness?

  • WHERE
  • TOP
  • DISTINCT
  • HAVING

Q10. Is DISTINCT a performance booster always?

  • Yes, always
  • No, it may add overhead
  • Only in views
  • Only with joins

πŸ’‘ Bonus Insight

Use TOP carefully when analyzing only a subset of large datasetsβ€”without an ORDER BY, the results may not be predictable. For cross-database compatibility, prefer using LIMIT in MySQL or FETCH FIRST in ANSI SQL.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

Learn More on SQL Topics

SQL Joins Explained: INNER, LEFT, RIGHT, FULL πŸ‘‰ Explained
SQL Subqueries and Nested Queries Explained πŸ‘‰ Explained
SQL Aggregate Functions: COUNT, SUM, AVG, MIN, MAX πŸ‘‰ Explained
Understanding SQL GROUP BY and ORDER BY πŸ‘‰ Explained
Difference Between DELETE, TRUNCATE and DROP πŸ‘‰ Explained
SQL Views: Use Cases and Performance Tips πŸ‘‰ Explained
Common Table Expressions (CTE) in SQL πŸ‘‰ Explained
SQL Window Functions and OVER Clause πŸ‘‰ Explained
SQL Indexes: Clustered vs Non-Clustered πŸ‘‰ Explained
SQL Normalization and Denormalization πŸ‘‰ Explained
SQL Constraints: NOT NULL, UNIQUE, CHECK, DEFAULT πŸ‘‰ Explained
SQL Injection and How to Prevent It πŸ‘‰ Explained
Primary Key vs Foreign Key in SQL πŸ‘‰ Explained
Data Types in SQL: INT, VARCHAR, DATE and more πŸ‘‰ Explained
How to Use CASE Statement in SQL πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup