TGViewer
There will be no singularity There will be no singularity @nosingularity · 1.96K subscribers
Post #1307 2.86K
Friendly reminder:
The UNION and UNION ALL operators may order the rows in the final result set in an unspecified order. For example, it may place rows from the second result set before/after the row from the first result set.


I have encountered a situation where a query with UNION and UNION ALL resulted in a stable different row order...

UNION is often used for something like this:

SELECT * FROM (
SELECT id FROM t WHERE name = 'user_input'
UNION
SELECT id FROM t WHERE name = 'default_value'
)
LIMIT 1


It is important that if a record with 'user_input' is found, it should be the first record in the result.
In such a case, add an ORDER BY of this form to the query:

ORDER BY name = 'default_value'
  • 👍 3
  • 🤔 3
  • 🤨 1
More from @nosingularity
  1. Sep 19, 2026fixupx.com/KaiLentit/status/2100629518784328013/video/1
  2. Sep 18, 2026fixupx.com/iam_zachi/status/2100679300756435135
  3. Aug 30, 2026photo post
  4. Aug 25, 2026fixupx.com/meganreyno/status/2091918430374957416
  5. Aug 3, 2026Вышла новость, что Andy Pavlo приняли на борт Clickhouse Inc. Энди довольно известная фигу…
  6. Jul 23, 2026fixupx.com/unclebobmartin/status/2080257779395154409
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →