Where clause and operators: in, between, like, not in, comparison operators, wildcard, order by, group by, distinct

WHERE filters rows to just the ones you want using comparison operators, IN, BETWEEN, LIKE (with % wildcards) and NOT IN, while ORDER BY sorts the result, DISTINCT removes duplicates, and GROUP BY collapses rows into per-category summaries.

12 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Do not give me everything

SELECT * FROM sales dumps every row. But Meera rarely wants everything, she wants 'groceries under 50', or 'items starting with S', or 'the list sorted by price'.

That selectivity is the WHERE clause and its companions. This is where SELECT stops being a data dump and becomes a question.

And you have met the idea before: WHERE is exactly Unit 2's Filter and ORDER BY is exactly its Sort, now typed instead of clicked. Here is the shop table again to keep in view.

Practical

The sales table (keep this in mind)

-- id | item  | category | price | qty
--  1 | Sugar | Grocery  |   45  | 20
--  2 | Tea   | Beverage |  120  | 15
--  3 | Rice  | Grocery  |   60  | 30
--  4 | Soap  | Personal |   35  | 25
--  5 | Milk  | Dairy    |   30  | 40

SELECT item, price FROM sales
WHERE category = 'Grocery' AND price < 50;
-- returns: Sugar 45

This example runs in Gri-Learn on the web, where you can edit it and see the output.

At a glance

WHERE operators

OperatorMeaningExample
=, >, <, >=, <=, <>Compareprice > 40
IN (a, b)Matches any in a listcategory IN ('Grocery','Dairy')
BETWEEN a AND bIn a range (inclusive)price BETWEEN 30 AND 60
LIKE 'S%'Pattern match (% and _)item LIKE 'S%'
NOT IN (a, b)Excludes a listcategory NOT IN ('Personal')

Theory

LIKE and the wildcards

LIKE matches text patterns using two wildcards:

  • `%` stands for any sequence of characters (including none).
  • `_` stands for exactly one character.

So item LIKE 'S%' matches Sugar and Soap (start with S). item LIKE '%a' matches anything ending in 'a'. item LIKE 'T__' matches 'Tea' (T + exactly two more).

This is how you search 'names starting with', 'containing', 'ending with', the pattern-matching workhorse of SQL. Getting % (many) vs _ (one) straight is a guaranteed mark.

Theory

Shaping the result: ORDER BY, DISTINCT, GROUP BY

Beyond filtering rows, three clauses shape the output:

  • ORDER BY sorts: ORDER BY price DESC (highest first; ASC is the default).
  • DISTINCT removes duplicate values: SELECT DISTINCT category FROM sales lists each category once.
  • GROUP BY collapses rows sharing a value into groups, used with aggregate functions for per-group summaries: SELECT category, SUM(qty) FROM sales GROUP BY category gives total quantity per category.

WHERE picks rows; GROUP BY bundles them; ORDER BY arranges them; DISTINCT dedupes them.

Quiz

Which items does WHERE item LIKE 'S%' match in the sales table?

  1. Sugar and Soap (names starting with S)
  2. Only Sugar (exact match)
  3. All items containing the letter S
  4. Tea and Rice
Show the answer

Sugar and Soap (names starting with S)

'S%' means 'S followed by any characters', so it matches every item name starting with S: Sugar and Soap. To match names containing S anywhere you would write '%S%'; for an exact single-letter-plus-two you would use _. The % (any sequence) vs _ (one character) distinction is the most-tested LIKE fact.

Think first

BETWEEN is inclusive

Meera runs SELECT item FROM sales WHERE price BETWEEN 30 AND 60. Looking at the table (prices 45, 120, 60, 35, 30), which items come back? Watch the boundaries carefully.

Show the answer

Sugar (45), Rice (60), Soap (35), Milk (30), four items. BETWEEN is inclusive, so both 30 and 60 are included (Milk at exactly 30 and Rice at exactly 60 both qualify). Only Tea (120) is excluded. Students often wrongly assume BETWEEN excludes the endpoints, it includes them, which is a classic exam catch.

Watch out

Where marks leak

LIKE wildcards: `%` = any number of characters, `_` = exactly one, do not swap them. BETWEEN is inclusive of both ends. Not-equal is written `<>` (or !=). Text values need quotes ('Grocery'), numbers do not. And GROUP BY groups rows for aggregates, it is not the same as ORDER BY (which only sorts). Precise operator use is where SQL marks are won or lost.

Theory

You clicked this in Unit 1

Feel the connection: WHERE is the Filter you ticked in Unit 2, ORDER BY is the Sort, GROUP BY is Consolidate, and DISTINCT is Remove Duplicates. Everything you did by mouse in Excel, you now do by precise typed commands that scale to millions of rows. That is the whole promise of databases over spreadsheets. Next lesson: combining conditions with AND, OR, EXISTS, and naming things with aliases.

Summary

Key takeaways

  • WHERE filters rows by a condition using =, >, <, <>, IN, BETWEEN, LIKE, NOT IN.
  • LIKE matches patterns: % = any sequence of characters, _ = exactly one character.
  • BETWEEN a AND b is INCLUSIVE of both endpoints.
  • ORDER BY sorts (ASC default, DESC for high-to-low); DISTINCT removes duplicate values.
  • GROUP BY collapses rows sharing a value into groups for per-group summaries.
  • Memory hook: WHERE = Excel Filter, ORDER BY = Sort, GROUP BY = Consolidate, now typed.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of SQL and Queries (Single Table only)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Where clause and operators: in, between, like, not in, comparison operators, wildcard, order by, group by, distinct · Data Processing and Analysis (DPA) · Gri-Learn