Finding SQL Rows With Similar Values
Databases
05/07/2021
Let me first clarify what I mean by similar values. Imagine the following Users
table containing emails and names:
Id | Name | |
---|---|---|
1 | jack@gmail.com | Jack |
2 | mike@gmail.com | Mike |
3 | anna@yahoo.com | Anna |
4 | stan@qq.com | Stan |
5 | sara@gmail.com | Sara |
How would we go about retrieving all Gmail emails? Allow me to introduce you to LIKE
. 🤗 This command enables you to perform some basic pattern matching.
SQL
SELECT * FROM Users WHERE Email LIKE '%gmail%';
Outputs:
Id | Name | |
---|---|---|
1 | jack@gmail.com | Jack |
2 | mike@gmail.com | Mike |
5 | sara@gmail.com | Sara |
After LIKE
, simply enter the common value you'd like to find.
%
serve as a catch-all symbol, meaning it will match any character. For instance, %gmail
will only match with values that has any kind and any amount of characters preceding the word gmail, but none following it.