Skip to content
Computer ScienceCBSE Class 12 · Structured Query Language (SQL)

LIKE Patterns, NULL Handling and ORDER BY in SQL (Class 12 Computer Science)

Updated 17 October 2026 · Fact-checked

LIKE matches text against a pattern using % (any number of characters, including none) and _ (exactly one character). NULL, a missing value, must be tested with IS NULL or IS NOT NULL, never with =. ORDER BY sorts the result in ascending order by default, or descending with DESC.

Understand LIKE, IS NULL and ORDER BY

The examples use the EMPLOYEE table of the chapter (EmpID, Name, DeptID, Salary, City, DOJ), as in the previous topic.

LIKE and the two wildcards.

  • % stands for any number of characters (zero or more).
  • _ stands for exactly one character.
SELECT Name FROM EMPLOYEE WHERE Name LIKE 'K%';        -- starts with K
SELECT Name FROM EMPLOYEE WHERE City LIKE '%ai';        -- city ends with ai
SELECT Name FROM EMPLOYEE WHERE Name LIKE '_a%';        -- second letter is a
  • 'K%' returns Kiran Shah.
  • '%ai' on City returns the employees of Mumbai and Chennai (Ravi Kumar, Meena Iyer, Farah Khan).
  • '_a%' returns Ravi Kumar and Farah Khan.
  • NOT LIKE reverses the match. In MySQL the comparison is not case-sensitive with the usual setting.

NULL. NULL means the value is missing or unknown. It is not zero and not an empty string. Any comparison with NULL (= NULL, <> NULL, > 5) is not true, so use:

SELECT Name FROM EMPLOYEE WHERE Salary IS NULL;
SELECT Name FROM EMPLOYEE WHERE Salary IS NOT NULL;

Salary = NULL returns no rows. The first query returns Kiran Shah. To include the NULL row in a "not equal" test write WHERE Salary <> 45000 OR Salary IS NULL.

ORDER BY. Sorts the rows of the result.

SELECT Name, Salary FROM EMPLOYEE ORDER BY Salary DESC;
SELECT Name, DeptID, Salary FROM EMPLOYEE ORDER BY DeptID, Salary DESC;
  • Ascending (ASC) is the default; DESC is descending.
  • With several columns, rows are sorted by the first column, and ties are sorted by the next.
  • Text sorts alphabetically, dates by time. NULL values come first in ascending order and last in descending order in MySQL.
  • ORDER BY comes after WHERE and is the last clause.

Output of the second query:

NameDeptIDSalary
Meena IyerD161000
Asha RaoD152000
Ravi KumarD238000
Kiran ShahD2NULL
Joy DasD345000
Farah KhanD345000

In D3 the salaries tie at 45000, so the two rows stay in their table order.

LIKE, IS NULL and ORDER BY: key facts

Wildcards
  • % : zero or more characters
  • _ : exactly one character
  • LIKE pattern in single quotes
  • NOT LIKE reverses
Name LIKE 'K%': starts with K.
Testing NULL
  • IS NULL
  • IS NOT NULL
  • = NULL gives no rows
  • NULL is not zero or empty
Comparison with NULL is never true.
ORDER BY
ASClow to high, the default
DESChigh to low
Several columnsfirst column first, ties by the next
Written after WHERE
NULL comes first in ascending order.
Clause order
  • SELECT
  • FROM
  • WHERE
  • GROUP BY
  • HAVING
  • ORDER BY
ORDER BY is last.

How to answer LIKE, IS NULL and ORDER BY questions

For LIKE questions, read the pattern character by character; for ORDER BY questions, sort first and then cut.

  1. 1Translate the pattern: letters must match exactly; % takes any run (even none); _ takes one character.
  2. 2Apply to each value; mark the matching rows.
  3. 3Handle NULL: IS NULL picks the missing rows; every other comparison leaves them out.
  4. 4Apply WHERE first, then sort the surviving rows with ORDER BY (first key, then ties by the second key).
  5. 5Remember DESC applies to the column it follows only.
  6. 6Write the output in the sorted order with headings.

LIKE, IS NULL and ORDER BY in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Names beginning with a letter

Suppose the notice board needs the list of employees whose name starts with K and cities that end with "ai".

Write the two conditions and the matches.

Try it first, then show the solutionSolution
  1. Name LIKE 'K%' matches Kiran Shah.
  2. City LIKE '%ai' matches Mumbai (Ravi Kumar, Farah Khan) and Chennai (Meena Iyer).

Answer: Kiran Shah; Ravi Kumar, Meena Iyer and Farah Khan.

Memory hook: Percent is a stretchy ribbon; underscore is a single bead.

Highest salaries first

