Pattern Matching Tutorial
ThisPost/Pagemaycontainaffiliate links,meaningwhenyouclickthelinks andmakeapurchase,wereceivea commission.

TheSQLLIKEOperatorforPatternMatching
Likeitornot,the LIKE operatorisessentialinSQL.Itgivesdatapractitionersthepowertofilter dataonspecificstringmatches.Thisarticleprovidesaquicktutorialon LIKE forbeginnersand intermediates.Ifyoulearnbetterwithhands-onpractice,youcanalsofollowalong(andrunthe code)onthisDataCampWorkspace
Learn SQL Commands —25 Most Popular SQL Commands
Learn SQL Commands 25 Most Popular SQL Commands

Learn the basics of the 25 most popular SQL commands and how to use them What is SQL? SQL


Learn Power BI Calculate function Tutorial
Learn Power BI Calculate function Tutorial Learn how to use the Power BI CALCULATE function while giving examples of how you can use it The

SQL Joins Cheat Sheet
SQL Joins Cheat Sheet
With this SQL Joins cheat sheet, you'll have a handy reference guide to joining data in SQL SQL, also known as St


21 Essential Tools for Python
21 Essential Tools for Python Learn about the essential Python tools for software development, web scraping and development, data analysis a
Creating Synthetic Data with Python Faker Tutorial Creating Synthetic Data with Python Faker Tutorial Generating synthetic data using Python Faker to supplement real world data for applicatio

What is R? The Statistical Computing base for data science
What is R? The Statistical Computing base for data science Learn the history of R programming language and discover why it is the most wid
Certi ed CyberOps
CBROPS 200

employees


emp_no birth_d ate first_na me last_na me gender hire_dat e
10001
19530902T00:0 0:00.000 Z
Georgi Facello M

19860626T00:0 0:00.000 Z
10002
19640602T00:0 0:00.000 Z
Bezalel Simmel F
1985-1121T00:0 0:00.000 Z
10003
19591203T00:0 0:00.000 Z
Parto Bamford M
19860828T00:0 0:00.000 Z
Supposeyouhavean employees tableandwouldliketofindallnamesthatstartwith ‘A’ .You couldspendtimelookingthroughthetablemanually.Butwhywouldyoudothatwhenyouhave theLIKEoperator?
Code
SELECTDISTINCT first_name FROMemployees WHEREfirst_nameLIKE'A%'
Themagichereisintheclause `WHERE first_name LIKE ‘A%’` ,whichmeans“find all first_name startingwithAandendingwithanynumberofcharacters.”The `A%` hereis knownasapatternformatching.
The `%` isnottheonlywildcardyoucanuseinconjunctionwiththe LIKE operator.Youcould usetheunderscoresign `_` too.
`%` matchesanynumberofcharacters.
`_` matchesanysinglecharacter
Youcanuse LIKE toachieveavarietyofpattern-matching.Here’show
BeginnerSQLLIKEExamples
Below,we’veoutlinedsomepracticalexamplesofhowyoucanusethe LIKE statementandthe resultsfromoursampledataset.
1.Use LIKE forExactStringMatch
Ifyou’dliketoperformanexactstringmatch,useLIKEwithout ‘%’ or ‘_’
Code SELECT
first_name,last_name FROMemployees
WHEREfirst_nameLIKE'Barry'--thesameasWHEREfirst_name=‘Barry’
2.Use ‘%’ tomatchanynumberofcharacters
‘%’ canbeusedtomatchany(evenzero)numberofcharacters–anumber,analphabet,ora symbol.
Supposeyouwanttofindallemployeeswhosenamestartswith ‘Adam’ ;youcanusethe pattern ‘Adam%’


SELECTDISTINCT first_name FROMemployees
first_nameLIKE'Adam%'
Tofindnamesthatendwith ’Z’ ,trythepattern ‘%z’ .Youcanalsousemultiple ‘%’ inone pattern.Forexample,ifyouwanttofindnamesthatcontainz,use ‘%z%’
Like
game
the
onlyfitonecharacter
SELECTDISTINCT first_name FROMemployees
first_name
'___'
4.Useboth ‘%’ and ‘_’ tomatchanypattern
Code
SELECTDISTINCT first_name FROMemployees WHEREfirst_nameLIKE'%ann_'

Thepattern ‘%ann_’
5.Use NOT tofindstringsthatdonotmatchapattern
C Sharp
& Swift
Introduction to Journalism and Reporting
Introduction to Databases and SQL

