You are on page 1of 11

Advanced Database Management

4
MySQL Clauses and Operators
UNIT

Topic
The Limit, Alias, Between, and In Operators
4
Time Allotment: 5 hours

Learning Objectives

At the end of the session, you will be able to use and execute:

a. LIMIT clause to constrain the number of rows returned by a query;


b. BETWEEN operator to determine whether a value is in a range of
values;
c. IN operator to determine if a specified value matches any value in a
list of values; and
d. alias to improve the readability of the queries.

Activate Prior Learning 4.4

What is software engineering?

Give your thoughts on the following terms.

Terms Your thoughts

Greater than or equal

Less than or equal

Equal

1
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database Management

Presentation of Content

Introduction to MySQL LIMIT clause


The LIMIT clause is used in the SELECT statement to constrain the number of rows to
return. The LIMIT clause accepts one or two arguments. The values of both arguments
must be zero or positive integers.

The following illustrates the LIMIT clause syntax with two arguments:

SELECT
select_list
FROM
table_name
LIMIT [offset,] row_count;

Code language: SQL (Structured Query Language) (sql)

In this syntax:

• The offset specifies the offset of the first row to return. The offset of the first row
is 0, not 1.
• The row_count specifies the maximum number of rows to return.

The following picture illustrates the LIMIT clause:

When you use the LIMIT clause with one argument, MySQL will use this argument to
determine the maximum number of rows to return from the first row of the result set.

Therefore, these two clauses are equivalent:

LIMIT row_count;
and
2
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database Management

LIMIT 0 , row_count;

In addition to the above syntax, MySQL provides the following alternative LIMIT clause

syntax:

LIMIT row_count OFFSET offset

The LIMIT and ORDER BY clauses

By default, the SELECT statement returns rows in an unspecified order. When you add
the LIMIT clause to the SELECT statement, the returned rows are unpredictable.

Therefore, to ensure the LIMIT clause returns an expected output, you should always
use it with an ORDER BY clause like this:

SELECT
select_list
FROM
table_name
ORDER BY
sort_expression
LIMIT offset, row_count;

MySQL LIMIT clause example

We’ll use the tblReceiver table from the sample database for demonstration

We’ll use the tblReceiver table from the sample database for demonstration.

This statement uses the LIMIT clause to get the top five receivers who have the
farthest distance.

SELECT
PostalID, Name, Distance
FROM
tblReceiver
ORDER BY Distance DESC
LIMIT 5;

3
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database Management

In this example:
• First, the ORDER BY clause sorts the receivers by distance from far to near.
• Then, the LIMIT clause returns the first 5 rows.

Similarly, this example uses the LIMIT clause to find five receivers who have the nerest
distance:

SELECT
PostalID, Name, Distance
FROM
tblReceiver
ORDER BY Distance
LIMIT 4;

In this example:
• First, the ORDER BY clause sorts the receivers by distance from near to far.
• Then, the LIMIT clause returns the first 4 rows.

Using MySQL LIMIT to get the nth highest or lowest value

To get the nth highest or lowest value, you use the following LIMIT clause:

4
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database Management

SELECT select_list
FROM table_name
ORDER BY sort_expression
LIMIT n-1, 1;

The clause LIMIT n-1, 1 returns 1 row starting at the row n.

For example, the following finds the receiver who has the second-farthest distance:

SELECT
Name, City, Distance
FROM
tblReceiver
ORDER BY
Distance DESC
LIMIT 1, 1;

To check, the distances

select Name, City, Distance from tblReceiver;

5
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database Management

MySQL BETWEEN operator examples

Let’s practice with some examples of using the BETWEEN operator.

Using MySQL BETWEEN with number examples

The following example uses the BETWEEN operator to find HouseNumbers whose
numbers between 1 and 15:

SELECT
Name, HouseNumber, Street
FROM
tblreceiver
WHERE
Housenumber BETWEEN 1 AND 15;

This query uses the greater than or equal (>=) and less than or equal ( <= ) operators
instead of the BETWEEN operator to get the same result:

SELECT
Name, HouseNumber, Street
FROM
tblreceiver
WHERE
HouseNumber >= 1 AND HouseNumber <= 15;

6
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database Management

To find the distance whose lengths are not between 10 and 20, you use the NOT
BETWEEN operator as follows:

SELECT
PostalID, Name, Distance
FROM
tblReceiver
WHERE
Distance NOT BETWEEN 10 AND 20;

The MySQL IN Operator

MySQL IN() function finds a match in the given arguments.

The function returns 1 if expr is equal to any of the values in the IN list, otherwise, returns
0. If all values are constants, they are evaluated according to the type of expr and
sorted. The search for the item then is done using a binary search. This means IN is very

7
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database Management

quick if the IN value list consists entirely of constants. Otherwise, type conversion takes
place according to the rules.

Syntax:

expr IN (value,...)

The IN operator allows you to specify multiple values in a WHERE clause.

The IN operator is a shorthand for multiple OR conditions.

IN Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

Or

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

IN Operator Examples

The following SQL statement selects all receivers that are located in Barangay
"Centro 1", "Centro 4" or "Caggay":

SELECT * FROM tblReceiver


WHERE Barangay IN (“Centro 1”, “Centro 4”, “Caggay”);

8
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database Management

The following SQL statement selects all that are located in Barangay "Centro 1",
"Centro 4" or "Caggay":

SELECT * FROM tblReceiver


WHERE Barangay NOT IN (“Centro 1”, “Centro 4”, “Caggay”);

9
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database Management

Summary Unit

Use the MySQL LIMIT clause to constrain the number of rows returned by
the SELECT statement.

Use the MySQL BETWEEN operator to test if a value falls within a range of values.

Use the IN operator to check if a value is in a set of values r to form a condition for the
WHERE clause.

Aliases can be useful when there are more than one table involved in a query,
functions are used in the query, column names are big or not very readable and if
there are two or more columns are combined together.

10
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021
Advanced Database Management

References

mysqltutorial.org (n.d.). MySQL. Retrieved from https://tinyurl.com/yhny9uh9

w3sccolls.com (n.d.) MySQL IN Operator. Retrieved from


https://www.w3schools.com/

w3schools.com (n.d.). SQL Aliases. Retrieved from https://tinyurl.com/ydac6jpa

11
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021

You might also like