SQL
Database Management
Coding
SELECT statement
WHERE clause

SQL SELECT WHERE field contains words

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Structured Query Language (SQL) is a standardized programming language used for managing relational databases and performing various operations on the data they contain. One of the key operations in SQL is the SELECT statement, which is used to retrieve data from a database. Coupled with the WHERE clause, it becomes a powerful tool for filtering data based on specific conditions. In this article, we'll discuss how to use the SELECT WHERE clause to filter records that contain specific words within a field.

The Basics of the SELECT Statement

The SELECT statement is used to select data from a database. Data can be selected from one or more tables. Here's the basic syntax of the SELECT statement:

sql
SELECT column1, column2, ...
FROM tablename
WHERE condition;

Using the WHERE Clause for Text Searches

The WHERE clause is used to specify the conditions that must be met for the rows to be returned. When searching for rows in which a text field contains certain words, SQL provides several functions and operators, depending on the database system (like MySQL, PostgreSQL, SQL Server, etc.).

SQL Operators and Functions

  1. LIKE Operator: The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
    Example:
sql
   SELECT * FROM employees
   WHERE name LIKE '%John%';

This query retrieves all records from the employees table where the name column contains "John" anywhere in the text.

  1. ILIKE Operator (PostgreSQL specific): This operator functions like LIKE, but it is case-insensitive.
    Example:
sql
   SELECT * FROM employees
   WHERE name ILIKE '%john%';

Similar to the previous example, but case-insensitivity means it will match "john", "John", "JOHN", etc.

  1. REGEXP/RLIKE (MySQL/MariaDB specific): Used for complex pattern matching using regular expressions.
    Example:
sql
   SELECT * FROM products
   WHERE description REGEXP '\\b(smart|phone)\\b';

This query searches for products with descriptions containing the words "smart" or "phone" as whole words.

Case Sensitivity

SQL operations are generally case-sensitive, depending on the database system and collation settings. Using functions like LOWER() or UPPER() can help perform case-insensitive comparisons.

Practical Examples

Consider a database with a table named articles, having columns id, title, and content. Here are some practical examples of using the SELECT WHERE clause to find articles based on keywords in the title.

  1. Finding Articles with Exact Phrase:
sql
   SELECT id, title FROM articles
   WHERE title LIKE '%climate change%';
  1. Finding Articles with Any Specified Words:
sql
   SELECT id, title FROM articles
   WHERE title LIKE '%recycle%' OR title LIKE '%reuse%';
  1. Case-Insensitive Search:
sql
   SELECT id, title FROM articles
   WHERE LOWER(title) LIKE '%global warming%';

Performance Considerations

Using LIKE or ILIKE with wildcard characters, especially at the beginning of the pattern (e.g., LIKE '%word%'), can lead to performance issues in large databases because they may prevent the database from using indexes effectively. Full-text search features or dedicated search engines like Elasticsearch might be more efficient for larger datasets or more complex search requirements.

Summary Table

Function / OperatorUsageCase SensitiveAllows RegExExample
LIKEGeneral pattern matchingYesNoLIKE '%word%'
ILIKECase-insensitive pattern matching (PostgreSQL)NoNoILIKE '%word%'
REGEXP / RLIKEComplex patterns using regular expressionsYes (MySQL)YesREGEXP '\\bword\\b'

Conclusion

SQL's SELECT WHERE clause provides robust tools for filtering data based on text content. Using operators like LIKE, ILIKE, and REGEXP, you can tailor queries to meet nearly any text search requirement. However, understanding the implications of each method on performance and how they interact with data indexing is crucial for maintaining efficient database queries.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.