Sorting the Results using ORDER BY
Group Multiple Rows using GROUP BY
There are situations where you don't need all the results in the database - you just need a few. Say you need the names of the first 5 characters in the database. If you run a SELECT query, you will get the entire table. But if you use the LIMIT option you can limit the number of results. To get the just 5 characters, the command would be...
Syntax of LIMIT Command
SELECT <fields> FROM <table> LIMIT <Offset>,<Number of Results>
- The offset is the number of the row from where the results start. If the offset is 0, the results are fetched from the beginning of the table. If offset is 1, then the first row is ignored - the fetching is started at the second row. If offset is 5, the first 5 rows are skipped.
- Number of Results
- This should be self-explanatory. This specifies the number of rows that should be fetched.
LIMIT has a lot of uses. For example, say you want the name of the character with the second highest IQ. Try this query...
LIMIT is most often used in pagination. Pagination is a design pattern that lets you split up a big number of results into multiple pages. The best example I can give you is the Google results page. At the end of each result page, there is a link to the next page where there are more results. This can be implemented using LIMIT...