SQL | Wildcard operators - GeeksforGeeks

01 Sep, 2020
Prerequisite:  SQL | WHERE ClauseIn 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:
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

Queries
To fetch records from Student table with NAME ending with letter ‘T’.SELECT * FROM Student WHERE NAME LIKE '%T';
Output:ROLL_NONAMEADDRESSPHONEAge3SUJITROHTAKXXXXXXXXXX203SUJITROHTAKXXXXXXXXXX20
SELECT * FROM Student WHERE NAME LIKE '%T';

Output:
To fetch records from Student table with NAME ending any letter but starting from ‘RAMES’.SELECT * FROM Student WHERE NAME LIKE 'RAMES_';
Output:2RAMESHGURGAONXXXXXXXXXX18ROLL_NONAMEADDRESSPHONEAge2RAMESHGURGAONXXXXXXXXXX18
SELECT * FROM Student WHERE NAME LIKE 'RAMES_';

Output:
2RAMESHGURGAONXXXXXXXXXX18
To fetch records from Student table with address containing letters ‘a’, ‘b’, or ‘c’.SELECT * FROM Student WHERE ADDRESS LIKE '%[A-C]%';
Output:2RAMESHGURGAONXXXXXXXXXX18ROLL_NONAMEADDRESSPHONEAge2RAMESHGURGAONXXXXXXXXXX182RAMESHGURGAONXXXXXXXXXX183SUJITROHTAKXXXXXXXXXX203SUJITROHTAKXXXXXXXXXX20
SELECT * FROM Student WHERE ADDRESS LIKE '%[A-C]%';

Output:
2RAMESHGURGAONXXXXXXXXXX18
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_NONAMEADDRESSPHONEAge1RamDelhiXXXXXXXXXX184SURESHDelhiXXXXXXXXXX18
SELECT * FROM Student WHERE ADDRESS LIKE '%[^A-C]%';

Output:
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_NONAMEADDRESSPHONEAge1RamDelhiXXXXXXXXXX18
SELECT * FROM Student WHERE PHONE LIKE '9__5%';

Output:
To fetch records from Student table with ADDRESS containing total of 6 characters.SELECT * FROM Student WHERE ADDRESS LIKE '______';
Output:ROLL_NONAMEADDRESSPHONEAge3SUJITROHTAKXXXXXXXXXX203SUJITROHTAKXXXXXXXXXX20
SELECT * FROM Student WHERE ADDRESS LIKE '______';

Output:
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_NONAMEADDRESSPHONEAge3SUJITROHTAKXXXXXXXXXX20
SELECT DISTINCT * FROM Student WHERE ADDRESS LIKE '%OH%';

Output:
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.
RAJAT1996
MANMEETKAUR3
lonewolf_ab
shubham_singh
khushboogoyal499
SQL-Clauses-Operators
DBMS
SQL
DBMS
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between Clustered and Non-clustered index
CTE in SQL
SQL | Views
Difference between DELETE, DROP and TRUNCATE
Difference between Primary Key and Foreign Key
How to find Nth highest salary from a table
SQL | ALTER (RENAME)
CTE in SQL
SQL | Views
Difference between DELETE, DROP and TRUNCATE