Consider this select statement:
SELECT *,
1 AS query_id
FROM players
WHERE username='foobar';
It returns the column query_id with value 1 along with a player's other columns.
How would one make the above SQL return at least the query_id of 1 even if the select finds no rows that match?
BTW, it's PostgreSQL 8.4.
order by. The second one "creates" a virtual table with exactly one row and one column and does an outer join it (without any "real" join condition), thus you always get back at least that one row. Usingselect *in production code is bad style. Don't do it. Always list the columns you need.select *should only be used in ad-hoc queries. – Apr 27 '13 at 20:48left joinis not readable? – Apr 28 '13 at 05:41SELECT * FROM weather INNER JOIN cities ON (weather.city = cities.name);orSELECT * FROM weather, cities WHERE city = name;. Some people might find one or the other version more readable or easier to understand. Thanks for updating your answer with the alternate syntax and thanks for the great answer itself. – Nathanael Weiss Apr 28 '13 at 07:41UNION ALLcan sometimes be more efficient thanUNION, as you are explicitly telling the query planner that either you expect there to be no duplicate rows coming out of theUNIONed queries or if there are you want them to be output. Without theALLmodifier it assumes you want duplicate rows removing (only one of each returned) much like with theDISTINCTkeyword, and to guarantee that it may need to resort+rescan the results an extra time. So useALLwithUNIONunless you specifically need output row de-duplication. – David Spillett Jul 16 '13 at 15:52