{"id":33094,"date":"2023-04-17T02:08:36","date_gmt":"2023-04-17T09:08:36","guid":{"rendered":"https:\/\/coderpad.io\/?p=33094"},"modified":"2023-06-07T13:22:34","modified_gmt":"2023-06-07T20:22:34","slug":"postgresql-like-operator-a-detailed-guide","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/postgresql-like-operator-a-detailed-guide\/","title":{"rendered":"PostgreSQL LIKE Operator: A Detailed Guide"},"content":{"rendered":"\n<p>When using databases, searching for values that match a given pattern is a familiar necessity. You might want to retrieve all students whose last name starts with a given letter or update all products whose ids include a particular string. If you&#8217;re a PostgreSQL user, you will use the PostgreSQL <code>LIKE<\/code> operator for that.<\/p>\n\n\n\n<p>If you want to learn more about PostgreSQL <code>LIKE<\/code>, you&#8217;ve come to the right place, since this post is all about this operator. By the end of the post, you&#8217;ll have learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>what PostgreSQL <code>LIKE<\/code> does, and why you&#8217;d want to use it<\/li>\n\n\n\n<li>how it differs from the <code>LIKE<\/code> operators in different database engines<\/li>\n\n\n\n<li>how it differs from the <code>ILIKE<\/code> operator<\/li>\n<\/ul>\n\n\n\n<p>To paraphrase that famous movie, though, only theory and no practice make this a dull post. So, before wrapping up, we&#8217;ll walk you through several examples of the <code>LIKE<\/code> operator in practice.<\/p>\n\n\n\n<p><strong>Requirements<\/strong><\/p>\n\n\n\n<p>If you want to follow along with the practical portion of the post, keep in mind that the post assumes the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>there is a functional instance of PostgreSQL installed on your machine<\/li>\n\n\n\n<li>you can connect to the said instance through a client<\/li>\n\n\n\n<li>you have at least a bit of experience with the PostgreSQL database and the SQL language<\/li>\n<\/ul>\n\n\n\n<p>Instead of actually installing PostgreSQL, &nbsp;I recommend using the<a href=\"https:\/\/hub.docker.com\/_\/mysql\" target=\"_blank\" rel=\"noopener\"> <\/a><a href=\"https:\/\/coderpad.io\/languages\/mysql\/\">CoderPad MySQL Sandbox<\/a> to quickly and easily get started writing SQL&nbsp; as it\u2019ll be your MySQL client for this article.<\/p>\n\n\n\n<p><strong>PostgreSQL <code>LIKE<\/code>: The Fundamentals<\/strong><\/p>\n\n\n\n<p>With the requirements out of the way, let&#8217;s start by covering some fundamentals about today&#8217;s topic.<\/p>\n\n\n\n<p><strong>What Is PostgreSQL <code>LIKE<\/code>?<\/strong><\/p>\n\n\n\n<p>PostgreSQL <code>LIKE<\/code> is PostgreSQL&#8217;s implementation of the <code>LIKE<\/code> operator from the SQL language. You use LIKE when you want to retrieve rows in which one or more of their textual columns match a given pattern. In the introduction, I gave an example: retrieving students whose last name starts with a given letter. So, let&#8217;s see a real query that does just that using PostgreSQL <code>LIKE<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">id<\/span>, first_name, last_name, email <span class=\"hljs-keyword\">FROM<\/span> students <span class=\"hljs-keyword\">WHERE<\/span> last_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'M%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The query above retrieves some columns from the <code>students<\/code> table, but only the rows which match the condition: the value from the <code>last_name<\/code> column has to start with the letter &#8220;M&#8221;. There are some interesting points to notice from this first example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You use the <code>LIKE<\/code> operator in the <code><a href=\"https:\/\/coderpad.io\/blog\/development\/sql-functions-and-techniques-every-data-person-should-know\/\">WHERE<\/a><\/code> clause of a query<\/li>\n\n\n\n<li>It goes on the same spot you&#8217;d put any comparison operator such as &#8220;==&#8221;, &#8220;&gt;&#8221;, &#8220;&lt;&#8220;, and so on<\/li>\n\n\n\n<li>The percentage character (%) is used as a wildcard to match any number of characters<\/li>\n<\/ul>\n\n\n\n<p>What if you wanted only students whose first name ended with a &#8220;k&#8221;? Simple, just swap the letter and the wildcard:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">id<\/span>, first_name, last_name, email <span class=\"hljs-keyword\">FROM<\/span> students <span class=\"hljs-keyword\">WHERE<\/span> first_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%k'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Another example: let&#8217;s match students whose email addresses contain the word &#8220;gmail&#8221;:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">id<\/span>, first_name, last_name, email <span class=\"hljs-keyword\">FROM<\/span> students <span class=\"hljs-keyword\">WHERE<\/span> email <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%gmail%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>That&#8217;s right: if you want to match values that contain a given string, you surround the string with percentage signs (<code>%<\/code>).<\/p>\n\n\n\n<p><strong>What Is the Difference Between LIKE and ILIKE in Postgresql?<\/strong><\/p>\n\n\n\n<p>When reviewing PostgreSQL code in the wild, you might encounter queries that use the <code>ILIKE<\/code> operator instead of <code>LIKE<\/code> and get confused. The difference is that the <code>LIKE<\/code> operator is case sensitive\u2014i.e. it distinguishes between lowercase and uppercase letters. On the other hand, the <code>ILIKE<\/code> operator is case <em>insensitive<\/em>\u2014hence the i. It does not distinguish between upper and lowercase letters, and therefore you should use it when you don&#8217;t care about the case.<\/p>\n\n\n\n<p>An important thing to keep in mind: the <code>ILIKE<\/code> operator doesn&#8217;t exist in ANSI SQL; instead, it&#8217;s a specific extension of PostgreSQL. That means that employing ILIKE makes your SQL code less portable, in case you have the need to change your database engine.<\/p>\n\n\n\n<p><strong>PostgreSQL Like: Practical Use Cases<\/strong><\/p>\n\n\n\n<p>Having covered the basics of PostgreSQL <code>LIKE<\/code>, let&#8217;s now see several usage examples. We&#8217;ll begin by preparing the database for the tests.<\/p>\n\n\n\n<p><strong>Preparing the Database<\/strong><\/p>\n\n\n\n<p>Using your preferred client, connect to your PostgreSQL instance, making sure there&#8217;s a database you can connect to for the tests.<\/p>\n\n\n\n<p>Let&#8217;s start by creating a table and inserting some rows into it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> albums (\n  <span class=\"hljs-keyword\">id<\/span>              <span class=\"hljs-built_in\">SERIAL<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n  title           <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">250<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  artist  \t\t  <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">250<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  release_year <span class=\"hljs-built_in\">int<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> albums (title, artist, release_year) <span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Abbey Road'<\/span>, <span class=\"hljs-string\">'The Beatles'<\/span>, <span class=\"hljs-number\">1969<\/span>);\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> albums  (title, artist, release_year) <span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Constru\u00e7\u00e3o'<\/span>, <span class=\"hljs-string\">'Chico Buarque'<\/span>, <span class=\"hljs-number\">1971<\/span>);\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> albums  (title, artist, release_year) <span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'The Dark Side of the Moon'<\/span>, <span class=\"hljs-string\">'Pink Floyd'<\/span>, <span class=\"hljs-number\">1973<\/span>);\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> albums  (title, artist, release_year) <span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Reckless'<\/span>, <span class=\"hljs-string\">'Bryan Adams'<\/span>, <span class=\"hljs-number\">1984<\/span>);\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> albums  (title, artist, release_year) <span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Ryan Adams'<\/span>, <span class=\"hljs-string\">'Ryan Adams'<\/span>, <span class=\"hljs-number\">2014<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Matching a Single Character<\/strong><\/p>\n\n\n\n<p>As you&#8217;ve seen, the table now contains albums by both Bryan and Ryan Adams. That&#8217;s not by coincidence. First, use the following query to retrieve albums by the two singers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> albums <span class=\"hljs-keyword\">WHERE<\/span> artist <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%yan%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The query above matches all artists which contain the string &#8220;yan&#8221; anywhere in their name. Here&#8217;s what the result looks like to me:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"> id |   title    |   artist    | release_year \n<span class=\"hljs-comment\">----+------------+-------------+--------------<\/span>\n  4 | Reckless   | Bryan Adams |         1984\n  5 | Ryan Adams | Ryan Adams  |         2014\n(2 rows)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>LIKE<\/code> operator offers an additional wildcard, the underscore character (_) which matches a single character. That way, it&#8217;s possible to rewrite the query in such a way that only Ryan Adams is returned:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> albums <span class=\"hljs-keyword\">WHERE<\/span> artist <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'_yan%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And here, again, is the result:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"> id |   title    |   artist   | release_year \n<span class=\"hljs-comment\">----+------------+------------+--------------<\/span>\n  5 | Ryan Adams | Ryan Adams |         2014\n(1 row)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Negative Matches<\/strong><\/p>\n\n\n\n<p>When working with database tables, you often need to retrieve data that doesn&#8217;t match a given pattern. For that, you can use the <code>NOT LIKE<\/code> operator. For instance, let&#8217;s get only artists that don&#8217;t start with &#8220;The&#8221;:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">SELECT * FROM albums WHERE artist NOT LIKE <span class=\"hljs-string\">'The%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the only artist starting with &#8220;The&#8221; are The Beatles, they won&#8217;t be fetched by the query above.<\/p>\n\n\n\n<p><strong>Matching Without Case Sensitivity<\/strong><\/p>\n\n\n\n<p>As mentioned earlier, the <code>ILIKE<\/code> operator is a special PostgreSQL extension to <code>LIKE<\/code> that matches in a case-insensitive way. Let&#8217;s see that in practice. First, let&#8217;s use the <code>LIKE<\/code> operator to get all artists which contain an uppercase &#8220;A&#8221;:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> albums <span class=\"hljs-keyword\">WHERE<\/span> artist <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%A%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Only &#8220;Bryan Adams&#8221; and &#8220;Ryan Adams&#8221; are returned, which makes sense:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"> id |   title    |   artist    | release_year \n<span class=\"hljs-comment\">----+------------+-------------+--------------<\/span>\n  4 | Reckless   | Bryan Adams |         1984\n  5 | Ryan Adams | Ryan Adams  |         2014\n(2 rows)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Let&#8217;s now replace the <code>LIKE<\/code> operator with the <code>ILIKE<\/code> one and rerun the query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">SELECT * FROM albums WHERE artist ILIKE <span class=\"hljs-string\">'%A%'<\/span>;\n\n id |   title    |    artist     | release_year \n----+------------+---------------+--------------\n  <span class=\"hljs-number\">1<\/span> | Abbey Road | The Beatles   |         <span class=\"hljs-number\">1969<\/span>\n  <span class=\"hljs-number\">2<\/span> | Constru\u00e7\u00e3o | Chico Buarque |         <span class=\"hljs-number\">1971<\/span>\n  <span class=\"hljs-number\">4<\/span> | Reckless   | Bryan Adams   |         <span class=\"hljs-number\">1984<\/span>\n  <span class=\"hljs-number\">5<\/span> | Ryan Adams | Ryan Adams    |         <span class=\"hljs-number\">2014<\/span>\n(<span class=\"hljs-number\">4<\/span> rows)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, all artists are returned, except for Pink Floyd.<\/p>\n\n\n\n<p><strong>Escaping the WildCards<\/strong><\/p>\n\n\n\n<p>For this example, let&#8217;s update two rows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">UPDATE<\/span> albums <span class=\"hljs-keyword\">SET<\/span> artist = <span class=\"hljs-string\">'The%Beatles'<\/span> <span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-keyword\">id<\/span> = <span class=\"hljs-number\">1<\/span>;\n\n<span class=\"hljs-keyword\">UPDATE<\/span> albums <span class=\"hljs-keyword\">SET<\/span> artist = <span class=\"hljs-string\">'Chico_Buarque'<\/span> <span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-keyword\">id<\/span> = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, let&#8217;s say we want to retrieve albums from artists whose name contains a percentage sign or an underscore:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> albums <span class=\"hljs-keyword\">WHERE<\/span> artist <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%%%'<\/span>;\n\n<span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> albums <span class=\"hljs-keyword\">WHERE<\/span> artist <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%_%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As you&#8217;ve probably imagined, both queries don&#8217;t work: they return all rows. Fortunately, there&#8217;s a way around this problem: it&#8217;s possible to escape a wildcard so we can use it as a normal character. You simply put a backslash before the offending character:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> albums <span class=\"hljs-keyword\">WHERE<\/span> artist <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%\\%%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And here&#8217;s the result:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"> id |   title    |    artist     | release_year \n<span class=\"hljs-comment\">----+------------+---------------+--------------<\/span>\n  1 | Abbey Road | The Beatles   |         1969\n(1 rows)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The same would work for the underscore character. But before wrapping up, let&#8217;s complicate things just a bit more\u2014because, why not?<\/p>\n\n\n\n<p>First, let&#8217;s update the &#8220;Abbey Road&#8221; row once more:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">UPDATE<\/span> albums <span class=\"hljs-keyword\">SET<\/span> artist = <span class=\"hljs-string\">'The\\Beatles'<\/span> <span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-keyword\">id<\/span> = <span class=\"hljs-number\">1<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, in order to use LIKE to retrieve a row containing the backslash character, you&#8217;d have to write this query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">SELECT * FROM albums WHERE artist LIKE <span class=\"hljs-string\">'%\\\\%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You have to escape the escape character. It&#8217;s not that terrible, but you might want to write a more readable version. If that&#8217;s the case, it&#8217;s possible to choose a different character as the escape character: just use the <code>ESCAPE<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">SELECT * FROM albums WHERE artist LIKE <span class=\"hljs-string\">'%$\\%'<\/span> ESCAPE <span class=\"hljs-string\">'$'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>With the <code>ESCAPE<\/code> clause, you can choose a different character as the escape character. That way\u2014at least in my opinion\u2014the resulting query is more explicit in the way it escapes the wildcard.<\/p>\n\n\n\n<p>Experiment with the <code>LIKE<\/code> operator in the sandbox below:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=247535&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<p><strong>Conclusion: PostgreSQL LIKE: Learn It, Leverage It<\/strong><\/p>\n\n\n\n<p>In this post, you&#8217;ve learned about the LIKE operator in PostgreSQL: what it is, what&#8217;s used for, and how it works along several examples. As you&#8217;ve seen, the operator&#8217;s working is quite easy once you understand how the wildcards work. We&#8217;ve also walked you through some usage examples, like matching against a single character and performing negative matches.<\/p>\n\n\n\n<p>Where should you go now?&nbsp; For starters, continue to learn about pattern matching on PostgreSQL. Here are some topic suggestions for you:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>SIMILAR TO<\/code>, a more recent clause<\/li>\n\n\n\n<li>the <code>substring()<\/code> function<\/li>\n\n\n\n<li>the <code>regexp_like()<\/code> function<\/li>\n<\/ul>\n\n\n\n<p>Additionally, the site you&#8217;re on right now has great resources for people who want to learn more not only about PostgreSQL but databases in general. Here are some posts that are worth your time:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/postgresql-system-catalogs-metadata\/\">How to Get Metadata from PostgreSQL System Catalogs<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/window-functions-aggregate-data-postgres\/\">Why &amp; How to Use Window Functions to Aggregate Data in Postgres<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/sqlalchemy-with-postgresql\/\">How to Interact with Databases using SQLAlchemy with PostgreSQL<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/sql-functions-and-techniques-every-data-person-should-know\/\">SQL Functions and Techniques Every Data Person Should Know<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading, and until next time!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using databases, searching for values that match a given pattern is a familiar necessity. Learn how you can retrieve entries with queries including a particular string using Postgres LIKE operator.<\/p>\n","protected":false},"author":1,"featured_media":33126,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[67],"keyword-cluster":[],"class_list":["post-33094","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/33094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=33094"}],"version-history":[{"count":37,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/33094\/revisions"}],"predecessor-version":[{"id":34533,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/33094\/revisions\/34533"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/33126"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=33094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=33094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=33094"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=33094"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=33094"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=33094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}