Suppose a manager wants to see who earns the most, with the unpaid-yet entry at the bottom.

Write the query and the first row.

Try it first, then show the solutionSolution
  1. ORDER BY Salary DESC puts the largest salary first and the NULL last.
  2. The first row is Meena Iyer with 61000.

Answer: SELECT Name, Salary FROM EMPLOYEE ORDER BY Salary DESC; first row: Meena Iyer 61000.

Memory hook: DESC means down the ladder from the top.

Tricks to remember LIKE, IS NULL and ORDER BY

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • % many, _ one

    Percent can take many, underscore takes exactly one.

    Use it when: Reading patterns.

  • IS for NULL

    NULL is never "equal"; it only "is".

    Use it when: NULL tests.

  • ASC is the default

    If no word is written, it is ascending.

    Use it when: ORDER BY questions.

  • First key first

    Sort by the first column, and only break ties with the next.

    Use it when: Multi-column sort.

LIKE, IS NULL and ORDER BY: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Wildcard · 1 mark · MCQBoard level

Which wildcard character in LIKE matches any number of characters?
(a) _ (b) % (c) * (d) ?

Try it first, then show the solutionSolution
  1. The underscore matches exactly one character.

Answer: (b) %

Example 2: Two LIKE queries · 2 marks · Short answerBoard level

Write queries on EMPLOYEE to display (i) the names of employees whose name starts with F, (ii) the names of employees whose city ends with ai.

Try it first, then show the solutionSolution
  1. (i) SELECT Name FROM EMPLOYEE WHERE Name LIKE 'F%'; gives Farah Khan. (1 mark)
  2. (ii) SELECT Name FROM EMPLOYEE WHERE City LIKE '%ai'; gives Ravi Kumar, Meena Iyer, Farah Khan. (1 mark)

Answer: (i) Farah Khan (ii) Ravi Kumar, Meena Iyer, Farah Khan

Example 3: Patterns and outputs · 3 marks · Short answerHarder board level

Write the output of (i) SELECT Name FROM EMPLOYEE WHERE Name LIKE '%an'; (ii) SELECT Name, City FROM EMPLOYEE WHERE City LIKE '_u%'; (iii) SELECT Name FROM EMPLOYEE WHERE Name LIKE '_____ %';

Try it first, then show the solutionSolution
  1. (i) Names ending with an: Farah Khan. (1 mark)
  2. (ii) City with u as the second letter: Mumbai and Pune. Rows: Ravi Kumar Mumbai; Kiran Shah Pune; Farah Khan Mumbai. (1 mark)
  3. (iii) Five characters then a space: Meena Iyer, Kiran Shah, Farah Khan (Asha has only four). (1 mark)

Answer: (i) Farah Khan (ii) Ravi Kumar Mumbai; Kiran Shah Pune; Farah Khan Mumbai (iii) Meena Iyer; Kiran Shah; Farah Khan

Example 4: NULL tests · 3 marks · Short answerHarder board level

(i) What does SELECT Name FROM EMPLOYEE WHERE Salary = NULL; return? (ii) Write the correct query for the employee with no salary. (iii) Write a query to list every employee whose salary is not 45000, including those with no salary.

Try it first, then show the solutionSolution
  1. (i) No rows, because a comparison with NULL is never true. (1 mark)
  2. (ii) SELECT Name FROM EMPLOYEE WHERE Salary IS NULL; returns Kiran Shah. (1 mark)
  3. (iii) SELECT Name FROM EMPLOYEE WHERE Salary <> 45000 OR Salary IS NULL; returns Asha Rao, Ravi Kumar, Meena Iyer, Kiran Shah. (1 mark)

Answer: (i) empty result (ii) Kiran Shah (iii) Asha Rao, Ravi Kumar, Meena Iyer, Kiran Shah

Example 5: Case: the salary register · 4 marks · Case-basedHOTS / competency

Suppose a clerk prepares reports from EMPLOYEE.
(i) Write the output of SELECT Name, City FROM EMPLOYEE ORDER BY City, Name; (2 marks)
(ii) Which employee is first in SELECT Name, DOJ FROM EMPLOYEE ORDER BY DOJ DESC; and why? (1 mark)
(iii) Write a query to list the names of employees earning more than 40000 in alphabetical order. (1 mark)

Try it first, then show the solutionSolution
  1. (i) Sorted by City, ties by Name: Meena Iyer Chennai; Asha Rao Delhi; Joy Das Delhi; Farah Khan Mumbai; Ravi Kumar Mumbai; Kiran Shah Pune.
  2. (ii) Kiran Shah (DOJ 2023-02-10), because the latest date comes first in descending order.
  3. (iii) SELECT Name FROM EMPLOYEE WHERE Salary > 40000 ORDER BY Name; gives Asha Rao, Farah Khan, Joy Das, Meena Iyer.

