w3resource

SQL Exercises: Filtering records with where clause and like operator


10. Customers with Names Ending with N

From the following table, write a SQL query to find the details of the customers whose names end with the letter 'n'. Return customer_id, cust_name, city, grade, salesman_id.

Sample table: customer

 customer_id |   cust_name    |    city    | grade | salesman_id 
-------------+----------------+------------+-------+-------------
        3002 | Nick Rimando   | New York   |   100 |        5001
        3007 | Brad Davis     | New York   |   200 |        5001
        3005 | Graham Zusi    | California |   200 |        5002
        3008 | Julian Green   | London     |   300 |        5002
        3004 | Fabian Johnson | Paris      |   300 |        5006
        3009 | Geoff Cameron  | Berlin     |   100 |        5003
        3003 | Jozy Altidor   | Moscow     |   200 |        5007
        3001 | Brad Guzan     | London     |       |        5005

Sample Solution:

-- This query selects all columns from the 'customer' table.
SELECT *
-- Specifies the table from which to retrieve the data (in this case, 'customer').
FROM customer
-- Filters the rows to only include those where the 'cust_name' column contains the letter 'n' at the end.
WHERE cust_name LIKE '%n';

Output of the Query:

customer_id	cust_name		city	grade	salesman_id
3008		Julian Green		London	300	5002
3004		Fabian Johnson		Paris	300	5006
3009		Geoff Cameron		Berlin	100	5003
3001		Brad Guzan		London		5005

Code Explanation:

The said SQL query selects all columns (*) from the 'customer' table where the value of the "cust_name" column ends with the letter 'n' .
The '%' is a wildcard character in SQL, which can match any string of any length (including an empty string).
The 'LIKE' operator is used to search for a specific pattern in a column.

Relational Algebra Expression:

Relational Algebra Expression: Using where clause with like operator.

Relational Algebra Tree:

Relational Algebra Tree: Using where clause with like operator.

Explanation :

Syntax of filter records using where clause with like operator

Visual presentation :

Result of filter records using where clause with like operator

Go to:


PREV : Customers with Names Starting with B.
NEXT : Salespeople Name Starts 'N' with Fourth 'L'.


Practice Online



For more Practice: Solve these Related Problems:

  • Write a SQL query to find the details of customers whose names end with the letter 'd'. Return customer_id, cust_name, city, grade, salesman_id.
  • Write a SQL query to retrieve the details of customers whose names start with the letter 'F'. Return customer_id, cust_name, city, grade, salesman_id.
  • Write a SQL query to find the details of customers whose names contain the letter 'e'. Return customer_id, cust_name, city, grade, salesman_id.
  • Write a SQL query to list all customers whose names have exactly 5 letters. Return customer_id, cust_name, city, grade, salesman_id.


Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.