100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
SQL

DISTINCT and Basic Operators

Remove duplicate rows with DISTINCT and use comparison, logical, and pattern-matching operators in queries.

SQL BasicsBeginner10 min readJul 8, 2026
Analogies

Introduction

Real-world tables often contain duplicate values in a column, such as multiple employees in the same department. DISTINCT removes duplicate rows from a result set. SQL also provides a set of basic operators — comparison, logical, and pattern-matching — that let you express precise conditions in queries.

🏏

Cricket analogy: A team roster often lists the same bowling style ('right-arm fast') for multiple players; DISTINCT collapses that list to unique styles, while comparison and pattern-matching operators let you filter for players whose name starts with 'S'.

Syntax

sql
SELECT DISTINCT column1, column2, ...
FROM table_name;

-- Common operators
-- Comparison: =, <>, !=, <, >, <=, >=
-- Logical: AND, OR, NOT
-- Range: BETWEEN low AND high
-- Set membership: IN (value1, value2, ...)
-- Pattern match: LIKE 'pattern%'
-- Null check: IS NULL / IS NOT NULL

Explanation

DISTINCT applies to the entire row of selected columns: two rows are considered duplicates only if all selected columns match exactly. IN checks whether a value matches any item in a list, acting as shorthand for multiple OR comparisons. BETWEEN tests whether a value falls within an inclusive range. LIKE performs pattern matching using wildcards: % matches zero or more characters, and _ matches exactly one character.

🏏

Cricket analogy: DISTINCT on (team, ground) only collapses rows where both match exactly; IN('India','Australia') shortcuts multiple OR checks, BETWEEN 2020 AND 2024 filters match years inclusively, and LIKE 'Kohl_' with an underscore matches a five-letter surname exactly.

Example

sql
-- employees table: id, first_name, last_name, department, salary, hire_date

-- Unique department names
SELECT DISTINCT department
FROM employees;

-- Employees in Engineering or Sales whose last name starts with 'S'
SELECT first_name, last_name, department
FROM employees
WHERE department IN ('Engineering', 'Sales')
  AND last_name LIKE 'S%';

Output

The first query returns each distinct department name once, e.g. 'Engineering', 'Sales', 'Marketing', with no repeats even if many employees share a department. The second query returns only employees whose department is Engineering or Sales and whose last name begins with 'S', such as 'Maria | Silva | Sales'.

🏏

Cricket analogy: A DISTINCT query on team returns each franchise once, e.g., 'Mumbai Indians', 'Chennai Super Kings', with no repeats even though many players belong to each; a filtered query returns only bowlers on 'Mumbai' or 'Chennai' whose surname starts with 'S', like 'Ravi | Sharma | Mumbai'.

Key Takeaways

  • DISTINCT removes duplicate rows based on the full combination of selected columns.
  • IN is a concise alternative to chaining multiple OR conditions for the same column.
  • LIKE uses % for zero-or-more characters and _ for exactly one character.
  • BETWEEN is inclusive of both boundary values.

Practice what you learned

Was this page helpful?

Topics covered

#SQL#DatabaseSQLStudyNotes#Database#DISTINCTAndBasicOperators#DISTINCT#Operators#Syntax#Explanation#StudyNotes#SkillVeris