Answer: (i) Meena Iyer Chennai; Asha Rao Delhi; Joy Das Delhi; Farah Khan Mumbai; Ravi Kumar Mumbai; Kiran Shah Pune (ii) Kiran Shah (iii) Asha Rao, Farah Khan, Joy Das, Meena Iyer

Where students lose marks in LIKE, IS NULL and ORDER BY

The slips examiners see most often, and what to write instead.

  • Mistake: Using = NULL

    A comparison with NULL is never true.

    Wrong working

    WHERE Salary = NULL

    Right working

    WHERE Salary IS NULL

    Do this: Say IS NULL aloud.

    Usually costs: 1 mark

  • Mistake: Confusing % and _

    % takes any number of characters; _ takes one.

    Wrong working

    LIKE 'K_' to find Kiran Shah

    Right working

    LIKE 'K%'

    Do this: Count the characters allowed.

    Usually costs: 1 mark

  • Mistake: Placing ORDER BY before WHERE

    ORDER BY is the last clause.

    Wrong working

    SELECT Name FROM EMPLOYEE ORDER BY Name WHERE Salary > 40000;

    Right working

    SELECT Name FROM EMPLOYEE WHERE Salary > 40000 ORDER BY Name;

    Do this: Follow the clause order SELECT, FROM, WHERE, ORDER BY.

    Usually costs: 1 mark

Exam tips

  • Underline the wildcard characters in the pattern and count them before checking each name.
  • Write IS NULL, never = NULL, and explain why in "differentiate" questions.
  • State "ascending by default" in ORDER BY answers.
  • For multi-column ORDER BY, say which column is the primary sort.
  • Show tie rows in table order unless the question says otherwise.

Practice questions on LIKE, IS NULL and ORDER BY

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    The default order of ORDER BY is:
    (a) descending (b) ascending (c) random (d) by primary key

    Hint

    It sorts low to high unless told otherwise.

    Answer

    (b) ascending

  2. Q2 · 1 mark · Very short answerBoard level

    Which operator is used to check whether a column has no value?

    Hint

    Not an equals sign.

    Answer

    IS NULL

  3. Q3 · 3 marks · Short answerHarder board level

    Write the output of SELECT Name, City, Salary FROM EMPLOYEE WHERE City <> 'Delhi' ORDER BY Salary DESC;

    Hint

    Sort the non-Delhi rows; NULL is last in descending order.

    Answer

    Meena Iyer | Chennai | 61000
    Farah Khan | Mumbai | 45000
    Ravi Kumar | Mumbai | 38000
    Kiran Shah | Pune | NULL

  4. Q4 · 1 mark · Assertion-reasonHarder board level

    Assertion (A): In the pattern '_a%', the second character must be a.
    Reason (R): The underscore matches exactly one character and % matches any number of characters.
    (a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.

    Hint

    The underscore holds the first place.

    Answer

    (a) Both are true and R explains A.

  5. Q5 · 3 marks · Short answerHOTS / competency

    Write a query that shows the names of the Mumbai employees with a non-NULL salary in descending order of name, and give its output. Then explain why this query would return 6 rows if the WHERE clause were removed.

    Hint

    City LIKE or = both work.

    Answer

    SELECT Name FROM EMPLOYEE WHERE City = 'Mumbai' AND Salary IS NOT NULL ORDER BY Name DESC; Output: Ravi Kumar, then Farah Khan. Without WHERE the query would return all six employees, since no condition filters any row.

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.What does % match in LIKE?Show answer

    Zero or more characters.

  2. 2.What does _ match in LIKE?Show answer

    Exactly one character.

  3. 3.How is a missing value tested?Show answer

    IS NULL or IS NOT NULL.

  4. 4.What is the default sort order?Show answer

    Ascending.

  5. 5.Where does NULL appear in ascending order?Show answer

    First (MySQL).

  6. 6.Which clause is last in a SELECT query?Show answer

    ORDER BY.

LIKE, IS NULL and ORDER BY: frequently asked questions

What is the difference between % and _ in the LIKE operator?

The percent sign matches any number of characters, including none. The underscore matches exactly one character.

Why can NULL not be tested with the equals sign?

NULL means unknown, and any comparison with an unknown value is not true. Use IS NULL or IS NOT NULL.

How do I sort a result in descending order?

Add DESC after the column name in the ORDER BY clause, for example ORDER BY Salary DESC.

Can ORDER BY use two columns?

Yes. Rows are sorted by the first column and rows with equal values are sorted by the second, each with its own ASC or DESC.