The Like Condition:

The LIKE condition is used to specify a search for a pattern in a column.

Syntax:

SELECT column FROM table_name WHERE column LIKE pattern

Note:

We can use a “%” both before and after the pattern to define wildcards (missing letters in the pattern)

Example:

Using ‘UserInfo’ Table

LastName FirstName Address City Year
Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980
Pettersen Kari Storgt 20 Stavanger 1960

The SQL statement given below with LIKE condtion will return user information with first names that start with an ‘O’:

Query:

SELECT * FROM UserInfo WHERE FirstName LIKE ‘O%’

The Result:

LastName FirstName Address City Year
Hansen Ola Timoteivn 10 Sandnes 1951

The SQL statement given below with LIKE condtion will return user information with first names that start with an ‘e’:

Query:

SELECT * FROM UserInfo WHERE FirstName LIKE ‘%e’

The Result:

LastName FirstName Address City Year
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980

The SQL statement given below with LIKE condtion will return user information with first names that start with an ‘ar’:

Query:

SELECT * FROM UserInfo WHERE FirstName LIKE ‘%ar%’

The Result:

LastName FirstName Address City Year
Pettersen Kari Storgt 20 Stavanger 1960

 

Leave a Reply

Your email address will not be published. Required fields are marked *