Tutorials  /  Databases

Introduction to SQL LIKE Operator

Ccentron Redaktion · January 2024 ·4 min read ·Databases, Tutorial

SQL LIKE is used with WHERE clause to search for a pattern for a column. Wildcards are the one which is used for specifying the pattern. There are two wildcards that are used with the LIKE operator.

  • %: Percentage is used for representation of single, multiple or no occurrence.
  • _: The underscore is used for representation of a single character.

To use SQL LIKE operator, we must be very sure of the usage of the wildcard position as that will define the search pattern.

SQL Like Syntax

SQL Like operator can be used with any query with where clause. So we can use it with Select, Delete, Update etc.

SQL
SELECT column FROM table_name WHERE column LIKE pattern;

UPDATE table_name SET column=value WHERE column LIKE pattern;

DELETE FROM table_name WHERE column LIKE pattern;

In the sql like syntax mentioned above the “pattern” is the one that is defined by the usage of wildcards.

DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

SQL Like Example

Let’s try to understand the usage of SQL LIKE statement along with wildcards by some examples. Consider the following Customer table for the example.

CustomerId CustomerName
1 Amit
2 John
3 Annie

Find customer name with name starting with 'A'.

SQL
SELECT CustomerName FROM Customer WHERE CustomerName LIKE 'A%';
Output: Amit, Annie

Find customer name with name ending with 'e'.

SQL
SELECT CustomerName FROM Customer WHERE CustomerName LIKE '%e'
Output: Annie

Find customer name with name starting with 'A' and ending with 't'.

SQL
SELECT CustomerName FROM Customer WHERE CustomerName LIKE 'A%t'
Output: Amit

Find customer name with name containing 'n' at any position.

SQL
SELECT CustomerName FROM Customer WHERE CustomerName LIKE '%n%'
Output: Annie, John

Find customer name with name containing 'n' at second position.

SQL
SELECT CustomerName FROM Customer WHERE CustomerName LIKE '_n%'
Output: Annie

Find customer name with name containing 'i' at third position and ending with 't'.

SQL
SELECT CustomerName FROM Customer WHERE CustomerName LIKE '__i%t'
Output: Amit

SQL Not Like

Sometimes we want to get records that don’t match the like pattern. In that case we can use SQL not like operator. SQL not like statement syntax will be like below.

SQL
SELECT column FROM table_name WHERE column NOT LIKE pattern;

UPDATE table_name SET column=value WHERE column NOT LIKE pattern;

DELETE FROM table_name WHERE column NOT LIKE pattern;

As an example, let’s say we want the list of customer names that don’t start with 'A'. Below query will give us the required result set.

SQL
SELECT CustomerName FROM Customer WHERE CustomerName NOT LIKE 'A%';
Output: John

SQL Multiple Like

We can have multiple like statements in SQL query. For example, if we want a list of customer names starting from 'Jo' and 'Am' then we will have to use multiple like statements like below.

SQL
SELECT CustomerName FROM Customer WHERE CustomerName LIKE 'Am%' OR CustomerName LIKE 'Jo%';

That’s all for SQL like operator and SQL not like operator examples. Introduction to SQL LIKE Operator

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren