Perbedaan Union Dan Union All

 Posted admin

Untuk menggabungkan isi dari dua tabel atau lebih sering kita menggunakan UNION. Namun sesungguhnya di SQL terdapat dua jenis UNION, yaitu UNION dan UNION ALL. Secara gambar himpunan dapat kita lihat perbedaan seperti dibawah: UNION UNION ALL. Dari gambar di atas dapat kita ketahui bahwa pada UNION data yang sama akan ditampilkan satu kali saja. SELECT. FROM B; Implementasi: 1. Select. from perusahaanA - union - select. from perusahaanB; (menampilkan semua data karyawan di perusahaan A dan B, karyawan yang mendobel bekerja di 2 perusahaan di tulis 1 kali saja) UNION ALL. Sama dengan UNION tapi akan menampilkan semua data meskipun data tersebut sama. SELECT. FROM A.

By: Greg Robidoux Last Updated: 2007-12-05 Comments (2) Related Tips: 1 2 3 4 5 More >JOIN Tables

Problem

Sometimes there is a need to combine data from multiple tables or views into one comprehensive dataset. This may be for tables with similar data within the same database or maybe there is a need to combine similar data across databases or even across servers. In this tip we will take a look at the how to use UNION vs. UNION ALL commands and how they differ.

Solution

In SQL Server you have the ability to combine multiple datasets into one comprehensive dataset by using the UNION vs. UNION ALL operators. There is a big difference in how these work as well as the final result set that is returned, but basically these commands join multiple datasets that have similar structures into one combined dataset.

Here is a brief description:

  • UNION - this operation will allow you to join multiple datasets into one dataset and will remove any duplicates that exist. Basically it is performing a DISTINCT operation across all columns in the result set.
  • UNION ALL - this operation again allows you to join multiple datasets into one dataset, but it does not remove any duplicate rows. Because this does not remove duplicate rows this process is faster, but if you don't want duplicate records you will need to use the UNION operator instead.

Rules to union data:

  • Each query must have the same number of columns
  • Each column must have compatible data types
  • Column names for the final result set are taken from the first query
  • ORDER BY and COMPUTE clauses can only be issued for the overall result set and not within each individual result set
  • GROUP BY and HAVING clauses can only be issued for each individual result set and not for the overall result set
Tip

If you don't have the exact same columns in all queries use a default value or a NULL value such as:

SELECT firstName, lastName, company FROM businessContacts
UNION
SELECT firstName, lastName, NULL FROM nonBusinessContacts

Houdini tomori vex course pdf. or

SELECT firstName, lastName, createDate FROM businessContacts
UNION ALL
SELECT firstName, lastName, getdate() FROM nonBusinessContacts

UNION vs. UNION ALL Examples

Let's take a look at a few simple examples of how these commands work and how they differ. As you will see the final resultsets will differ, but there is some interesting info on how SQL Server actually completes the process.

UNION ALL

In this first example we are using the UNION ALL operator against the Employee table from the AdventureWorks database. This is probably not something you would do, but this helps illustrate the differences of these two operators.

There are 290 rows in table dbo.Employee.

When this query is run the result set has 870 rows. This is the 290 rows returned 3 times. The data is just put together one dataset on top of the other dataset.

Here is the execution plan for this query. We can see that the table was queried 3 times and SQL Server did a Concatenation step to concatenate all of the data.

UNION

In this next example we are using the UNION operator against the Employee table again from the AdventureWorks database.

When this query is run the result set has 290 rows. Even though we combined the data three times the UNION operator removed the duplicate records and therefore returns just the 290 unique rows.

Here is the execution plan for this query. We can see that SQL Server first queried 2 of the tables, then did a Merge Join operation to combine the first two tables and then it did another Merge Join along with querying the third table in the query. So we can see there was much more worked that had to be performed to get this result set compared to the UNION ALL.

UNION vs. UNION ALL Examples With Sort on Clustered Index Column

If we take this a step further and do a SORT of the data using the Clustered Index column we get these execution plans. From this we can see that the execution plan that SQL Server is using is identical for each of these operations even though the final result sets will still contain 870 rows for the UNION ALL and 290 rows for the UNION.

UNION ALL

UNION

Perbedaan Union Dan Union Alliance

