SQL [charlist] wildcards
[charlist] wildcards
The [charlist] WILDCARDS are used to represent any single character within a charlist.
The [^charlist] and [!charlist] WILDCARDS is used to represents any single character not in the charlist.
Example:
Sample table: agents+------------+----------------------+--------------------+------------+-----------------+---------+ | AGENT_CODE | AGENT_NAME | WORKING_AREA | COMMISSION | PHONE_NO | COUNTRY | +------------+----------------------+--------------------+------------+-----------------+---------+ | A007 | Ramasundar | Bangalore | 0.15 | 077-25814763 | | | A003 | Alex | London | 0.13 | 075-12458969 | | | A008 | Alford | New York | 0.12 | 044-25874365 | | | A011 | Ravi Kumar | Bangalore | 0.15 | 077-45625874 | | | A010 | Santakumar | Chennai | 0.14 | 007-22388644 | | | A012 | Lucida | San Jose | 0.12 | 044-52981425 | | | A005 | Anderson | Brisban | 0.13 | 045-21447739 | | | A001 | Subbarao | Bangalore | 0.14 | 077-12346674 | | | A002 | Mukesh | Mumbai | 0.11 | 029-12358964 | | | A006 | McDen | London | 0.15 | 078-22255588 | | | A004 | Ivan | Torento | 0.15 | 008-22544166 | | | A009 | Benjamin | Hampshair | 0.11 | 008-22536178 | | +------------+----------------------+--------------------+------------+-----------------+---------+
To get all rows from the table 'agents' with following condition -
1. the 'agent_name' must begin with the letter 'a' or 'b' or 'i'
the following sql statement can be used :
SELECT *
-- Select all columns
FROM agents
-- From the table named "agents"
WHERE agent_name LIKE '[abi]%';
-- Where the agent_name starts with 'a', 'b', or 'i', followed by any characters
Explanation:
- SELECT *: This statement selects all columns from the specified table.
- FROM agents: This specifies the table from which to retrieve data, in this case, the table named "agents".
- WHERE agent_name LIKE '[abi]%': This is the conditional clause that filters the rows returned by the query. It uses the LIKE operator to match patterns in the agent_name column. So, it looks for rows where the agent_name starts with the characters '['a', 'b', or 'i']' followed by any characters due to the '%' wildcard.
Relational Algebra Expression:
Relational Algebra Tree:
Sql [^charlist] wildcards
Sample table: agents+------------+----------------------+--------------------+------------+-----------------+---------+ | AGENT_CODE | AGENT_NAME | WORKING_AREA | COMMISSION | PHONE_NO | COUNTRY | +------------+----------------------+--------------------+------------+-----------------+---------+ | A007 | Ramasundar | Bangalore | 0.15 | 077-25814763 | | | A003 | Alex | London | 0.13 | 075-12458969 | | | A008 | Alford | New York | 0.12 | 044-25874365 | | | A011 | Ravi Kumar | Bangalore | 0.15 | 077-45625874 | | | A010 | Santakumar | Chennai | 0.14 | 007-22388644 | | | A012 | Lucida | San Jose | 0.12 | 044-52981425 | | | A005 | Anderson | Brisban | 0.13 | 045-21447739 | | | A001 | Subbarao | Bangalore | 0.14 | 077-12346674 | | | A002 | Mukesh | Mumbai | 0.11 | 029-12358964 | | | A006 | McDen | London | 0.15 | 078-22255588 | | | A004 | Ivan | Torento | 0.15 | 008-22544166 | | | A009 | Benjamin | Hampshair | 0.11 | 008-22536178 | | +------------+----------------------+--------------------+------------+-----------------+---------+
To get all rows from the table 'agents' with following condition -
1.the 'agent_name' must not begin with the letter 'a' or 'b' or 'i',
the following sql statement can be used :
SELECT *
-- Select all columns
FROM agents
-- From the table named "agents"
WHERE agent_name LIKE '[^abi]%';
-- Where the agent_name doesn't start with 'a', 'b', or 'i', followed by any characters
Explanation:
- SELECT *: This statement selects all columns from the specified table.
- FROM agents: This specifies the table from which to retrieve data, in this case, the table named "agents".
- WHERE agent_name LIKE '[^abi]%': This is the conditional clause that filters the rows returned by the query. It uses the LIKE operator to match patterns in the agent_name column.
Relational Algebra Expression:
Relational Algebra Tree:
Sql [!charlist] wildcards
Sample table: agents+------------+----------------------+--------------------+------------+-----------------+---------+ | AGENT_CODE | AGENT_NAME | WORKING_AREA | COMMISSION | PHONE_NO | COUNTRY | +------------+----------------------+--------------------+------------+-----------------+---------+ | A007 | Ramasundar | Bangalore | 0.15 | 077-25814763 | | | A003 | Alex | London | 0.13 | 075-12458969 | | | A008 | Alford | New York | 0.12 | 044-25874365 | | | A011 | Ravi Kumar | Bangalore | 0.15 | 077-45625874 | | | A010 | Santakumar | Chennai | 0.14 | 007-22388644 | | | A012 | Lucida | San Jose | 0.12 | 044-52981425 | | | A005 | Anderson | Brisban | 0.13 | 045-21447739 | | | A001 | Subbarao | Bangalore | 0.14 | 077-12346674 | | | A002 | Mukesh | Mumbai | 0.11 | 029-12358964 | | | A006 | McDen | London | 0.15 | 078-22255588 | | | A004 | Ivan | Torento | 0.15 | 008-22544166 | | | A009 | Benjamin | Hampshair | 0.11 | 008-22536178 | | +------------+----------------------+--------------------+------------+-----------------+---------+
To get all rows from the table 'agents' with following condition -
1.the 'agent_name' must not begin with the letter 'a' or 'b' or 'i',
the following sql statement can be used :
SELECT *
-- Select all columns
FROM agents
-- From the table named "agents"
WHERE agent_name LIKE '[!abi]%';
-- Where the agent_name starts with any character except 'a', 'b', or 'i', followed by any characters
Explanation:
- SELECT *: This statement selects all columns from the specified table.
- FROM agents: This specifies the table from which to retrieve data, in this case, the table named "agents".
- WHERE agent_name LIKE '[!abi]%': This is the conditional clause that filters the rows returned by the query. It uses the LIKE operator to match patterns in the agent_name column.
Relational Algebra Expression:
Relational Algebra Tree:
Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/sql/wildcards-like-operator/wildcards-charlist.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics