LIKE

LIKE is an operator utilized in SQL to determine if a certain pattern exists within a specified string. It is often used with the WHERE clause to search for specified patterns in a column.

Example

SELECT * FROM Customers
WHERE CustomerName LIKE "%or%";

Output

+----+--------------+-------------+
| Id | CustomerName | ContactName |
+----+--------------+-------------+
| 3 | Antonio Moreno| Antonio Moreno |
| 7 | Around the Horn| Thomas Hardy |
+----+--------------+-------------+

Explanation

In this example, the LIKE clause is used on the Customers table to select all records where CustomerName contains the text “or”. Two rows in the result set contain “or” in the CustomerName.

Example

SELECT *
FROM employees
WHERE first_name LIKE 'J%';

Output

| id | first_name | last_name | hire_date |
|----|------------|-----------|------------|
| 1 | John | Doe | 2015-01-22 |
| 3 | Jennifer | Smith | 2017-12-18 |

Explanation

The SQL snippet retrieves all records from the employees table where the first_name starts with ‘J’. The ’%’ character is a wildcard in SQL and matches any sequence of characters (including no characters).

Example

SELECT *
FROM Employees
WHERE FirstName LIKE '%a%';

Output

FirstName LastName
------------ ---------
Anisha Sinha
Ram Kumar
Mira Singh

Explanation

The LIKE operator in this SQL statement allows pattern matching. It selects all records from the Employees table where the FirstName contains ‘a’ anywhere within the string.

Example

SELECT customer_name
FROM customers
WHERE customer_name LIKE 'A%';

Output

| CUSTOMER_NAME |
|----------------|
| Adams |
| Anderson |
| Arnold |

Explanation

The above example demonstrates the use of the LIKE clause in SQL. The LIKE clause is utilized to filter the records based on a specified pattern, in this case, selecting customer names from the ‘customers’ table that begin with ‘A’. The ’%’ symbol is used as a wildcard, representing any sequence of characters. Hence, ‘A%’ stands for any strings that start with ‘A’.

Example

SELECT *
FROM employees
WHERE last_name LIKE 'S%';

Output

| id | first_name | last_name |
|----|------------|-----------|
| 1 | John | Smith |
| 3 | Sam | Snow |

Explanation

The LIKE keyword in the SQL statement is used to search for a specified pattern in a column. In the example, it is fetching all the records from the employees table where the last_name starts with ‘S’. The ’%’ symbol is a wildcard character that represents zero, one, or multiple characters.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.