In MySQL, the TABLE
statement returns rows and columns of the given table.
The TABLE
statement is similar to the SELECT
statement, and it can be used as a kind of shorthand version of the SELECT
statement.
The TABLE
statement was introduced in MySQL 8.0.19.
Syntax
The syntax goes like this:
TABLE table_name [ORDER BY column_name] [LIMIT number [OFFSET number]]
Example
Here’s an example to demonstrate:
TABLE Genres;
Result:
+---------+---------+ | GenreId | Genre | +---------+---------+ | 1 | Rock | | 2 | Jazz | | 3 | Country | | 4 | Pop | | 5 | Blues | | 6 | Hip Hop | | 7 | Rap | | 8 | Punk | +---------+---------+
That is the equivalent of doing the following:
SELECT * FROM Genres;
Result:
+---------+---------+ | GenreId | Genre | +---------+---------+ | 1 | Rock | | 2 | Jazz | | 3 | Country | | 4 | Pop | | 5 | Blues | | 6 | Hip Hop | | 7 | Rap | | 8 | Punk | +---------+---------+
Ordering
You can use the ORDER BY
clause to order the rows returned by the TABLE
statement:
TABLE Genres
ORDER BY Genre ASC;
Result:
+---------+---------+ | GenreId | Genre | +---------+---------+ | 5 | Blues | | 3 | Country | | 6 | Hip Hop | | 2 | Jazz | | 4 | Pop | | 8 | Punk | | 7 | Rap | | 1 | Rock | +---------+---------+
The LIMIT
Clause
You can also limit the rows returned by the TABLE
statement by using the LIMIT
clause:
TABLE Genres
LIMIT 5;
Result:
+---------+---------+ | GenreId | Genre | +---------+---------+ | 1 | Rock | | 2 | Jazz | | 3 | Country | | 4 | Pop | | 5 | Blues | +---------+---------+
You can use OFFSET
to specify an offset for the limited results:
TABLE Genres
LIMIT 5 OFFSET 3;
Result:
+---------+---------+ | GenreId | Genre | +---------+---------+ | 4 | Pop | | 5 | Blues | | 6 | Hip Hop | | 7 | Rap | | 8 | Punk | +---------+---------+
And you can use ORDER BY
and LIMIT
together:
TABLE Genres
ORDER BY Genre ASC
LIMIT 5;
Result:
+---------+---------+ | GenreId | Genre | +---------+---------+ | 5 | Blues | | 3 | Country | | 6 | Hip Hop | | 2 | Jazz | | 4 | Pop | +---------+---------+
Differences Between TABLE
and SELECT
Although the TABLE
and the SELECT
statements are similar, there are some differences. Here are the main differences between TABLE
and SELECT
:
- The
TABLE
statement always displays all columns of the table (you can’t explicitly select certain columns). - The
TABLE
statement does not allow for any arbitrary filtering of rows. In other words,TABLE
does not support anyWHERE
clause.
See the MySQL documentation for more info.