How to do Case Sensitive and Case Insensitive Search in a Column in MySQL
Last Updated :
08 Apr, 2024
LIKE Clause is used to perform case-insensitive searches in a column in MySQL and the COLLATE clause is used to perform case-sensitive searches in a column in MySQL.
Learning both these techniques is important to understand search operations in MySQL. Case sensitivity can affect search queries in MySQL so knowing when to perform a specific type of search becomes very important. This article explains how to search with the LIKE operator and COLLATE() clause with examples.
Demo MySQL Database
Let's create a demo MySQL table that will be used for examples in this tutorial.
Users Table:
name |
---|
John |
john |
ava |
joHn |
JOHN |
neel |
To create this table, write the following MySQL queries:
MySQL
CREATE TABLE Users (
name VARCHAR(255)
);
INSERT INTO Users VALUES
('John'),
('john'),
('ava'),
('joHn'),
('JOHN'),
('neel');
COLLATION
In MySQL, a collation determines how string comparison is done. It’s a set of rules that define how characters in a character set are compared and sorted.
By default, MySQL uses a case-insensitive collation for string comparisons. This means that when MySQL compares two strings, it considers ‘A’ and ‘a’ to be the same. For example, if a record in database is the name ‘John’, and user run a search for ‘john’, MySQL will return the record with the name ‘John’. This is because it uses a case-insensitive collation, so ‘John’ and ‘john’ are considered equal.
Example:
Let's say we have a table Users in which there is a column name
SELECT * FROM users WHERE name = 'john';
Output:
| name |
|-------|
| John |
| john |
| joHn |
| JOHN |
Using LIKE clause to do Case-Insensitive Search in a Column
The LIKE clause and wildcards in MySQL are powerful tools used in the WHERE clause of a SELECT, UPDATE, or DELETE statement to filter results. It allows you to search for a specified pattern within a column.
Example:
SELECT * FROM Users WHERE name LIKE 'john';
This query will return all customers whose name is "john". Since it is a case-insensitive search, the query returns multiple rows.
| name |
|------|
| John |
| john |
| joHn |
| JOHN |
Using COLLATE() Clause to do Case-sensitive Search in a Column
COLLATE clause is used to perform a case-sensitive search operation in a column in MySQL. The COLLATE clause allows users to specify the collation or the set of rules for comparing strings in SQL.
Case-sensitive Search
The utf8mb4 character set in MySQL is capable of storing any Unicode character. This is an extension of the utf8 character set. It allows characters that require up to four bytes in UTF-8, whereas utf8 only allows characters that require up to three bytes.
For instance, in Users table, if user wants to find all records where the name is ‘john’ (all lowercase), not ‘John’ or ‘JOHN’. If the column uses a case-sensitive collation (like utf8_bin), the query can be written as :
Example:
SELECT * FROM Users WHERE name COLLATE utf8mb4_bin LIKE 'john';
Output:
| name |
|------|
| john |
The SQL query written will return all records from the Users table where the name in 'john'. The COLLATE utf8mb4_bin makes the LIKE operator case-sensitive.
Explanation:
- SELECT * FROM Users: This part of the query selects all records from the Users table.
- WHERE name COLLATE utf8mb4_bin LIKE 'j%': This is the condition for selecting the records. It only selects the records where the name starts with a lowercase ‘j’. The COLLATE utf8mb4_bin makes the comparison case-sensitive, and the LIKE 'j%' matches any name that starts with a lowercase ‘j’.
- So above query will return the records for ‘john’ ,‘joHn’ and "John', but not , ‘JOHN’, ‘ava’, or ‘neel’.
Conclusion
MySQL provides powerful tools for string comparison and pattern matching in SQL queries. The LIKE operator, used in conjunction with wildcards, allows for flexible pattern matching within a column. By default, MySQL uses a case-insensitive collation for string comparisons, treating ‘A’ and ‘a’ as the same. This is useful for most general use-cases.
However, there are situations where case-sensitive search is required. For such scenarios, MySQL allows the use of the COLLATE clause to specify a case-sensitive collation for the query. This can be particularly useful when dealing with data that is case-sensitive, such as passwords or unique codes.
Remember, the choice of collation (case-sensitive or case-insensitive) depends on the specific requirements of database and the nature of the data. Always choose the one that best fits your needs.
Similar Reads
How to Query Case-insensitive Data in Django ORM
To query case insensitive data, Django provides several lookup methods that we can use like iexact, icontains, istartswith, iendswith, etc. In this article we will be discussing how to filter and query data from the database without caring if its upper or lowercase using Django ORM.Let's say we have
4 min read
How to Combine LIKE and IN in SQL Statement
The LIKE and IN operators in SQL are essential for building efficient, complex queries that filter data with precision. Combining these two operators in a single query enables users to target specific patterns in data while also filtering based on predefined valuesIn this article, we will explain ho
3 min read
How to Remove the First Characters of a Specific Column in a Table in SQL?
Here we will see, how to remove the first characters of a specific column in a table in SQL. We can do this task using the String function. String functions are used to perform an operation on an input string and return an output string. There are various string functions like LEN(for SQL server), S
3 min read
How to Case Insensitive Sorting on a Collection using Lodash orderBy?
When working with data, sorting is important to make it easy to understand. But with words, uppercase and lowercase letters can cause problems. Luckily, Lodash's orderBy function helps sort words without worrying about the case.There are two approaches to performing case-insensitive sorting using Lo
2 min read
How to Commit Case-Sensitive Only Filename Changes in Git?
Git is a distributed version control system that is widely used for tracking changes in source code during software development. By default, Git is case-sensitive, but the underlying file systems (like NTFS on Windows and HFS+ on macOS) can be case-insensitive. This can cause issues when renaming fi
2 min read
How to Check a Column is Empty or Null in MySQL?
In the databases, determining whether a column is empty or null is a common task. MySQL provides various techniques to perform this check, allowing users to filter and manipulate data efficiently. This article delves into the methods for checking if a column is empty or null in MySQL, outlining the
4 min read
How to Combine MySQL IN and LIKE Operators?
In database management, MySQL stands as one of the most popular relational database management systems. As developers strive to extract specific data from their databases, the judicious use of SQL operators becomes imperative. Among these operators, the IN and LIKE operators play crucial roles in fa
4 min read
How to Search For Column Names in SQL?
In SQL, sometimes we need to search the column names in a table using the prefixes. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks. Query: CREATE DATABA
2 min read
How to Rename a Column in MySQL?
Renaming columns in MySQL is a frequent task to keep data organized and flexible. It helps adjust database layouts to fit new needs without losing information. This article will show you different ways to rename columns in MySQL, making it easier to manage and update your database structure as your
4 min read
How to Split a Delimited String to Access Individual Items in MySQL?
In MySQL, it's common to encounter scenarios where we need to split a delimited string into individual items to process or manipulate them separately. This can be achieved using the various techniques and functions provided by MySQL. This article will explore splitting a delimited string and accessi
3 min read