List the SQL operations and explain range searching operations between and pattern matching operator ‘like’ with example.
Of course. Here is a comprehensive overview of SQL operations, followed by a detailed explanation of the BETWEEN
and LIKE
operators with examples.
SQL commands are divided into several categories, often called sublanguages, based on their function. The main categories of SQL operations are:
Data Definition Language (DDL):
Used to define and manage the structure of database objects. These commands are auto-committed, meaning their changes are permanent.
Examples: CREATE
(to create tables), ALTER
(to modify tables), DROP
(to delete tables), TRUNCATE
(to empty tables).
Data Manipulation Language (DML):
Used to manage the data within the database objects. These are the most common SQL commands.
Examples: SELECT
(to retrieve data), INSERT
(to add new data), UPDATE
(to modify existing data), DELETE
(to remove data).
Data Control Language (DCL):
Used to manage user access and permissions to the database.
Examples: GRANT
(to give permissions), REVOKE
(to take away permissions).
Transaction Control Language (TCL):
Used to manage transactions in the database, ensuring data integrity.
Examples: COMMIT
(to save a transaction), ROLLBACK
(to undo a transaction), SAVEPOINT
(to set a point to roll back to).
BETWEEN
OperatorThe BETWEEN
operator is used in a WHERE
clause to select values that fall within a specified range.
AND
(e.g., price >= 50 AND price <= 100
).BETWEEN
operator is inclusive, meaning it includes the start and end values in the range.`
sql
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
`
Let's use a sample Products
table to find products within a certain price range.
Products
Table:
| ProductID | ProductName | Price |
| :--- | :--- | :--- |
| 1 | Keyboard | 75.00 |
| 2 | Mouse | 25.00 |
| 3 | Monitor | 200.00 |
| 4 | USB Cable | 10.00 |
| 5 | Webcam | 100.00 |
Goal: Find all products with a price between $75 and $200, inclusive.
Query:
`
sql
SELECT ProductName, Price
FROM Products
WHERE Price BETWEEN 75.00 AND 200.00;
`
Result:
The query will return the Keyboard, Monitor, and Webcam, as their prices fall within or on the boundaries of the specified range.
| ProductName | Price |
| :--- | :--- |
| Keyboard | 75.00 |
| Monitor | 200.00 |
| Webcam | 100.00 |
LIKE
OperatorThe LIKE
operator is used in a WHERE
clause to search for a specified pattern in a column's text data.
LIKE
uses two special wildcard characters to define the search pattern.%
): Represents zero, one, or multiple characters._
): Represents exactly one single character.`
sql
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE 'pattern';
`
Let's use a sample Customers
table to perform various pattern searches.
Customers
Table:
| CustomerID | CustomerName | City |
| :--- | :--- | :--- |
| 101 | John Smith | New York |
| 102 | Jane Doe | London |
| 103 | Robert Jones | Los Angeles |
| 104 | Peter Pan | London |
Example 1: Find names that start with 'J'
The pattern 'J%'
means "starts with the letter J, followed by any number of characters."
Query:
`
sql
SELECT CustomerName
FROM Customers
WHERE CustomerName LIKE 'J%';
`
Result:
| CustomerName |
| :--- |
| John Smith |
| Jane Doe |
Example 2: Find names that end with 's'
The pattern '%s'
means "any number of characters, ending with the letter s."
Query:
`
sql
SELECT CustomerName
FROM Customers
WHERE CustomerName LIKE '%s';
`
Result:
| CustomerName |
| :--- |
| Robert Jones |
Example 3: Find names that contain 'an'
The pattern '%an%'
means "any number of characters, followed by 'an', followed by any number of characters."
Query:
`
sql
SELECT CustomerName
FROM Customers
WHERE CustomerName LIKE '%an%';
`
Result:
| CustomerName |
| :--- |
| Jane Doe |
| Peter Pan |
Example 4: Find names that are four letters long and start with 'J'
The pattern 'J___'
uses three underscores, each representing a single character.
Query:
`
sql
SELECT CustomerName
FROM Customers
WHERE CustomerName LIKE 'J___';
`
Result:
This would match "John" but not "Jane" because "Jane" has four letters, but the query is looking for a four-letter name, and John Smith has more than 4 letters. If there was a customer named "John", it would return that record. In our current table, no records are returned. Let's adjust to find cities that are 6 letters long and end with 'on'.
Query for a city that is 6 letters long and ends with 'on': LIKE '____on'
`
sql
SELECT City
FROM Customers
WHERE City LIKE '____on';
`
Result:
| City |
| :--- |
| London |
| London |