×

SQL Wildcards

SQL WILDCARD CHARACTERS:

SQL wild card character is used to substitute zero or more characters of a string.

These characters are used with the “LIKE” operator.

Wild Card Characters in SQL:

  • %
  • _
  • []
  • ^
  • -

“%” :

It is used to represent the zero or more characters in a string.

Example:

Student Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Command For Selecting details of students from Student table whose name Starts with “N”

SQL Code:

Select * from student where sname LIKE ‘N%’;

Output:

sidsnamesagesgenderPhonenumber
4Nethranand21Male7675643423
5Naveen23Male6567784532
8Nandhini23Female6785674839

“_” :

       It is used to represent a single character.

Example:

      Student Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Command For Selecting details of students from Student table whose name ends with “a”

SQL Code:

Select * from student where sname LIKE ‘_%a’;

Output:

sidsnamesagesgenderPhonenumber
2Ramya24Female6687654634
6Harshita22Female9867546231

“[]”:

     It is used to represent any single character within the bracket.

Example:

      Student Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Command For Selecting details of students from Student table whose name ends with “a”

SQL Code:

Select * from student where sname LIKE ‘[NH]%a’;

Output:

sidsnamesagesgenderPhonenumber
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
8Nandhini23Female6785674839
9Hashish22Male9453215052

“!”:

      It represents the character that is not in the bracket.

Example:

      Student Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Command For Selecting details of students from the Student table whose name does not start with N and H

SQL Code:

Select * from student where sname LIKE “[!NH]%”;

Output:

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
7Bindu26Female6563412768
10Rahul21Male9998989898

“-“ :

     It represents the character within the given range which is present in brackets.

Example:

      Student Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Command For Selecting details of students from Student table whose name in which first character lies in the range [A-R].

SQL Code:

Select * from student where sname LIKE ‘[A-R]%’;

Output:

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

“#”:

     It represents the numeric character in a string.

Example:

      Student Table

sidsnamesagesgenderPhonenumber
1Abhinav‘22’Male9895678909
2Ramya‘24’Female6687654634
3Preetham‘21’Male9867546453
4Nethranand‘21’Male7675643423
5Naveen‘23’Male6567784532
6Harshita‘22’Female9867546231
7Bindu‘26’Female6563412768
8Nandhini‘23’Female6785674839
9Hashish‘22’Male9453215052
10Rahul‘21’Male9998989898

Command For Selecting details of students from Student table whose age in which first character is ‘2’.

SQL Code:

Select * from student where sname LIKE ‘2#’;

Output:

sidsnamesagesgenderPhonenumber
1Abhinav‘22’Male9895678909
2Ramya‘24’Female6687654634
3Preetham‘21’Male9867546453
4Nethranand‘21’Male7675643423
5Naveen‘23’Male6567784532
6Harshita‘22’Female9867546231
7Bindu‘26’Female6563412768
8Nandhini‘23’Female6785674839
9Hashish‘22’Male9453215052
10Rahul‘21’Male9998989898

Related Topics

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.