![]() |
VOOZH | about |
The PL/SQL LIKE operator is a powerful tool used in SQL queries to search for patterns in character data. It allows you to match strings based on specific patterns defined by wildcards. This operator is commonly used in SELECT, UPDATE, and DELETE statements to filter records based on partial or complex string matches.
In this article, we will explain PL/SQL LIKE Operator with its syntax, examples and output in detail.
The LIKE operator is particularly useful when you need to find data that matches a certain pattern but does not know the exact value. The PL/SQL LIKE operator is used to match a string against a pattern. The pattern can include two wildcards:
Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
Key Terms
We'll create two tables, Products and Suppliers, to demonstrate how the LIKE operator can be used for pattern matching in SQL queries. The LIKE operator allows us to search for specific patterns within text data, making it useful for filtering records based on partial matches or specific formats.
The Products table is created with columns ProductID, ProductName, and Category. The ProductID is the primary key. Data is inserted into tables. The Products table includes various items under different categories.
Query:
CREATE TABLE Products (
ProductID NUMBER PRIMARY KEY,
ProductName VARCHAR2(50),
Category VARCHAR2(30)
);
INSERT INTO Products (ProductID, ProductName, Category)
VALUES (1, 'Laptop', 'Electronics');
INSERT INTO Products (ProductID, ProductName, Category)
VALUES (2, 'Smartphone', 'Electronics');
INSERT INTO Products (ProductID, ProductName, Category)
VALUES (3, 'Tablet', 'Electronics');
INSERT INTO Products (ProductID, ProductName, Category)
VALUES (4, 'Desk Chair', 'Furniture');
INSERT INTO Products (ProductID, ProductName, Category)
VALUES (5, 'Dining Table', 'Furniture');
Output:
ProductID | ProductName | Category |
|---|---|---|
1 | Laptop | Electronics |
2 | Smartphone | Electronics |
3 | Tablet | Electronics |
4 | Desk Chair | Furniture |
5 | Dining Table | Furniture |
Explanation:
Products table is populated with five records, each representing a different product category. ProductID for unique identification, a ProductName to specify the item, and a Category to classify the product. The Suppliers table is created with columns SupplierID, SupplierName, and ContactName. The SupplierID is the primary key. Data is inserted into tables. Suppliers table contains supplier information.
Query:
CREATE TABLE Suppliers (
SupplierID NUMBER PRIMARY KEY,
SupplierName VARCHAR2(50),
ContactName VARCHAR2(50)
);
INSERT INTO Suppliers (SupplierID, SupplierName, ContactName)
VALUES (1, 'TechC', 'John Doe');
INSERT INTO Suppliers (SupplierID, SupplierName, ContactName)
VALUES (2, 'FurniCo', 'Jane Smith');
INSERT INTO Suppliers (SupplierID, SupplierName, ContactName)
VALUES (3, 'GadgetWorld', 'Alice Johnson');
INSERT INTO Suppliers (SupplierID, SupplierName, ContactName)
VALUES (4, 'ElectroMart', 'Bob Brown');
Output:
SupplierID | SupplierName | ContactName |
|---|---|---|
1 | TechC | John Doe |
2 | FurniCo | Jane Smith |
3 | GadgetWorld | Alice Johnson |
4 | ElectroMart | Bob Brown |
Explanation:
Suppliers table includes four records, each representing a different supplier. SupplierID for unique identification, a SupplierName to identify the supplier, and a ContactName for the primary contact person. The % wildcard matches zero or more characters in a string. Let's find all products whose names start with 'D'.
The query retrieves all products from the Products table where the ProductName starts with the letter 'D'. The % wildcard after 'D' allows for any number of characters following 'D'.
Query:
SELECT ProductName, Category
FROM Products
WHERE ProductName LIKE 'D%';
Output:
ProductName | Category |
|---|---|
Desk Chair | Furniture |
Dining Table | Furniture |
Explanation:
LIKE operator with the pattern 'D%' to find products where the ProductName starts with the letter "D". The _ wildcard matches exactly one character. Let's find all suppliers whose names are five characters long.
The query retrieves all suppliers from the Suppliers table where the SupplierName has exactly five characters. Each underscore _ represents a single character.
Query:
SELECT SupplierName, ContactName
FROM Suppliers
WHERE SupplierName LIKE '_____';
Output:
SupplierName | ContactName |
|---|---|
TechC | John Doe |
Explanation:
LIKE operator with the pattern '_____' to find suppliers whose SupplierName consists of exactly five characters. The NOT operator can be combined with LIKE to find records that do not match a pattern. Let's find all products that do not belong to the Electronics category.
The query retrieves all products from the Products table where the Category is not 'Electronics'. The NOT operator negates the LIKE condition.
Query:
SELECT ProductName, Category
FROM Products
WHERE Category NOT LIKE 'Electronics';
Output:
ProductName | Category |
|---|---|
Desk Chair | Furniture |
Dining Table | Furniture |
Explanation:
NOT LIKEoperator to filter out products in the Electronics category, displaying only those in other categories.Sometimes we need to search for patterns that include the % or _ characters. We can use the escape character to treat them as literals. Let's find all suppliers whose names contain 'Tech_'.
The query retrieves all suppliers from the Suppliers table where the SupplierName contains 'Tech_' as part of the name. The \ is used as an escape character to treat the underscore _ as a literal character.
Query:
SELECT SupplierName, ContactName
FROM Suppliers
WHERE SupplierName LIKE 'Tech_%' ESCAPE '\';
Output:
SupplierName | ContactName |
|---|---|
TechC | John Doe |
Explanation:
LIKE operator with the pattern 'Tech_%' and specifies an escape character '\ to handle special characters.The PL/SQL LIKE operator is an essential tool for pattern matching in Pl/SQL queries. It provides flexibility in searching for specific data using wildcards and can be combined with other operators to refine search criteria.
Understanding how to use the LIKE operator effectively can significantly enhance your ability to query databases.