How to use LIMIT, BETWEEN, IN, LIKE and ILIKE? SQL Part 5

Limit:

LIMIT Functions helps us receiving the limited results. For example: if there are 1000 rows but if you want only top 50 rows than we can use the LIMIT function.

If in the above query if we want only 2 results than:

SELECT * FROM Table 1

ORDER BY C3 DESC, C2 ASC

LIMIT 2;

Result:

Table 1
C1C2C3
B2Pencil
C4Pencil

*Limit can also be used to get a general understanding of a table by using the commant:

SELECT * FROM Table

LIMIT 1;

Try this yourself on SQL dvdrental database:

Challenge 1:

We want to reward our first 10 paying customers. What are the customer ids of the first 10 customers who created a payment.

Challenge 2:

A customer wants to quickly rent a video to watch over their short lunch break. What are the titles of the shortest (in length of runtime) movies?

BETWEEN:

BETWEEN operator can be used to match a value according to a range of values. For example “BETWEEN 2 AND 100”. 

The above statement can also be written by using where statement:

WHERE N>2 AND N<100

BETWEEN can also be used for time period. For example: “BETWEEN 2022-01-01 AND 2022-05-30” 

IN:

IN operator helps us in creating a condition to check some specific value. For example: “ ‘Saurabh’ IN Table” 

Example:

Table 1
C1C2C3
A3Pen
B2Pencil
C2Pencil
D5Pen
E6Pen

If we want rows which have values only having value of 2 and 3

SELECT * FROM Table 1

WHERE C2 In (2,3)

Result:

Table 1
C1C2C3
A3Pen
B2Pencil
C2Pencil

Now if we want all the values other than 2 and 3 than:

SELECT * FROM Table 1

WHERE C2 NOT IN (2,3)

LIKE and ILIKE

If we want to get a pattern in the string of a column then Like and Ilike functions are used.

For example: if you want to get an email ending with @gmail.com or all names beginning with A etc. 

LIKE – It is case sensitive 

ILIKE – It is not case sensitive 

Like and Ilike is used with Wildcards i.e.,

  • “%” – matches any sequence of characters. For example: if we want all names starting with A then it will be “A%”
  • “_” Matches any single character. For example: if we want all fast and furious movies than we will use “Fast and Furious _” . Multiple underscores can be used in one single query. 

Example:

Table 1
C1C2C3
A3A1@gmail.com
B2B1@Gmail.com
C2C2@outlook.com
D5D2@gmail.com
E6E3@outlook.com

If we want rows which have @ gmail.com

SELECT * FROM Table 1

WHERE C3 LIKE ‘%@gmail.com’

Result:

Table 1
C1C2C3
A3A1@gmail.com
D5D2@gmail.com

We did not get the row B as LIKE function is case sensitive.

Now, if we use ILIKE:

SELECT * FROM Table 1

WHERE C3 ILIKE ‘%@gmail.com’

Table 1
C1C2C3
A3A1@gmail.com
B2B1@Gmail.com
D5D2@gmail.com

Now if we want in C3 only which have the numberic value of 2 than:

SELECT * FROM Table 1

WHERE C3 LIKE’ ‘_2%’

Table 1
C1C2C3
C2C2@outlook.com
D5D2@gmail.com

Credits: The concepts and materials are taken from the Udemy course:The complete SQL Bootcamp 2022: Go from Zero to Hero

Check out the other parts in the SQL series:

Previous:

Why do we need Structured Query Language (SQL)? SQL PART 4

Why do we need Structured Query Language (SQL)? SQL PART 3

 Why do we need Structured Query Language (SQL)? SQL PART 2

 Why do we need Structured Query Language (SQL)? SQL PART 1

5 thoughts on “How to use LIMIT, BETWEEN, IN, LIKE and ILIKE? SQL Part 5”
  1. I beloved as much as you’ll receive performed right here. The sketch is tasteful, your authored material stylish. nevertheless, you command get bought an shakiness over that you would like be turning in the following. unwell no doubt come more beforehand again as exactly the similar nearly very steadily inside of case you defend this increase.

  2. you are in reality a good webmaster The website loading velocity is amazing It sort of feels that youre doing any distinctive trick Also The contents are masterwork you have done a fantastic job in this topic

  3. Thanks I have recently been looking for info about this subject for a while and yours is the greatest I have discovered so far However what in regards to the bottom line Are you certain in regards to the supply

  4. Your writing is like a breath of fresh air in the often stale world of online content. Your unique perspective and engaging style set you apart from the crowd. Thank you for sharing your talents with us.

Leave a Reply

Your email address will not be published. Required fields are marked *