How to Program in C++
Server 2008 Complete Video Training
Corel Presentations
Application Training
Cisco CCNP Implementing Cisco
Switched
August (72)
July (6)
June (1)
May (3)
April (11)
March (2)
February (6)
January (4)
2021 (31)
(6)
(19)
(54)
Thisisexactlyequivalenttothesyntax
WHEREtitle!=‘Staff’
SELECTDISTINCT title FROMtitles
titleNOTLIKE'Staff'
Ofcourse,youcanuse `NOT LIKE` withanyofthepatternswedescribed.
SELECTDISTINCT title FROMtitles
titleNOTLIKE'%engineer'
6.Use LOWER (or UPPER )with LIKE forcase-insensitive patternmatching Code
Ifyouneedtoperformpatternmatchingbutaren’tsureifthestringisstoredinlowercase, uppercase,ormixedcase,youcanusethefollowingsyntax.
`LOWER(column_name)LIKEpattern`
Thefunction LOWER() returnsallstringsinlowercase,regardlessofwhethertheyarestoredas upper-,lower-ormixedcase.
Whenusingthesyntax,makesureyouspellthepatterninalllowercase!Else,youmightnotget anymatches.
Data Science Fundamentals

Python and SQL
Youcouldreplace LOWER with UPPER inthesyntaxabovetoo.Besuretospelloutthepatternin CAPITALLETTERS.
Code
UPPER(column_name)LIKEPATTERN

POWERED BY DATACAMP WORKSPACE
Forexample,tofindoutifanemployee’snameisJoanne,JoAnne,Joanna,orJoAnna,tryeither ofthefollowing.
Code
SELECTDISTINCT first_name FROMemployees
WHERElower(first_name)LIKE'joann_'
POWERED BY DATACAMP WORKSPACE
Code
SELECTDISTINCT first_name FROMemployees
WHEREUPPER(first_name)LIKE'JOANN_'
POWERED BY DATACAMP WORKSPACE
7.SQL LIKE withMultipleValuesUsing OR/AND
Youcancombinemultipleconditionsusingthe LIKE syntaxtoo.
Forexample,usethe OR conditiontofindresultsthatsatisfyatleastoneoutof multiple LIKE patterns.
Code
SELECTDISTINCT first_name FROMemployees WHERE first_nameLIKE'Ad_l'OR first_nameLIKE'Ad_m'
CODE
CODE
Ontheotherhand,tofindastringthatmatchesmultiple LIKE conditions,usethe AND keyword.
SELECTDISTINCT first_name FROMemployees WHERE first_nameLIKE'%am%'AND first_nameLIKE'%me%'
The LIKE syntaxcanbeappliedtomultiplecolumns,aslongastheirvariabletypeisavariablelengthcharacter( varchar ).Knowingthatwecanfindoutthenamesofemployeeswhoseinitials are ‘Z. Z.’
SELECTDISTINCT first_name,last_name FROMemployees WHERE first_nameLIKE'Z%'AND last_nameLIKE'Z%'

8.Use LIKE inthe SELECT CASE WHEN clause


Thusfar,wehavefocusedonusing LIKE asaconditionforselectingrecordsin the WHERE clause.Wealsouse LIKE inthe SELECT clausetoo.Forexample,canwefindout howmanyemployeeswiththename ‘Adam’ areinourdatabase?

SELECT
COUNT(CASEWHENfirst_nameLIKE'Adam'THEN1END)num_employees_adam FROMemployees
Ontheotherhand,howmanyemployeeshavetheinitials ‘A.Z.’ ?
SELECT
COUNT(CASEWHENfirst_nameLIKE'A%'ANDlast_nameLIKE'Z%'THEN1END)num_employees FROMemployees
IntermediateExamplesofSQLLIKE
The LIKE functionislargelysimilaracrossdifferentflavorsofSQL(e.g.PostgreSQL,MySQL, Redshift,etc.).Somehaveadditionalvariationsofthe LIKE functionthatareworthmentioning.
1.The ILIKE operator
AvailableinRedshiftandPostgreSQL, ILIKE isthecase-insensitiveversionof LIKE .Assuch, allofthefollowingareequivalent.
Code
SELECT
datacampILIKE‘datacamp’,--returnsTRUE DATACAMPILIKE‘datacamp’,--returnsTRUE DatacampILIKE‘datacamp’,--returnsTRUE datacampILIKE‘DataCamp’,--returnsTRUE
2.Usingsquarebrackets [] and [^] aswildcardcharacters
UsersofT-SQLorSQLServerhaveadditionalwildcardcharactersformorecomplexpattern matching.
Thesquarebracketsyntax [] matchesanysinglecharacterspecificwithintherangeorset.For example,thefollowingwillallreturnTRUE.
Code
SELECT
‘Carson’LIKE‘[CK]arson’,--returnsTRUEbecauseCisintherangeC-K ‘Karson’LIKE‘[CK]arson’,--returnsTRUEbecauseKisinrange ‘Larson’LIKE‘[CKL]arson’,--returnsTRUEbecauseLisintheset[CKL] ‘Parson’LIKE‘[CK]arson’--returnsFALSEbecausePisoutofrange
3.The RLIKE operator




UseSQLLIKEConfidently
LearnmoreaboutSQL
NickCarchedi CourseInstructor


