PostgreSQL – LIKE Operator

LIKE operator is used in the WHERE clause to filter results by pattern matching. LIKE operator uses 2 wildcard options. ‘%’ sign to match any number of characters(0,1, or more) and ‘_’ to match exactly 1 character.

Syntax:

LIKE '%abc%abc%';

LIKE '_abc';

Example:

Consider an employee table as shown below:

idfirst_namelast_namedepartmentsalaryemail
101bryanwalkersales50,000bryan.walker@email.com
102alexcarpenteraccounts55,000alex.carpenter@email.com
103samwhitesales60,000sam.white@egmail.com
104johnbarrysales54,000john.barry@email.com
105billwoodaccounts65,000
106roberthopkinssupport70,000
107davidleesupport72,000david.lee@email.com
108jacksearssupport65,000
109tomyoungsales55,000tom.young@email.com

Below is the SQL query to identify employees with invalid email addresses: The logic used here is if the ’email’ value does not contain ‘@’ it is considered invalid.

SELECT * FROM employee WHERE email not like '%@%';

Below is the SQL query to identify all employees with their first names starting with ‘a’

SELECT * FROM employee WHERE first_name like 'a%';

Below is the SQL query to identify all employees that have second character as ‘o’ in their first name.

SELECT * FROM employee WHERE first_name like '_o%';

See also  PostgreSQL - List installed extensions