How to select an empty result set?

SqlMysqlStored ProceduresNullResultset

Sql Problem Overview


I'm using a stored procedure in MySQL, with a CASE statement.

In the ELSE clause of the CASE ( equivalent to default: ) I want to select and return an empty result set, thus avoiding to throw an SQL error by not handling the ELSE case, and instead return an empty result set as if a regular query would have returned no rows.

So far I've managed to do so using something like:
Select NULL From users Where False

But I have to name an existing table, like 'users' in this example. It works, but I would prefer a way that doesn't break if eventually the table name used is renamed or dropped.

I've tried Select NULL Where False but it doesn't work.

Using Select NULL does not return an empty set, but one row with a column named NULL and with a NULL value.

Sql Solutions


Solution 1 - Sql

There's a dummy-table in MySQL called 'dual', which you should be able to use.

select
    1
from
    dual
where
    false

This will always give you an empty result.

Solution 2 - Sql

This should work on most DBs, tested on Postgres and Netezza:

SELECT NULL LIMIT 0;

Solution 3 - Sql

T-SQL (MSSQL):

SELECT Top 0 1;

Solution 4 - Sql

How about

 SELECT * FROM (SELECT 1) AS TBL WHERE 2=3

Checked in myphp, and it also works in sqlite and probably in any other db engine.

Solution 5 - Sql

This will probably work across all databases.

SELECT * FROM (SELECT NULL AS col0) AS inner0 WHERE col0 IS NOT NULL;

Solution 6 - Sql

SELECT TOP 0 * FROM [dbo].[TableName]

This is a reasonable approach to constant scan operator.

Solution 7 - Sql

SELECT NULL WHERE FALSE;

it works in postgresql ,mysql, subquery in mysql.

Solution 8 - Sql

How about this?

SELECT 'MyName' AS EmptyColumn
FROM dual
WHERE 'Me' = 'Funny'

Solution 9 - Sql

SELECT * FROM (SELECT NULL) WHERE 0

Solution 10 - Sql

In PostgreSQL a simple

SELECT;

works. You won't even get any columns labeled 'unknown'.
Note however, it still says 1 row retrieved.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPetruzaView Question on Stackoverflow
Solution 1 - SqlBjörnView Answer on Stackoverflow
Solution 2 - SqlAlehView Answer on Stackoverflow
Solution 3 - SqlirrelevantthesisView Answer on Stackoverflow
Solution 4 - SqlMakseeView Answer on Stackoverflow
Solution 5 - SqldhruvbirdView Answer on Stackoverflow
Solution 6 - SqlNileshView Answer on Stackoverflow
Solution 7 - SqlTimView Answer on Stackoverflow
Solution 8 - SqlRaj MoreView Answer on Stackoverflow
Solution 9 - SqlaasfalconView Answer on Stackoverflow
Solution 10 - SqlluckydonaldView Answer on Stackoverflow