SQL Like (2024)

What is the LIKE statement in SQL?

The SQL LIKE operator is used to search for a specific pattern in a string value. It is often used in the WHERE clause of a SELECT, UPDATE, or DELETE statement.

You can use two wildcard characters to help you define what you are looking for in a database. The percent sign (%) matches any number of characters, and the underscore (_) corresponds to a single character.

The following SQL statement exemplifies how the LIKE operator returns the rows from the tv_show table where the GENRE column starts with “Thriller”.

SELECT *FROM SANDBOX.tv_showWHERE GENRE LIKE 'Thriller%';

To understand the difference, compare the result when we used “=” instead of LIKE.

SQL LIKE allows you to search for data that matches a specific pattern. It is handy when you only know partially the records you are searching because it helps you perform a flexible search that considers variations in the data.

Case sensitivity varies depending on the specific database platform and configuration. (Consult the documentation for case sensitivity behavior within your specific database platform. See notes below for LIKE case sensitivity within RedShift, BigQuery, and Snowflake.)

Searching for a specific string:
SELECT *FROM SANDBOX.tv_showWHERE SHOW_NAME LIKE 'The Office';

Note that without the ‘%’ wildcard character, LIKE is equivalent to using the equals operator.

Searching for a pattern:
SELECT *FROM SANDBOX.tv_showWHERE SHOW_NAME LIKE 'The%';
Searching for records that contain a specific word pattern:
SELECT *FROM SANDBOX.tv_showWHERE SHOW_NAME LIKE '%Twil%';
Searching for records that match multiple conditions:
SELECT *FROM SANDBOX.tv_show WHERE SHOW_NAME LIKE 'Th%' AND GENRE LIKE 'Doc%';

Note that this one helps you find results with typos. You can combine it with other operators and wildcard characters to perform more complex searches and find patterns in your data.

Case Sensitivity

The case sensitivity of the LIKE operator can vary depending on the specific database platform and its configuration. Here’s an overview of the behavior of the LIKE operator in Redshift, BigQuery, and Snowflake:

  • Redshift: By default, the LIKE operator is case-sensitive in Redshift. However, you can use the ILIKE operator instead of LIKE to perform a case-insensitive search.
  • BigQuery: The LIKE operator is case-sensitive in BigQuery. However, you can use the REGEXP_CONTAINS function to perform a case-insensitive search using regular expressions.
  • Snowflake: The behavior of the LIKE operator in Snowflake is determined by the case sensitivity of the collation of the column being searched. By default, the collation is case-insensitive, so the LIKE operator will perform a case-insensitive search. However, if the column has a case-sensitive collation, the LIKE operator will be case-sensitive. You can also use the ILIKE operator to explicitly perform a case-insensitive search, regardless of the column’s collation.

It’s always a good idea to consult the documentation for your specific database platform to understand its behavior regarding case sensitivity and the LIKE operator.

  • SQL Like (1)
  • SQL Like (2)
SQL Like (2024)

FAQs

What is like %% in SQL? ›

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign % represents zero, one, or multiple characters.

What is like '%[ 0 9 ]%' in SQL? ›

LIKE operator as you have used before: LIKE '%[^0-9]%'. This will find rows that are NOT all digits, but won't catch NULL or empty, which may or may not be acceptable so you might need additional WHERE clauses.

Is like in SQL inefficient? ›

If you have a large table with many rows and columns, using the LIKE operator can be inefficient and time-consuming. To speed up your query, you can use indexes and full-text search features that are available in most database systems.

How do you escape a SQL like query? ›

ESCAPE Clause Example

SELECT last_name FROM employees WHERE last_name LIKE '%A\_B%' ESCAPE '\'; The ESCAPE clause identifies the backslash (\) as the escape character. In the pattern, the escape character precedes the underscore (_).

What does %% mean in SQL like? ›

'% %' means you are searching a String in a query with a " " space in any position. If the String don't have a space , It will not return anything. If you want to search more about SQL Wildcard Operators, Just click the link.

What does %s do in SQL? ›

%s is a placeholder used in functions like sprintf. Check the manual for other possible placeholders. $sql = sprintf($sql, "Test"); This would replace %s with the string "Test".

What is the difference between %SQL and %% SQL? ›

The difference between the two %sql commands is that %sql only allows for a single statement to be executed, while the %%sql allows a block of SQL to be executed.

How to use like in SQL for numbers? ›

Use the % wildcard character

