8 Ağustos 2023 Salı

SQL Inner Join - A ve B'nin kesişimi

Giriş
Aynı özelliklere sahip satırları gruplamak için kullanılır. Eğer sağ tabloda bir eşleşme yoksa, soldaki satır sonuca dahil edilmez. Açıklaması şöyle
Inner Join is one of the most frequently used joins. The left and right tables are joined by predicates. Only the rows that appear in both the left table and the right table meet the condition. It means the rows are in the set intersection of the left and right tables. The syntax is select A.x, B.y from A join B on A.x = B.y. Inner Join does not distinguish the order of the left and right tables. A inner join B is equivalent to B join A.

Inner Join is divided into Equal Join and Non Equal Join (Theta Join). The difference is that Equal Join is a certain field in the left table that is equal to a field in the right table under the join condition. The join condition of Theta Join is not the equality condition but may be greater-than or less-than conditions.
Şeklen şöyle
Inner Join 2 farklı şekilde olabilir
1. Theta Join
2. Equi Join

1. THETA JON
Örnek
Şöyle yaparız
SELECT firstname, lastname domicile, location
  FROM employee JOIN department
  ON domicile < location

2. EQUI JOIN
Örnek
Şöyle yaparız. Böylece A ve B'deki tüm sütunları alabiliriz.
SELECT * FROM a INNER JOIN b ON a.a = b.b;
JOIN Gerçekleştirimi
1. Hash Join
2. Neted Loop 
şeklinde olabilir

1. Hash Join 
Açıklaması şöyle
In a Hash Join, Database builds a hash table from the smaller of the two joining tables based on the join key.

Hash Join is well-suited for equi-joins, where the join condition is based on equality between columns.
2. Nested Loop
Açıklaması şöyle. Yani aslında Nested Loop Theta Join için daha uygun gibi
Nested Loop Join is a join method that works well for smaller datasets and simple join conditions.

In Nested Loop Join, Database iterates through each row in the outer table and matches it with corresponding rows from the inner table based on the join condition.

Nested Loop Join is suitable for join conditions involving range checks, inequalities, or non-equality conditions.

1. INNER JOIN Yerine Sadece Where
Inner Join kelimesi kullanmadan kısa şekliyle sadece Where ile yazılabilir

Örnek
Şöyle yaparız.
# inner join
SELECT o.order_id, o.order_date, c.customer_name
FROM orders o
INNER JOIN customers c ON o.customer_id = c.customer_id;
# where
SELECT o.order_id, o.order_date, c.customer_name
FROM orders o, customers c
WHERE o.customer_id = c.customer_id;
2. Alt SQL
Eğer A ve B'deki bazı sütunları görmek isteseydik alt SQL kullanırdık. Alt SQL'de parantez içinde SQL sonucuna bir alias tanımlanır. Örnekte alias olarak grp kullanılmış. Daha sonra On A.X = Alias. X ile süzme işlemi yapılır.
SELECT e.Name, e.Station 
FROM Employee e
INNER JOIN
(
   SELECT Salary
   FROM Employee
   GROUP BY Salary
   HAVING COUNT(*) > 1
) grp ON e.Salary = grp.Salary;
3. Genel Kullanım
Örnekler

Örnekte aynı maaşa sahip kişiler gösterilmek isteniyor
|Name |Station|Salary|
|Bob  |1      |2000  |
|Steve|2      |1750  |
|Mark |3      |2050  |
|Lisa |4      |2200  |
|Hans |5      |2000  |
İki SQL cümlesi kullanılarak Inner Join yapılır. Bu yöntemde B tablosundaki tüm sütunlar yerine bazı sütunlar istendiği için Inner Join'den sonra parantez için alt sql çalıştırılıyor.
SELECT e.Name, e.Station 
FROM Employee e
INNER JOIN
(
   SELECT Salary
   FROM Employee
   GROUP BY Salary
   HAVING COUNT(*) > 1
) grp ON e.Salary = grp.Salary;
İçerdeki cümle maaşları gruplar. Dışardaki cümle ise maaşla isimleri eşleştirir.

Hiç yorum yok:

Yorum Gönder