MySQL – Why is LIMIT 0 Allowed?

MySQL

I've just tried running SELECT * FROM tbl LIMIT 0, which unsurprisingly, returns 0 results.

I could see an argument for making it be an error, because it will never return any result, but then again it isn't invalid like LIMIT 'HelloWorld'.

Are there any actual real-world use cases for LIMIT 0, or is it just allowed because there's no real reason to not to allow it?

Best Answer

Perhaps a matter of convenience

I once answered a post Table JOIN and CREATE new table extremely slow, mentioning how CREATE TABLE AS SELECT will create a table and then perform INSERT INTO SELECT to populate the table

There are ways to use LIMIT 0 to create a temp table without indexes

CREATE TABLE mytable SELECT * FROM othertable LIMIT 0;

In another post, MySQL Locks while CREATE TABLE AS SELECT, I mentioned under OPTION #2 using WHERE 1=2 as a way to create a blank temp table with no indexes

CREATE TABLE 1_temp_foo SELECT * FROM crm_companies WHERE 1=2;
ALTER TABLE 1_temp_foo ENGINE=InnoDB;

In terms of end result, using LIMIT 0 and WHERE 1=2 and even WHERE 0 are synonymous.

I agree with your assumption that it was allowed for no particular reason (because there's no real reason to not allow it, as you say), but it sure comes in handy for such occasions.