Learn to Code SQL Example – SQL | Wildcard operators

(SQL Example for Citizen Data Scientist & Business Analyst)

 

SQL | Wildcard operators

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:

Operator Description
% It is used in substitute of zero or more characters.
_ It is used in substitute of one character.
[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_NO NAME ADDRESS PHONE Age
    3 SUJIT ROHTAK XXXXXXXXXX 20
    3 SUJIT ROHTAK XXXXXXXXXX 20
  • 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_NO NAME ADDRESS PHONE Age
    2 RAMESH GURGAON XXXXXXXXXX 18
  • 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_NO NAME ADDRESS PHONE Age
    2 RAMESH GURGAON XXXXXXXXXX 18
    2 RAMESH GURGAON XXXXXXXXXX 18
    3 SUJIT ROHTAK XXXXXXXXXX 20
    3 SUJIT ROHTAK XXXXXXXXXX 20
  • 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_NO NAME ADDRESS PHONE Age
    1 Ram Delhi XXXXXXXXXX 18
    4 SURESH Delhi XXXXXXXXXX 18
  • 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_NO NAME ADDRESS PHONE Age
    1 Ram Delhi XXXXXXXXXX 18
  • To fetch records from Student table with ADDRESS containing total of 6 characters.
    SELECT * FROM Student WHERE ADDRESS LIKE '______';
    

    Output:

    ROLL_NO NAME ADDRESS PHONE Age
    3 SUJIT ROHTAK XXXXXXXXXX 20
    3 SUJIT ROHTAK XXXXXXXXXX 20
  • 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_NO NAME ADDRESS PHONE Age
    3 SUJIT ROHTAK XXXXXXXXXX 20

 

 

 

Learn to Code SQL Example – SQL | Wildcard operators

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!