LIKE
Example
Section titled “Example”SELECT * FROM CustomersWHERE CustomerName LIKE "%or%";Output
Section titled “Output”+----+--------------+-------------+| Id | CustomerName | ContactName |+----+--------------+-------------+| 3 | Antonio Moreno| Antonio Moreno || 7 | Around the Horn| Thomas Hardy |+----+--------------+-------------+Explanation
Section titled “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
Section titled “Example”SELECT *FROM employeesWHERE first_name LIKE 'J%';Output
Section titled “Output”| id | first_name | last_name | hire_date ||----|------------|-----------|------------|| 1 | John | Doe | 2015-01-22 || 3 | Jennifer | Smith | 2017-12-18 |Explanation
Section titled “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
Section titled “Example”SELECT *FROM EmployeesWHERE FirstName LIKE '%a%';Output
Section titled “Output”FirstName LastName------------ ---------Anisha SinhaRam KumarMira SinghExplanation
Section titled “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
Section titled “Example”SELECT customer_nameFROM customersWHERE customer_name LIKE 'A%';Output
Section titled “Output”| CUSTOMER_NAME ||----------------|| Adams || Anderson || Arnold |Explanation
Section titled “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
Section titled “Example”SELECT *FROM employeesWHERE last_name LIKE 'S%';Output
Section titled “Output”| id | first_name | last_name ||----|------------|-----------|| 1 | John | Smith || 3 | Sam | Snow |Explanation
Section titled “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.