Open In App

How to Select a Range of Letters in SQL?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In SQL, the LIKE clause is used for pattern matching with strings. It allows you to search for data based on specific patterns using wildcard operators. Among these operators, the [ ] wildcard character is especially useful for selecting a range of letters. By using it, you can easily filter data that starts with or falls within a specific range of characters, such as A-Z. This article will provide step-by-step instructions and examples to help you select a range of letters in SQL effectively.

How to Select a Range of Letters in SQL?

Selecting a range of letters in SQL involves using pattern matching techniques with the LIKE clause. This article will guide us through the process of filtering data based on specific character ranges, using practical examples to illustrate how this powerful feature works in SQL. Whether we need to find names starting with specific letters or filter out certain patterns, this guide will show us how to achieve it efficiently.

Steps to Select a Range of Letters

Let's explain the functions mentioned above with an example. For the demonstration, follow the steps outlined below to effectively filter data based on specific character ranges in our SQL database.

Step 1: Create a Sample Table

First, create a table that contains the data you want to filter. For example, let’s create a demo_table to store employee details with columns for NAME, AGE, and CITY.

CREATE TABLE demo_table(
NAME VARCHAR(20),
AGE INT,
CITY VARCHAR(20) );

Step 4: Insert Sample Data

Populate the demo_table with some sample records to work with. The following SQL query inserts data into the table:

INSERT INTO demo_table VALUES
('ROMY KUMARI', 22, 'NEW DELHI'),
('PUSHKAR JHA',23, 'NEW DELHI'),
('RINKLE ARORA',23, 'PUNJAB'),
('AKASH GUPTA', 23, 'UTTAR PRADESH'),
('AKANKSHA GUPTA',22, 'PUNJAB'),
('SUJATA JHA', 30,'PATNA');

Step 5: Verify the Data

To view the data in the demo_table, run the following SQL query

SELECT * FROM demo_table;

Output

demo_table
demo_table

Step 6: Using the LIKE Clause for Pattern Matching

Now that we have a sample dataset, let’s use the LIKE clause with the [ ] wildcard operator to filter the data by a specific range of letters. The % is a wildcard character that specifies 0 or more characters. Here's how it works:

SELECT * from tablename WHERE column_name LIKE '%[range_value]%';
  • In this syntax, the % symbol is used to match any sequence of characters, and the [ ] wildcard is used to specify a character ranges.

1. To select names starting with a specific range (O to S):

The query WHERE NAME LIKE '[O-S]%' is used to filter the demo_table and return only those records where the NAME column begins with any letter between O and S in the alphabet. This query helps to easily find names within a specific range by matching the starting character, making it useful for tasks like filtering records in data analysis.

SELECT * FROM demo_table WHERE NAME LIKE '[O-S]%';

Output

NAMEAGECITY
ROMY KUMARI22NEW DELHI
PUSHKAR JHA23NEW DELHI
RINKLE ARORA23PUNJAB

2. To select ages within a specific range (22 to 25)

The query SELECT * FROM demo_table WHERE AGE LIKE '[22-25]%' is used to filter the demo_table and return only those records where the AGE column falls within the specified range (22 to 25). This query allows us to quickly find and select ages that meet our criteria, which is particularly useful for filtering data based on numerical ranges.

SELECT * FROM demo_table WHERE AGE LIKE '[22-25]%';

Output

NAMEAGECITY
ROMY KUMARI22NEW DELHI
AKANKSHA GUPTA22PUNJAB

Conclusion

The LIKE clause in SQL is a powerful tool for pattern matching that helps in filtering records based on specific criteria, such as ranges of letters or numbers. By using wildcards like % and [ ], we can easily extract the data that meets our requirements, whether it’s filtering out specific names, roles, or age groups. Understanding how to effectively use the LIKE clause will enhance our ability to perform detailed data analysis and reporting, ensuring accurate and relevant result.


Article Tags :

Similar Reads