How to Select Words With Certain Values at the End of Word in SQL?
Last Updated :
12 Dec, 2024
Selecting words from a string with specific values at the end can be crucial in SQL for tasks like filtering data, pattern matching, and extracting relevant information. In SQL, we often need to work with strings where we need to find words ending with certain characters or patterns.
This guide will show us how to use functions and operators effectively to achieve this, making it easier for both beginners and advanced users to perform complex queries.
Some Wildcard Characters and Their Meanings
In SQL, wildcard characters are used to match patterns in text fields. They help in filtering and selecting data based on patterns rather than exact matches. Understanding these wildcard characters—such as %
, _
, [ ]
, and ^
is crucial for effective querying and pattern matching within strings. Each symbol represents different types of matches, enabling flexible and powerful SQL queries.
Symbol | Description |
% | specifies 0 or more characters |
_ | specifies single character |
[ ] | specifies any single character within the brackets |
^ | specifies any characters, not in the brackets |
Examples:
Symbol | Description |
a% | any value that starts with a |
%a | any value that ends with a |
a%a | any value that starts with a and ends with a |
_a% | any value which has a at the second position |
%a% | any value having a in it |
%_a% | any value having at least one character before a |
Steps to Select Words with Certain Values at the End
To select words from a string with specific values at the end in SQL, follow these steps. By using the LIKE
operator combined with wildcard characters, we can filter and retrieve data based on specific patterns in text fields. This process is essential for tasks like filtering data, pattern matching, and extracting specific information from strings.
Step 1: Table definition
First, we need to set up a table with sample data to work with. In this example, we use a geeksforgeeks
table in a geeks
database to demonstrate the process:
Query:
CREATE TABLE geeksforgeeks(
FIRSTNAME VARCHAR(20), LASTNAME VARCHAR(20),
GENDER VARCHAR(20));
Step 2: Inserting a data
Populate the table with sample data that includes names and gender information. This will allow us to demonstrate different queries using the LIKE
operator:
Query:
INSERT INTO geeksforgeeks VALUES
('ROMY', 'Kumari', 'female'),
('Rinkle', 'Arora', 'female'),
('Nikhil', 'Kalra','male'),
('Pushkar', 'Jha', 'male'),
('Sujata', 'jha', 'female'),
('Roshini', 'Kumari','female'),
('Ayushi', 'Chaudhary', 'female'),
('Akash', 'Gupta', 'male'),
('Akanksha', 'Gupta', 'female'),
('Chiranjeev', 'Arya', 'male'),
('Shivani', 'Jindal','female'),
('Shalini', 'Jha', 'female'),
('Sambhavi','Jha', 'female');
Step 5: Viewing Table Data
To see the data in the geeksforgeeks
table, use the SELECT
statement. This command will display all columns and rows in the geeksforgeeks
table.
Query:
SELECT * FROM geeksforgeeks;
Output
geeksforgeeksStep 6: Matching End Character
To select words with specific values at the end of a word, use the LIKE
operator with patterns. For example, we can use %a
to find words ending with 'a', or %ra
to find words ending with 'ra'. These patterns enable flexible searches within text fields, helping us filter data based on specific criteria.
1. Query to get the last name from having 'a' at the end of their last name
The query retrieves the LASTNAME
column from the geeksforgeeks
table where the LASTNAME
ends with the letter 'a'. The %
character is used as a wildcard to match any sequence of characters, allowing us to filter names that end with 'a'.
Query:
SELECT LASTNAME FROM geeksforgeeks WHERE LASTNAME LIKE '%a';
Output
LASTNAME |
---|
Arora |
Kalra |
Jha |
jha |
Gupta |
Gupta |
Arya |
Jha |
Jha |
2. Query to get last names ending with 'ra'
The query retrieves last names from the geeksforgeeks
table that end with the substring 'ra'. The %
wildcard is used to match any characters before 'b', making it possible to filter names ending specifically with 'ra'.
Query:
SELECT LASTNAME FROM geeksforgeeks Where LASTNAME LIKE '%ra'
Output
3. Query to get last names ending with 'A':
This query selects last names from the geeksforgeeks
table that end with the letter 'A'. The result will show that there are no last names ending with 'A', demonstrating that SQL pattern matching is case-sensitive.
Query:
SELECT LASTNAME FROM geeksforgeeks Where LASTNAME LIKE 'A';
Output
Conclusion
Selecting words with certain values at the end in SQL can be effectively achieved using the LIKE
operator with wildcards, the RIGHT
function for precise character selection from the end, and the SUBSTRING
function for substring comparisons. Each of these methods provides a powerful way to filter and extract specific patterns within strings, enabling more complex data manipulation in SQL. By mastering these techniques, we can improve our SQL queries and make our database interactions more efficient.
Similar Reads
How to Exclude Records With Certain Values in SQL Select?
In this article, we will understand how to exclude some records having certain values from a table. For the purpose of demonstration, we will be creating a Participant table in a database called âGeeksForGeeksDatabaseâ. Step 1: Creating the DatabaseUse the below SQL statement to create a database ca
3 min read
How to Select the Nth Row in a SQLite Database Table?
In SQLite, selecting a specific row from a table can be a common requirement, especially when dealing with large datasets. In this article, we will explore different methods to select the nth row from a SQLite database table. Whether we're a beginner or an experienced developer understanding these m
4 min read
How to Insert a Value that Contains an Apostrophe in SQL?
SQL is a standard database language used to access and manipulate data in databases. SQL stands for Structured Query Language. SQL was developed by IBM Computer Scientists in the 1970s. By executing queries SQL can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL
3 min read
SQL SELECT WHERE Field Contains Words
In SQL, the SELECT WHERE clause is a fundamental tool for filtering data based on specific conditions. When working with text fields, the SELECT WHERE clause helps identify records that contain certain words or patterns. This is commonly achieved using the LIKE operator for basic pattern matching, o
5 min read
How to Print the Starting and End Position of Each Word Using CTE?
The Common Table Expressions (CTE) were introduced into standard SQL in order to simplify various classes of SQL Queries for which a derived table was just unsuitable. In this article, we will see, that in a given string, which contains alphabets and asterisks, print the starting and end position of
2 min read
How to Use Reserved Words as Column Names in SQL?
In SQL, certain words are reserved. These are called Keywords or Reserved Words. These words cannot be used as identifiers i.e. as column names in SQL. But, there is an exception to this rule too. In this article, we will discuss how to use Reserved Words as column names in SQL. For this article, we
2 min read
How to Select the First Row of Each GROUP BY in SQL?
Selecting the first row of each group in SQL is a common requirement in database queries, especially when working with grouped data. SQL GROUP BY queries are highly effective for organizing data into logical groups, and additional techniques like window functions help to isolate the first row from e
5 min read
SQL Query to Select Data from Tables Using Join and Where
In SQL, the JOIN clause combines data from multiple tables based on a common column, while the WHERE clause filters the results based on specific conditions. Together, they allow us to retrieve relevant data efficiently from related tables. This article will guide us in using SQL JOIN and WHERE clau
5 min read
How to Find and Replace Text in MS Word?
MS-Word is a powerful word processing application, used to create documents, articles, assignments, etc. with the help of features and tools provided by it. It is used to perform many operations and creating or editing files and documents. MS Word is popular in the market because of its wide variety
3 min read
How to UPDATE and REPLACE Part of a String in SQLite
In SQLite, updating and replacing parts of a string can be a common task, especially when dealing with textual data. SQLite, serverless architecture offers various methods to solve this problem. In this article, We will learn about string replace in a query with the help of various methods to know h
4 min read