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
1685
votes
19 answers

What is the difference between UNION and UNION ALL?

What is the difference between UNION and UNION ALL?
Brian G
  • 53,704
  • 58
  • 125
  • 140
60
votes
4 answers

Combining ORDER BY AND UNION in SQL Server

How can I get first record of a table and last record of a table in one result-set? This Query fails SELECT TOP 1 Id,Name FROM Locations ORDER BY Id UNION ALL SELECT TOP 1 Id,Name FROM Locations ORDER BY Id DESC Any help?
Faizal
  • 1,693
  • 6
  • 20
  • 24
40
votes
2 answers

How can I do a Union all in Entity Framework LINQ To Entities?

I came across a scenario where I had to use Union all, how can I achieve so in LINQ to entities ?
Rami Sakr
  • 556
  • 1
  • 6
  • 14
36
votes
10 answers

How to execute UNION without sorting? (SQL)

UNION joins two results and remove duplicates, while UNION ALL does not remove duplicates. UNION also sort the final output. What I want is the UNION ALL without duplicates and without the sort. Is that possible? The reason for this is that I want…
hightow
  • 769
  • 2
  • 9
  • 16
31
votes
2 answers

Curious issue with Oracle UNION and ORDER BY

The following query is perfectly valid in pretty much every database (give or take a dual dummy table), including Oracle: select 'A' as x from dual union all select 'B' from dual order by x asc Returning: | X | |---| | A | | B | Now this…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
28
votes
6 answers

Performance of UNION versus UNION ALL in SQL Server

I have to run a SELECT statement across several tables. I am sure the tables return different records. I am anyway using UNION ALL. Is it better to use UNION or of UNION ALL in performance terms when I am sure the tables return different records?
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
26
votes
3 answers

SQL Union All with order by and limit (Postgresql)

In the following query I get syntax error: SELECT , FROM ORDER BY LIMIT 1 UNION ALL SELECT , FROM WHERE ORDER BY LIMIT 1; syntax error at or…
michael
  • 3,835
  • 14
  • 53
  • 90
19
votes
1 answer

UNION types text and bigint cannot be matched

I'm running a complex stored procedure and I'm getting an error when I have 3 unions, but with 2 unions no error. If I remove either of the top two unions it runs fine. If I make one of the NULLs a 0, it runs fine. The error is "UNION types text and…
Stephane
  • 1,613
  • 5
  • 20
  • 40
16
votes
3 answers

Why would I want .union over .unionAll in Spark for SchemaRDDs?

I'm trying to wrap my head around these two functions in the Spark SQL documentation– def union(other: RDD[Row]): RDD[Row] Return the union of this RDD and another one. def unionAll(otherPlan: SchemaRDD): SchemaRDD Combines the tuples of two RDDs…
duber
  • 2,769
  • 4
  • 24
  • 32
15
votes
8 answers

Slow query on "UNION ALL" view

I have a DB view which basically consists of two SELECT queries with UNION ALL, like this: CREATE VIEW v AS SELECT time, etc. FROM t1 // #1... UNION ALL SELECT time, etc. FROM t2 // #2... The problem is that selects of the form SELECT ... FROM v…
Mladen Jablanović
  • 43,461
  • 10
  • 90
  • 113
14
votes
1 answer

Group By and Order By with UNION ALL

I have a stored procedure with the following query: SELECT (sum(addition)) AS [COUNT], MAX(CONVERT(VARCHAR(12),CREATED,102)) as [date] FROM [TABLE_ONE] WHERE convert(VARCHAR(12),CREATED,102) BETWEEN CONVERT(date,@startdate) AND…
HelpASisterOut
  • 3,085
  • 16
  • 45
  • 89
14
votes
5 answers

Same number of columns for Union Operation

I was going through union and union all logic and trying examples. What has puzzled me is why is it necessary to have same number of columns in both the tables to perform a union or union all operation? Forgive me if my question's silly, but i…
Sparky
  • 743
  • 6
  • 15
  • 28
13
votes
5 answers

SQL Server, Using UNION ALL for multiple tables then paging implementation

I need help also about paging and using UNION ALL for multiple tables: How do i implement an optimized paging when joining multiple tables using UNION ALL and returning only specific number of rows... declare @startRow int declare @PageCount…
12
votes
4 answers

SQL Server: ORDER BY in subquery with UNION

i have two queries being combined with a UNION ALL1: --Query 1 SELECT Flavor, Color FROM Friends   --Query 2 SELECT Flavor, (SELECT TOP 1 Color FROM Rainbows WHERE Rainbows.StrangerID = Strangers.StrangerID ORDER BY Wavelength…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
10
votes
5 answers

To union or union all, that is the question

I have two queries that I'm UNIONing together such that I already know there will be no duplicate elements between the two queries. Therefore, UNION and UNION ALL will produce the same results. Which one should I use?
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
1
2 3
51 52