File tree Expand file tree Collapse file tree 2 files changed +55
-1
lines changed Expand file tree Collapse file tree 2 files changed +55
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010
1111For a steady stream of TILs, [ sign up for my newsletter] ( https://tinyletter.com/jbranchaud ) .
1212
13- _ 1133 TILs and counting..._
13+ _ 1134 TILs and counting..._
1414
1515---
1616
@@ -630,6 +630,7 @@ _1133 TILs and counting..._
630630- [ Turning Timing On] ( postgres/turn-timing-on.md )
631631- [ Two Ways To Compute Factorial] ( postgres/two-ways-to-compute-factorial.md )
632632- [ Types By Category] ( postgres/types-by-category.md )
633+ - [ Union All Rows Including Duplicates] ( postgres/union-all-rows-including-duplicates.md )
633634- [ Use A psqlrc File For Common Settings] ( postgres/use-a-psqlrc-file-for-common-settings.md )
634635- [ Use Argument Indexes] ( postgres/use-argument-indexes.md )
635636- [ Use Not Valid To Immediately Enforce A Constraint] ( postgres/use-not-valid-to-immediately-enforce-a-constraint.md )
Original file line number Diff line number Diff line change 1+ # Union All Rows Including Duplicates
2+
3+ Two tables or sets of results can be joined together into a single result set
4+ using [ the ` union `
5+ operator] ( https://www.postgresql.org/docs/current/queries-union.html ) . When
6+ combining results with ` union ` , all duplicate rows will be removed from its
7+ result.
8+
9+ ``` sql
10+ > select generate_series(1 ,4 )
11+ union
12+ select generate_series(3 ,6 )
13+ order by 1 asc ;
14+
15+ generate_series
16+ -- ---------------
17+ 1
18+ 2
19+ 3
20+ 4
21+ 5
22+ 6
23+ (6 rows)
24+ ```
25+
26+ Notice that despite both sides of the ` union ` having their own 3 and 4, those
27+ values each only show up once in the result.
28+
29+ If we don't want duplicates to be excluded, we can use ` union all ` .
30+
31+ ``` sql
32+ > select generate_series(1 ,4 )
33+ union all
34+ select generate_series(3 ,6 )
35+ order by 1 asc ;
36+
37+ generate_series
38+ -- ---------------
39+ 1
40+ 2
41+ 3
42+ 3
43+ 4
44+ 4
45+ 5
46+ 6
47+ (8 rows)
48+ ```
49+
50+ In this case we have 8 rows instead of 6 with the values 3 and 4 each appearing
51+ twice.
52+
53+ [ source] ( https://www.postgresqltutorial.com/postgresql-union/ )
You can’t perform that action at this time.
0 commit comments