SQL – Basic Commands – LIKE

We have separated LIKE clause from other logical / comparison operators because it has slightly more examples that we wish to share.

Screen Shot 2018-01-06 at 1.01.38 AM.png

We can use these wildcard operators in different combinations and the basic syntax for this clause is:

SELECT first_name, last_name

FROM CUSTOMER

WHERE first_name LIKE “XXX%”;

XXX can be used to represent any numeric or character value. 

 

This is extremely useful for large database where we want to search for certain records.

For example – we can use this clause for the following scenarios:

  1. To find customer records with this first name.

WHERE first_name LIKE “Jason%”;

  1. To find customer records whose age starts with a 3.

WHERE age LIKE “3%”;

  1. To find customer records that have an ‘A’ in the second position in their first_name.

WHERE first_name LIKE “_a%”;

  1. To find customer records that have an “ON” in the second and third positions in their last_name.

WHERE last_name LIKE “_on%”;

  1. To find customer records that ends with “N” in their first_name.

WHERE first_name LIKE “%n”;

  1. To find customer records that have an “J” in their first_name or age over 34 years old.

WHERE first_name LIKE “J%” OR age > 34;

We can always use this clause and combine with other logical / comparison operators whenever required to query specific observations in the database.

Advertisements


Categories: SQL Learning

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: