Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

postgresql
communitycreator

What are the like and limit clauses in PostgreSQL?

Hifza Sohail

The like clause in PostgreSQL

The like clause is used in PostgreSQL to compare and match wildcards and see if a matching pattern exists. If a pattern exists, the output is 1 (true).

When to use like

There are situations in which we remember the first or last letter of a word but cannot remember the exact word. Like comes to the rescue in such situations.

Like can be used in various situations such as:


The wildcards used with the Like Operator are % and _.


  • WHERE kind:: text LIKE %table: To search any text that ends with table.

  • WHERE kind:: text LIKE table%: To search any text that starts with table.

  • WHERE kind:: text LIKE %table%: To search any text with table anywhere in the text.

  • WHERE kind:: text LIKE %table: To search any text that starts with table.

  • WHERE kind:: text LIKE _table%: To search any text with table starting from the second position and any letters after the table.

The limit clause in PostgreSQL

The limit clause is not used very often. limit is used with the SELECT statement when the table has a large amount of data and we want to restrict the output.

Syntax


SELECT rowName
FROM tablename
LIMIT count

We can set a count with the limit to specify the number of rows to be displayed.

  • If the count is 0, then no rows are displayed.

  • If the count is NULL, all the rows are displayed, just like a simple SELECT statement.

Code


SELECT thriller
FROM movies
LIMIT 5

Explanation

The above example will display 5 rows from the movies table that are thrillers.

RELATED TAGS

postgresql
communitycreator
RELATED COURSES

View all Courses

Keep Exploring