w3resource

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


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 Expression: [charlist] wildcards.

Relational Algebra Tree:

Relational Algebra Tree: [charlist] wildcards.

Sql [^charlist] wildcards

Sample table: agents


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 Expression: Sql [^charlist] wildcards.

Relational Algebra Tree:

Relational Algebra Tree: Sql [^charlist] wildcards.

Sql [!charlist] wildcards

Sample table: agents


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 Expression: Sql [!charlist] wildcards.

Relational Algebra Tree:

Relational Algebra Tree: Sql [!charlist] wildcards.

See our Model Database

Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.



Follow us on Facebook and Twitter for latest update.