SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | Wildcard operators

Wildcard operators in SQL are used with the SQL LIKE operator to perform pattern matching in queries. These operators enable you to search for data that fits specific patterns, rather than having to know the exact value to search for.

Here are the most commonly used wildcard operators:

  1. Percent %: Represents zero or more characters.
    Example:

    SELECT * FROM employees
    WHERE name LIKE 'J%';
    

    This query selects all employees whose names start with the letter "J".

  2. Underscore _: Represents a single character.
    Example:

    SELECT * FROM employees
    WHERE name LIKE 'J_n';
    

    This query selects all employees whose names are exactly 3 characters long and start with "J" and end with "n", like "Jon", "Jan", etc.

  3. Square Brackets [...] (MS SQL Server): Matches any single character within the brackets.
    Example:

    SELECT * FROM employees
    WHERE name LIKE 'J[oa]n';
    

    This query will select all employees whose names are either "Jon" or "Jan" (only supported in some database systems like MS SQL Server).

  4. Caret ^ inside Square Brackets (MS SQL Server): Matches any single character not within the brackets.
    Example:

    SELECT * FROM employees
    WHERE name LIKE 'J[^oa]n';
    

    This query will select all employees whose names are 3 characters long, start with "J", end with "n", but the middle character is not "o" or "a".

Combining Wildcards:

You can combine these wildcard characters to make more complex pattern-matching conditions.

Example:

SELECT * FROM employees
WHERE name LIKE 'J%n';

This query will select all employees whose names start with "J" and end with "n".

Not Like:

You can also use NOT LIKE to exclude rows that match the pattern.

Example:

SELECT * FROM employees
WHERE name NOT LIKE 'J%';

This query will select all employees whose names do not start with "J".

Case Sensitivity:

Note that the LIKE query may be case-sensitive or case-insensitive depending on your database. For instance, SQL Server is case-insensitive by default, whereas PostgreSQL is case-sensitive. You may need to use database-specific functions like UPPER() or LOWER() to standardize case if necessary.

Example for making a case-insensitive search in PostgreSQL:

SELECT * FROM employees
WHERE LOWER(name) LIKE 'j%';

In summary, wildcard operators offer a powerful way to perform pattern matching in SQL queries, enabling more flexible and dynamic data retrieval.

  1. How to Use Wildcards in SQL Queries:

    • Description: Wildcards are special characters used to match parts of a value. Common wildcard characters are % and _.
    • Code:
      -- Using wildcards
      SELECT * FROM employees
      WHERE last_name LIKE 'Sm%';
      
  2. LIKE Operator in SQL with Wildcard Characters:

    • Description: The LIKE operator is used with wildcard characters to perform pattern matching.
    • Code:
      -- Using LIKE with wildcard %
      SELECT * FROM products
      WHERE product_name LIKE 'Laptop%';
      
  3. SQL Wildcard Operators for Pattern Matching:

    • Description: Wildcard operators, % and _, are used for pattern matching in SQL.
    • Code:
      -- Using wildcards for pattern matching
      SELECT * FROM customers
      WHERE phone_number LIKE '555%';
      
  4. % and _ Wildcard in SQL:

    • Description: % matches any sequence of characters, and _ matches any single character.
    • Code:
      -- Using % and _ wildcards
      SELECT * FROM products
      WHERE product_name LIKE 'Laptop%';
      
      SELECT * FROM employees
      WHERE last_name LIKE 'A_';
      
  5. Using Wildcards in WHERE Clause in SQL:

    • Description: Wildcards are commonly used in the WHERE clause to filter rows based on patterns.
    • Code:
      -- Using wildcards in WHERE clause
      SELECT * FROM orders
      WHERE order_date LIKE '2023-01%';
      
  6. SQL Wildcard Operators with Multiple Characters:

    • Description: Wildcards can represent multiple characters, making pattern matching flexible.
    • Code:
      -- Using wildcards with multiple characters
      SELECT * FROM products
      WHERE product_name LIKE 'S%e';
      
  7. Escaping Wildcard Characters in SQL:

    • Description: To search for actual % or _, you can use the ESCAPE keyword to escape the wildcard character.
    • Code:
      -- Escaping wildcard character
      SELECT * FROM text_data
      WHERE text_value LIKE '%\_%' ESCAPE '\';
      
  8. SQL Wildcard Operators for Searching:

    • Description: Wildcards are powerful tools for searching and filtering data.
    • Code:
      -- Searching with wildcards
      SELECT * FROM products
      WHERE product_name LIKE '%camera%';
      
  9. Wildcard Search in SQL with Patterns:

    • Description: Wildcards allow for flexible pattern-based searches.
    • Code:
      -- Wildcard search with patterns
      SELECT * FROM customers
      WHERE email LIKE '%@example%';
      
  10. SQL NOT LIKE Operator with Wildcards:

    • Description: The NOT LIKE operator is used to filter rows not matching a specific pattern.
    • Code:
      -- NOT LIKE with wildcards
      SELECT * FROM products
      WHERE product_name NOT LIKE '%Out of Stock%';
      
  11. Combining Wildcards with Other Operators in SQL:

    • Description: Wildcards can be combined with other operators for more complex queries.
    • Code:
      -- Combining wildcards with other operators
      SELECT * FROM employees
      WHERE first_name LIKE 'A%' AND salary > 50000;
      
  12. SQL Wildcard Characters in SELECT Statement:

    • Description: Wildcards can be used in the SELECT statement for more flexible output.
    • Code:
      -- Using wildcards in SELECT statement
      SELECT product_name, SUBSTRING(product_name, 1, 3) AS short_name
      FROM products
      WHERE product_name LIKE 'Laptop%';
      
  13. SQL Wildcard Search for Numeric and Alphanumeric Values:

    • Description: Wildcards are not limited to text; they can also be used for numeric or alphanumeric patterns.
    • Code:
      -- Wildcard search for numeric and alphanumeric values
      SELECT * FROM orders
      WHERE order_number LIKE 'ORD%';