Questions tagged [union-all]

"UNION ALL" is a keyword in SQL that is used for combining the results of multiple SELECTs. The related tag is "UNION". "UNION ALL" combines the results without checking for Uniqueness. "UNION" combines the results eliminating duplicates.

UNION ALL is a keyword in SQL that is used for combining the results of multiple SELECTs. The related tag is UNION (). UNION ALL combines the results without checking for uniqueness. UNION combines the results eliminating duplicates. The type and order of the fields in the two SELECTs should be the same.

775 questions
3
votes
2 answers

SQL - does order of OR conditions matter?

I have to SELECT one row which meets condition1 OR condition2. However, condition1 is preferable. If there are two rows, where the first one meets condition1 (and does not meet condition2) and second meets condition2 (and does not meet condition1)…
user606521
  • 14,486
  • 30
  • 113
  • 204
3
votes
2 answers

T-SQL - populating data in temp table in stored procedure.

I need help with my stored procedure which I'm currently working on. Basically stored procedure works fine and gets me required data. I would like to keep this funcionality and add new temporary table within stored procedure and populate this temp…
Dear Deer
  • 99
  • 1
  • 9
3
votes
2 answers

Tsql, union changes result order, union all doesn't

I know UNION removes duplicates but it changes result order even when there are no duplicates. I have two select statements, no order by statement anywhere I want union them with or without (all) i.e. SELECT A UNION (all) SELECT B "Select B"…
Li Tian Gong
  • 393
  • 1
  • 6
  • 16
3
votes
2 answers

Why does UNION ALL seem to wait for both queries to finish?

Running this query: select null, "hello" union all select sleep(4), "world"; on 5.5.29 doesn't return the first row right away as expected. Instead, I have to wait 4 seconds before getting anything. How can I make MySQL return the first row right…
Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
3
votes
3 answers

Is the order of union all guaranteed

Is it guaranteed that order of two parts of the union all query will be given in the particular order? I.e. that the result of this query: select 'foo' from dual union all select 'bar' from dual Will always be foo bar and not this bar foo ? I…
Ivan Sopov
  • 2,300
  • 4
  • 21
  • 39
3
votes
1 answer

Use UNION and UNION ALL in same query

I am running into some performance issue and I have a query as follow: SELECT * FROM Foo UNION SELECT * FROM Boo UNION SELECT * FROM Koo I know for sure that Koo is not returning any duplicates. I was considering to use UNION ALL at the end and…
Moslem Ben Dhaou
  • 6,897
  • 8
  • 62
  • 93
3
votes
3 answers

Can't UNION ALL two equivalent queries?

I have a working query that I need to repeat a few times, however I'm getting syntax errors on the UNION ALL: Working query: set @num := 0, @group := ''; select person, `group`, age from ( select person, `group`, age, @num := if(@group =…
Yarin
  • 173,523
  • 149
  • 402
  • 512
2
votes
2 answers

UNION ALL in CTE

I'm trying to get a UNION ALL working with a CTE which I'm using for paging. I need to get all records matching a set of criteria from two tables, then page the results. The first table's CTE looks like this: ;WITH Results_CTE AS (SELECT…
2
votes
1 answer

Getteing more values with UNION ALL SQL Query

I am using this query to get results from two tables: SELECT * FROM ( SELECT parent_id as mID, count(*) as cnt FROM wp_forum_posts WHERE text LIKE '%{$word}%' GROUP by 1 UNION ALL SELECT id, count(*) …
user998163
  • 471
  • 1
  • 9
  • 28
2
votes
3 answers

How to find all objects in a database that use UNION

How can I find all views and SP's that contains UNION keyword? (Alternate question: in what system table is stored the text of SP's and Views) Thank you
bzamfir
  • 4,698
  • 10
  • 54
  • 89
2
votes
3 answers
2
votes
0 answers

Postgres query runs 1000X slower when combined with UNION ALL than when queries run separate

I have two queries that when run separately, each take 1-10 seconds to execute. When I combine their results with UNION ALL, the execution time jumps up to 4500 seconds (over 1 hour)! I have run the long query with EXPLAIN ANALYZE and it appears to…
Scott C.
  • 21
  • 2
2
votes
2 answers

SQL Server: Is it possible to UNION two tables in a SELECT statement without using a temp table?

I am creating a stored procedure that sets 3 variables based on queries and I have to return the MAX value of them. I am trying to do so without using a temp table however the following gives me the error Incorrect styntax near ')' SELECT…
jon3laze
  • 3,188
  • 6
  • 36
  • 69
2
votes
1 answer

MariaDB / MySQL Combine two tables

I have 2 tables tnHeaders and tnData tnHeaders fnIDX fnDESCRIPTION 1 h1 2 h2 3 h3 tnData fnIDX fnHEADER_IDX fnDESCRIPTION 1 1 d1 2 1 d2 3 1 d3 4 2 d4 5 2 d5 6 2 d6 7 3 d7 8 3 d8 9 3 d9 and would like to…
pcurtis
  • 81
  • 5
2
votes
2 answers

Count of rows returned by 2 out of 3 SELECTs combined with UNION

I have a hard time titling this post. How can we accomplish the following? SELECT 'Body' AS LineType, ItemName, Quantity FROM TableX UNION ALL SELECT 'Body' AS LineType, ItemName, Quantity FROM TableY UNION ALL SELECT 'Trailer' AS LineType, null…
FMFF
  • 1,652
  • 4
  • 32
  • 62