Skip to content
Related Articles
Open in App
Not now

Related Articles

SQL | Wildcard operators

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 28 Feb, 2023
Improve Article
Save Article
Like Article

Prerequisite:  SQL | WHERE Clause In the above mentioned article WHERE Clause is discussed in which LIKE operator is also explained, where you must have encountered the word wildcards now lets get deeper into Wildcards. Wildcard operators are used with LIKE operator, there are four basic operators:

OperatorDescription
%It is used in substitute of zero or more characters.
_It is used in substitute of one character.
_It is used to substitute a range of characters.
[range_of_characters]It is used to fetch matching set or range of characters specified inside the brackets.
[^range_of_characters] or [!range of characters]It is used to fetch non-matching set or range of characters specified inside the brackets.

Basic syntax:

SELECT column1,column2 FROM table_name WHERE column LIKE wildcard_operator;
column1 , column2: fields in the table
table_name: name of table
column: name of field used for filtering data

table1

Queries

  • To fetch records from Student table with NAME ending with letter ‘T’.
SELECT * FROM Student WHERE NAME LIKE '%T';
  • Output:
ROLL_NONAMEADDRESSPHONEAge
3SUJITROHTAKXXXXXXXXXX20
3SUJITROHTAKXXXXXXXXXX20
  • To fetch records from Student table with NAME starting with letter ‘T’.
SELECT * FROM Student WHERE NAME LIKE 'T%';
  • output
ROLL_NONAMEADDRESSPHONEAGE
2TUSHARMUMBAI***21
2TANYAMUMBAI***21
  • To fetch records from Student table with NAME with letter ‘T’ at any position.
SELECT * FROM Student WHERE NAME LIKE '%T%';

output

ROLL_NONAMEADDRESSPHONEAGE
2ATHARVAMUMBAI*****20
4DIPTITHANE*****12
  • To fetch records from Student table with NAME ending any letter but starting from ‘RAMES’.
SELECT * FROM Student WHERE NAME LIKE 'RAMES_';
  • Output: 2RAMESHGURGAONXXXXXXXXXX18
ROLL_NONAMEADDRESSPHONEAge
2RAMESHGURGAONXXXXXXXXXX18
  • To fetch records from Student table with address containing letters ‘a’, ‘b’, or ‘c’.
SELECT * FROM Student WHERE ADDRESS LIKE '%[A-C]%';
  • Output: 2RAMESHGURGAONXXXXXXXXXX18
ROLL_NONAMEADDRESSPHONEAge
2RAMESHGURGAONXXXXXXXXXX18
2RAMESHGURGAONXXXXXXXXXX18
3SUJITROHTAKXXXXXXXXXX20
3SUJITROHTAKXXXXXXXXXX20
  • To fetch records from Student table with ADDRESS not containing letters ‘a’, ‘b’, or ‘c’.
SELECT * FROM Student WHERE ADDRESS LIKE '%[^A-C]%';
  • Output:
ROLL_NONAMEADDRESSPHONEAge
1RamDelhiXXXXXXXXXX18
4SURESHDelhiXXXXXXXXXX18
  • To fetch records from Student table with PHONE field having a ‘9’ in 1st position and a ‘5’ in 4th position.
SELECT * FROM Student WHERE PHONE LIKE '9__5%';
  • Output:
ROLL_NONAMEADDRESSPHONEAge
1RamDelhiXXXXXXXXXX18
  • To fetch records from Student table with ADDRESS containing total of 6 characters.
SELECT * FROM Student WHERE ADDRESS LIKE '______';
  • Output:
ROLL_NONAMEADDRESSPHONEAge
3SUJITROHTAKXXXXXXXXXX20
3SUJITROHTAKXXXXXXXXXX20
  • To fetch records from Student table with ADDRESS containing ‘OH’ at any position, and the result set should not contain duplicate data.
SELECT DISTINCT * FROM Student WHERE ADDRESS LIKE '%OH%';
  • Output:
ROLL_NONAMEADDRESSPHONEAge
3SUJITROHTAKXXXXXXXXXX20

This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!