Professional Documents
Culture Documents
WK 11 ADS Content 4.5 Limit Clause
WK 11 ADS Content 4.5 Limit Clause
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:
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
The following illustrates the LIMIT clause syntax with two arguments:
SELECT
select_list
FROM
table_name
LIMIT [offset,] row_count;
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.
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.
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:
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;
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.
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;
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;
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
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 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,...)
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":
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":
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
11
Module in Advanced Database Management. For validation and evaluation purposes.
Dexter P. Dumayag, CICS Faculty Member. First Semester, SY 2020-2021