UNION vs. UNION ALL Examples With Sort on Non-indexed Column

Here is another example doing the same thing, but this time doing a SORT on a non indexed column. As you can see the execution plans are again identical for these two queries, but this time instead of using a MERGE JOIN, a CONCATENATION and SORT operations are used.

UNION ALL

UNION

Next Steps
  • Take a look at these other tips that may be useful for using the union operators

Last Updated: 2007-12-05



About the author
Greg Robidoux is the President of Edgewood Solutions and a co-founder of MSSQLTips.com.
View all my tips
Related Resources

Apa Perbedaan Union Dan Union All



'UNION ALL' vs 'UNION'

Pentingnya database dan sistem manajemen basis data semakin meningkat dengan penggunaan teknologi informasi dalam kehidupan sehari-hari masyarakat awam. Setiap organisasi mengubah catatan manual dan data mereka ke database digital. Ada beberapa istilah dan fungsi dalam database yang memainkan peran sangat vital dalam pengelolaan basis data. Manajemen basis data itu sendiri merupakan elemen penting dalam suatu organisasi dengan jumlah besar, dan terkadang sensitif, data yang akan dibuat dan dikelola secara reguler. Database menyimpan data dalam bentuk tabel, yang pada gilirannya berisi baris dan kolom untuk menyimpan catatan.

Tabel dalam database memerlukan manajemen yang sangat efisien untuk memanfaatkan sumber daya sebaik mungkin untuk mendapatkan hasil query secepat mungkin. Tabel menyimpan catatan dengan tipe data yang berbeda sesuai kebutuhan dan terkadang mereka memiliki data duplikat yang tersimpan dalam satu tabel. Catatan dari satu tabel mungkin juga ada di tabel lain dalam database yang sama. Sistem manajemen basis data, seperti SQL Server atau Oracle, memiliki beberapa perintah untuk mengelola tabel. UNION dan UNION ALL adalah dua perintah yang mengatur data tabel dan mengoptimalkan hasil kueri.

Dan

Perintah UNION digunakan untuk memilih informasi terkait, memiliki tipe data yang sama, dari beberapa tabel. Selanjutnya, perintah serikat hanya memilih catatan yang berbeda. Ini memerlukan waktu pemrosesan yang relatif lama dan penggunaan sumber daya sistem yang lebih besar saat melakukan operasi pencarian dan pencocokan untuk mengetahui informasi yang berbeda dari catatan gabungan tabel. Selain itu, perintah UNION efektif dengan tabel yang memiliki sejumlah besar catatan dengan banyak duplikasi karena hasil kueri akan sangat spesifik. Hal ini berlaku jika sumber daya sistem yang sangat tinggi tersedia. Namun, lebih efektif dengan tabel yang memiliki sebagian besar data unik karena kurang duplikasi akan mudah diproses dengan sistem yang memiliki sumber daya terbatas.

Perintah UNION ALL memilih semua catatan dari tabel. Tidak seperti UNION, UNION ALL melakukannya dengan cara yang sangat efisien karena tidak memeriksa data yang berlebihan dan menemukan semua hasilnya. Hasil pencarian adalah tabel gabungan yang memiliki semua data termasuk baris duplikat. UNION ALL cepat karena tidak melibatkan pemilahan data. Selanjutnya, perintah UNION ALL paling efektif bila digunakan dengan tabel yang berisi catatan lebih sedikit meskipun tabel berisi banyak duplikat. Meskipun, tabel dengan catatan lebih sedikit dan data unik akan ideal untuk perintah UNION ALL.

Ringkasan:

Perbedaan Union Dan Join

1. UNION ALL operasi dilakukan lebih cepat dari perintah UNION.

2. UNION ALL tidak melakukan pemilahan data sedangkan perintah UNION memberikan hasil query dengan cara yang disortir.

3. UNION ALL mencakup catatan tabel yang berlebihan sementara perintah UNION menghilangkan duplikasi dalam tabel dan hasilnya tidak mengandung baris yang berlebihan.

4. UNION sangat efektif dengan tabel yang lebih besar sedangkan UNION ALL lebih efektif dalam kasus dimana tabel tidak terlalu besar dan redundansi tidak menjadi masalah.
Rating: 8. Artikel bagus. Pengeditan kecil dilakukan.