If the LIKE '5%' symbol is specified, the Database Engine searches for the number 5 followed by any string of zero or more characters. For example, the following query shows all dynamic management views in the AdventureWorks2022 database, because they all start with the letters dm .

How do I optimize a like query in SQL? ›

There is no way to optimise the query without making changes to the structure, a LIKE filter like that is always going to result in an index or table scan. You might get a small improvement with a index over just Order_Description though it would only be a very small difference.

Is SQL becoming less popular? ›

While SQL's elder sibling COBOL clings to life on the mainframe, SQL remains one of the most in-demand skills in the industry, according to the TIOBE index. In the 20 years since 2004 it's gone from 7th to 8th most popular language along side mainstays like C and upstarts like C++, Java, and Visual Basic.

How to avoid wildcard in SQL? ›

Escaping wildcard operators

In SQL, escaping a wildcard character is used to indicate that a wildcard character should be treated as a literal character rather than as a wildcard one. To escape a character we usually use the backslash ' \ ', but it may be different depending on the specific SQL database you are using.

What is square brackets in like SQL? ›

The square bracket syntax [] matches any single character specific within the range or set. For example, the following will all return TRUE. The caret-in-square-bracket [^] wildcard matches any single character that is not within the specified range or set.

What is like contains in SQL? ›

The LIKE vs CONTAINS SQL Server comparison boils down to a few facts: LIKE is a pattern-matching operator used for simple string matching, while CONTAINS is used for more advanced full-text searching.

How to use '%' in SQL? ›

Use the % wildcard character

If the LIKE '5%' symbol is specified, the Database Engine searches for the number 5 followed by any string of zero or more characters. For example, the following query shows all dynamic management views in the AdventureWorks2022 database, because they all start with the letters dm .

Why %S is used in SQL? ›

The %s format specifier is used with the % operator to perform string formatting in python. It allows you to format a value inside a string by replacing the %s with the value passed as an argument. The %s format specifier can also be used to insert values into SQL queries.

What is the difference between %% and %/%? ›

Modulus, %% , which returns the remainder of one number divided by another. Integer Division, %/% , which returns the integer quotient of two numbers.

Top Articles
Who Owns Apple?
Who Owns The Most SOL? | SOL Holders
Golden Abyss - Chapter 5 - Lunar_Angel
Sprinter Tyrone's Unblocked Games
Erika Kullberg Wikipedia
Overnight Cleaner Jobs
Davante Adams Wikipedia
Urinevlekken verwijderen: De meest effectieve methoden - Puurlv
O'reilly's Auto Parts Closest To My Location
TS-Optics ToupTek Color Astro Camera 2600CP Sony IMX571 Sensor D=28.3 mm-TS2600CP
D10 Wrestling Facebook
Connect U Of M Dearborn
Katherine Croan Ewald
Best Uf Sororities
Healthier Homes | Coronavirus Protocol | Stanley Steemer - Stanley Steemer | The Steem Team
Teacup Yorkie For Sale Up To $400 In South Carolina
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Barber Gym Quantico Hours
Wbiw Weather Watchers
Ford F-350 Models Trim Levels and Packages
Xfinity Outage Map Fredericksburg Va
Best Sports Bars In Schaumburg Il
Boise Craigslist Cars And Trucks - By Owner
Lexus Credit Card Login
Times Narcos Lied To You About What Really Happened - Grunge
4.231 Rounded To The Nearest Hundred
Florence Y'alls Standings
Used 2 Seater Go Karts
Warn Notice Va
Gasbuddy Lenoir Nc
Atlantic Broadband Email Login Pronto
Junee Warehouse | Imamother
Cvb Location Code Lookup
Edict Of Force Poe
Are you ready for some football? Zag Alum Justin Lange Forges Career in NFL
Instafeet Login
Gets Less Antsy Crossword Clue
Tokyo Spa Memphis Reviews
Felix Mallard Lpsg
11301 Lakeline Blvd Parkline Plaza Ctr Ste 150
Entry of the Globbots - 20th Century Electro​-​Synthesis, Avant Garde & Experimental Music 02;31,​07 - Volume II, by Various
Cpmc Mission Bernal Campus & Orthopedic Institute Photos
Pain Out Maxx Kratom
Rs3 Nature Spirit Quick Guide
Hk Jockey Club Result
Avatar: The Way Of Water Showtimes Near Jasper 8 Theatres
Okta Login Nordstrom
Barber Gym Quantico Hours
Fallout 76 Fox Locations
Craigslist Cars And Trucks For Sale By Owner Indianapolis
Factorio Green Circuit Setup
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 5563

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.