Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Select Words With Certain Values at the End of Word in SQL?

Improve Article
Save Article
  • Last Updated : 23 Sep, 2021
Improve Article
Save Article

To select words with certain values at the end of the word In SQL, we can use pattern matching. A pattern matching allows users to search for certain patterns in the data. It is done using the LIKE operator in SQL. The query uses wildcard characters to match a pattern, Wildcard characters are case-sensitive.

Some wildcard characters and their meanings:

SymbolDescription
%specifies 0 or more characters
_specifies single character
[ ]specifies any single character within the brackets
^specifies any characters, not in the brackets

Examples:

SymbolDescription
a%any value that starts with a
%aany value that ends with a
a%aany 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

To select words with certain values at the end:

Step 1: Create a database

The database can be created using CREATE command.

Query:

CREATE DATABASE geeks;

Step 2: Using a database

Use the below SQL statement to switch the database context to geeks:

Query:

USE geeks;

Step 3:  Table definition

We have the following demo_table in our geek’s database.

Query:

CREATE TABLE geeksforgeeks(
FIRSTNAME VARCHAR(20), LASTNAME VARCHAR(20),
GENDER VARCHAR(20));

Step 4: Inserting a data

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: For a view a table data 

Query:

SELECT * FROM geeksforgeeks;

Output:

Step 6: Matching end character

  • Query to get the last name from having ‘a’ at the end of their last name

Query:

SELECT LASTNAME FROM geeksforgeeks WHERE LASTNAME LIKE '%a';

Output:

  • Query to get Last name having ‘ra’ at the end.

Query:

SELECT LASTNAME FROM geeksforgeeks Where LASTNAME LIKE '%ra';

Output:

  • Get the last name having “A’ at the end. 

Query:

SELECT LASTNAME FROM geeksforgeeks Where LASTNAME LIKE 'A';

Output:

No value has ‘A’ at the end in the last name. This shows that values entered for pattern matching are case-sensitive.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!