Database Systems
CS – 2102
Wildcards in SQL
Week /Lab 8
Engr. Tahir Abbasi
Quote of the Day!!!
12/21/2022 Database Systems 2
Session’s Objectives
⚫ At the end of today’s session, you should have an
understanding of:
⚫ Wildcards
⚫ IN Operator
⚫ BETWEEN Operator
12/21/2022 Database Systems 3
WILDCARDS
⚫ A wildcard character is used to substitute one or
more characters in a string.
⚫ Wildcard characters are used with the SQL
LIKE operator.
⚫ The LIKE operator is used in a WHERE clause to
search for a specified pattern in a column.
12/21/2022 Database Systems 4
WILDCARDS
Symbol Description Example
% Represents zero or more
characters
bl% finds bl, black, blue, and blob
_ Represents a single character h_t finds hot, hat, and hit
[ ] Represents any single character
within the brackets
h[oa]t finds hot and hat, but not
hit
^
!
Represents any character not in
the brackets
h[^oa]t finds hit, but not hot and
hat
- Represents a range of characters c[a-f]t finds cat and cbt
12/21/2022 Database Systems 5
Wildcards: LIKE Operator
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that starts with "a"
WHERE CustomerName LIKE '%a' Finds any values that ends with "a"
WHERE CustomerName LIKE
'%or%'
Finds any values that have "or" in any
position
WHERE CustomerName LIKE
'_r%'
Finds any values that have "r" in the
second position
WHERE CustomerName LIKE
'a_%_%'
Finds any values that starts with "a" and
are at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that starts with "a" and
ends with "o"
12/21/2022 Database Systems 6
SQL IN Operator
⚫ The IN operator allows you to specify multiple
values in a WHERE clause.
⚫ The IN operator is a shorthand for multiple OR
conditions.
⚫ SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
⚫ SELECT *
FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
12/21/2022 Database Systems 7
SQL BETWEEN Operator
⚫ The BETWEEN operator selects values within a
given range. The values can be numbers, text, or
dates.
⚫ The BETWEEN operator is inclusive: begin and
end values are included.
⚫ SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
⚫ SELECT *
FROM Products
WHERE Price BETWEEN 10 AND 20;
12/21/2022 Database Systems 8
Next Session
⚫ Aggregate Functions
⚫ GROUP BY Clause
⚫ HAVING Clause
12/21/2022 Database Systems 9
Questions?
12/21/2022 Database Systems 10

Wildcard In database

  • 1.
    Database Systems CS –2102 Wildcards in SQL Week /Lab 8 Engr. Tahir Abbasi
  • 2.
    Quote of theDay!!! 12/21/2022 Database Systems 2
  • 3.
    Session’s Objectives ⚫ Atthe end of today’s session, you should have an understanding of: ⚫ Wildcards ⚫ IN Operator ⚫ BETWEEN Operator 12/21/2022 Database Systems 3
  • 4.
    WILDCARDS ⚫ A wildcardcharacter is used to substitute one or more characters in a string. ⚫ Wildcard characters are used with the SQL LIKE operator. ⚫ The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. 12/21/2022 Database Systems 4
  • 5.
    WILDCARDS Symbol Description Example %Represents zero or more characters bl% finds bl, black, blue, and blob _ Represents a single character h_t finds hot, hat, and hit [ ] Represents any single character within the brackets h[oa]t finds hot and hat, but not hit ^ ! Represents any character not in the brackets h[^oa]t finds hit, but not hot and hat - Represents a range of characters c[a-f]t finds cat and cbt 12/21/2022 Database Systems 5
  • 6.
    Wildcards: LIKE Operator LIKEOperator Description WHERE CustomerName LIKE 'a%' Finds any values that starts with "a" WHERE CustomerName LIKE '%a' Finds any values that ends with "a" WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position WHERE CustomerName LIKE 'a_%_%' Finds any values that starts with "a" and are at least 3 characters in length WHERE ContactName LIKE 'a%o' Finds any values that starts with "a" and ends with "o" 12/21/2022 Database Systems 6
  • 7.
    SQL IN Operator ⚫The IN operator allows you to specify multiple values in a WHERE clause. ⚫ The IN operator is a shorthand for multiple OR conditions. ⚫ SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2, ...); ⚫ SELECT * FROM Customers WHERE Country IN ('Germany', 'France', 'UK'); 12/21/2022 Database Systems 7
  • 8.
    SQL BETWEEN Operator ⚫The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. ⚫ The BETWEEN operator is inclusive: begin and end values are included. ⚫ SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2; ⚫ SELECT * FROM Products WHERE Price BETWEEN 10 AND 20; 12/21/2022 Database Systems 8
  • 9.
    Next Session ⚫ AggregateFunctions ⚫ GROUP BY Clause ⚫ HAVING Clause 12/21/2022 Database Systems 9
  • 10.