The LIMIT clause takes two arguments, as the following syntax shows: LIMIT [,] : Limits « Select Clause « SQL / MySQL






The LIMIT clause takes two arguments, as the following syntax shows: LIMIT [,]

      
mysql>
mysql>
mysql> CREATE TABLE CDs
    -> (
    ->     CDID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->     CDName VARCHAR(50) NOT NULL,
    ->     InStock SMALLINT UNSIGNED NOT NULL,
    ->     OnOrder SMALLINT UNSIGNED NOT NULL,
    ->     Reserved SMALLINT UNSIGNED NOT NULL,
    ->     Department ENUM('Classical', 'Popular') NOT NULL,
    ->     Category VARCHAR(20) NOT NULL,
    ->     RowUpdate TIMESTAMP NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO CDs (CDName, InStock, OnOrder, Reserved, Department, Category) VALUES
    -> ('Xml', 10, 5, 3, 'Popular', 'Rock'),
    -> ('Java', 10, 5, 3, 'Classical', 'Opera'),
    -> ('SQL', 17, 4, 1, 'Popular', 'Jazz'),
    -> ('MySQL', 9, 4, 2, 'Classical', 'Dance'),
    -> ('CSS', 24, 2, 5, 'Classical', 'General'),
    -> ('HTML', 16, 6, 8, 'Classical', 'Vocal'),
    -> ('Oracle', 2, 25, 6, 'Popular', 'Blues'),
    -> ('Javascript', 32, 3, 10, 'Popular', 'Jazz'),
    -> ('Data type', 12, 15, 13, 'Popular', 'Country'),
    -> ('Flash', 5, 20, 10, 'Popular', 'New Age'),
    -> ('Ajax', 24, 11, 14, 'Popular', 'New Age'),
    -> ('Photoshop', 42, 17, 17, 'Classical', 'General'),
    -> ('Word', 25, 44, 28, 'Classical', 'Dance'),
    -> ('iPhone', 32, 15, 12, 'Classical', 'General'),
    -> ('MacBook', 20, 10, 5, 'Classical', 'Opera'),
    -> ('Linux', 23, 12, 8, 'Classical', 'General'),
    -> ('Shell', 23, 10, 17, 'Popular', 'Country'),
    -> ('Pascal', 18, 20, 10, 'Popular', 'Jazz'),
    -> ('Ruby', 22, 5, 7, 'Popular', 'Blues'),
    -> ('Sql Server', 28, 17, 16, 'Classical', 'General'),
    -> ('Opera', 10, 35, 12, 'Classical', 'Opera'),
    -> ('Safari', 15, 30, 14, 'Popular', 'Blues'),
    -> ('C', 42, 0, 8, 'Popular', 'Blues'),
    -> ('C++', 16, 8, 8, 'Classical', 'General');
Query OK, 24 rows affected (0.00 sec)
Records: 24  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql>
mysql> SELECT CDID, CDName, InStock
    -> FROM CDs
    -> WHERE Department='Classical'
    -> ORDER BY CDID DESC
    -> LIMIT 4;
+------+------------+---------+
| CDID | CDName     | InStock |
+------+------------+---------+
|   24 | C++        |      16 |
|   21 | Opera      |      10 |
|   20 | Sql Server |      28 |
|   16 | Linux      |      23 |
+------+------------+---------+
4 rows in set (0.00 sec)

mysql>
mysql>
mysql> drop table CDs;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
  








Related examples in the same category

1.Limit the query results: Get 3 Most Expensive Products
2.Use LIMIT in SELECT
3.Use LIMIT
4.Another ORDER BY and limit
5.Simple LIMIT
6.Retrieving the Top Five Students
7.ORDER and limit
8.Limits and order
9.Scrolling through the entire product table four records at a time
10.Determining the Number of Records Suppressed by LIMIT (SQL_CALC_FOUND_ROWS, FOUND_ROWS)
11.Limiting a Selection Using LIMIT
12.Use ORDER BY to sort the result set. Then you can use LIMIT to find smallest and largest values.
13.Order the rows with the largest SUM( ) values first and use LIMIT to select the first record
14.A DELETE statement can be qualified by using an ORDER BY and a LIMIT clause
15.SELECT statement includes a LIMIT clause that specifies an offset value of 3 and a row count value of 4
16.WHERE clause can include additional logical operators and expressions that further limit the results returned
17.Limit to first three columns
18.LIMIT 5 OFFSET 3
19.Delete with order by and limit clause
20.Pulling a Section from the Middle of a Result Set
21.Tell MySQL what offset to use, from which result to start limiting.
22.The default offset is 0, so by specifying an offset of 1, you're taking the second record.
23.Selecting Records from the Beginning or End of a Result Set
24.Processing the First or Last n Records
25.Limiting the Number of Results
26.But LIMIT allows you to return only that record