What if you wanted only students whose first name ended with a “k”? Simple, just swap the letter and the wildcard: SELECT id, first_name, last_name, email FROM students WHERE first_name LIKE '%k' Code language: SQL (Structured Query Language) ( sql )Īnother example: let’s match students whose email addresses contain the word “gmail”: SELECT id, first_name, last_name, email FROM students WHERE email LIKE '%gmail%' Code language: SQL (Structured Query Language) ( sql ) The percentage character (%) is used as a wildcard to match any number of characters.It goes on the same spot you’d put any comparison operator such as “=”, “>”, “You use the LIKE operator in the WHERE clause of a query.There are some interesting points to notice from this first example: The query above retrieves some columns from the students table, but only the rows which match the condition: the value from the last_name column has to start with the letter “M”. So, let’s see a real query that does just that using PostgreSQL LIKE: SELECT id, first_name, last_name, email FROM students WHERE last_name LIKE 'M%' Code language: SQL (Structured Query Language) ( sql ) In the introduction, I gave an example: retrieving students whose last name starts with a given letter. You use LIKE when you want to retrieve rows in which one or more of their textual columns match a given pattern. PostgreSQL LIKE is PostgreSQL’s implementation of the LIKE operator from the SQL language. With the requirements out of the way, let’s start by covering some fundamentals about today’s topic. Instead of actually installing PostgreSQL, I recommend using the CoderPad MySQL Sandbox to quickly and easily get started writing SQL as it’ll be your MySQL client for this article.
0 Comments
Leave a Reply. |