Questions tagged [union]

Use this tag only for questions about UNION, a keyword of the SQL language for combining the results of multiple SQL queries. Use [union-all] for the UNION-ALL keyword. Use the tag [unions] for unions in C, C++, and similar languages.

UNION is a keyword of SQL for combining the results of multiple SQL queries. The results are combined and duplicate rows are eliminated (similar to DISTINCT). If UNION ALL is used, the rows are combined but duplicates are not removed.

Use the tag for unions in C, C++, and similar languages. Use for the UNION-ALL keyword.

Reference

5266 questions
23
votes
3 answers

Convert List> into List in C#

I have a List>. I would like to convert it into a List where each int is unique. I was wondering if anyone had an elegant solution to this using LINQ. I would like to be able to use the Union method but it creates a new List<>…
user57230
  • 233
  • 1
  • 2
  • 5
22
votes
3 answers

PostgreSQL syntax error at or near 'union'

I have a small query, and a union to put another small query next to it. However, the union has a syntax error in it. Select , , From where order by…
Padagomez
  • 1,114
  • 5
  • 14
  • 35
22
votes
1 answer

How to union two subqueries in SQLAlchemy and postgresql

Raw SQL desired: SELECT id FROM (SELECT some_table.id FROM some_table WHERE some_table.some_field IS NULL) AS subq1 UNION (SELECT some_table.id WHERE some_table.some_field IS NOT NULL) LIMIT 10; Here is the python code: import…
kkaehler
  • 493
  • 1
  • 4
  • 13
21
votes
5 answers

mysql order by with union doesn't seem to work

Here is my query (SELECT * FROM `jokes` WHERE `flags` < 5 AND (`title` LIKE "%only three doors%" OR `joke` LIKE "%only three doors%") ORDER BY `ups` DESC,`downs` ASC) UNION (SELECT * FROM `jokes` WHERE `flags` < 5 AND (`title` LIKE "%only%" OR…
Kelly Elton
  • 4,373
  • 10
  • 53
  • 97
21
votes
7 answers

How can I order entries in a UNION without ORDER BY?

How can I be sure that my result set will have a first and b second? It would help me to solve a tricky ordering problem. Here is a simplified example of what I'm doing: SELECT a FROM A LIMIT 1 UNION SELECT b FROM B LIMIT 1;
markus
  • 40,136
  • 23
  • 97
  • 142
21
votes
8 answers

How do I Concatenate entire result sets in MySQL?

I'm trying out the following query: SELECT A,B,C FROM table WHERE field LIKE 'query%' UNION SELECT A,B,C FROM table WHERE field LIKE '%query' UNION SELECT A,B,C FROM table WHERE field LIKE '%query%' GROUP BY B ORDER BY B ASC LIMIT 5 That's three…
mauriciopastrana
  • 5,010
  • 7
  • 35
  • 36
21
votes
1 answer

Combine two SELECT queries in PostgreSQL

I would like to combine two select queries with UNION. How can I use the result from the first SELECT in the second SELECT? (SELECT carto_id_key FROM table1 WHERE tag_id = 16) UNION (SELECT * FROM table2 WHERE carto_id_key =
Bwyss
  • 1,736
  • 3
  • 25
  • 48
21
votes
3 answers

Which is faster: Union or Concat?

Which is faster: Union or Concat? I don't care about the order of the elements. Enumerable.Union Method Enumerable.Concat Method
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
20
votes
2 answers

Polymorphic entities in Room

There are 3 entities in my Room DB: Album, PhotosMediaItem and VideosMediaItem. VideosMediaItem and PhotosMediaItem inherit from MediaItem. MediaItem is not an entity in the DB, it's just an abstract base class. I would like to create a query that…
idish
  • 3,190
  • 12
  • 53
  • 85
20
votes
5 answers

Java: Is there an easy, quick way to AND, OR, or XOR together sets?

That is, if I had two or more sets, and I wanted to return a new set containing either: All of the elements each set has in common (AND). All of the elements total of each set (OR). All of the elements unique to each set. (XOR). Is there an easy,…
Daddy Warbox
  • 4,500
  • 9
  • 41
  • 56
20
votes
4 answers

Identifying source table from UNION query

I'm building an RSS feed in PHP which uses data from three separate tables. The tables all refer to pages within different areas of the site. The problem I have is trying to create the link fields within the XML. Without knowing which table each…
Dan
  • 1,011
  • 4
  • 14
  • 19
20
votes
3 answers

Combine multiple SELECT statements

I've used Excel to generate numerous SELECT statements from a list of the schema names from a database with a large number of identical schemas: select result from foo.table limit 1; select result from bar.table limit 1; select result from doo.table…
Andrew M
  • 373
  • 1
  • 3
  • 12
19
votes
4 answers

SELECT UNION as DISTINCT

How do I perform a DISTINCT operation on a single column after a UNION is performed? T1 -- ID Value 1 1 2 2 3 3 T2 -- ID Value 1 2 4 4 5 5 I am trying to return the table: ID Value 1 1 2 2 3 3 4 4 5 5 I tried: SELECT DISTINCT ID,…
user1124535
  • 765
  • 3
  • 9
  • 15
19
votes
4 answers

SQL: Using Top 1 in UNION query with Order By

I have a table as below Rate Effective_Date ---- -------------- 5.6 02/02/2009 5.8 05/01/2009 5.4 06/01/2009 5.8 12/01/2009 6.0 03/15/2009 I am supposed to find the all rates that are effective for current date and after it. So to get the…
Rajeshwaran S P
  • 582
  • 1
  • 7
  • 15
19
votes
4 answers

Apply "ORDER BY" on a "UNION" (Mysql)

So, everything is in the title. I am looking to merge the result of two requests and order the result together (as in, not one after the other). => I was thinking of applying a union and ordering them. It didn't work. I looked around like here on…
Quiche
  • 301
  • 1
  • 2
  • 13