LEFT JOIN
SELECT l.*,r.*
FROM LeftTable l
LEFT JOIN RightTable r ON l.LCode=r.RCode
LCode
|
LDescr
|
RCode
|
RDescr
|
1
|
L-1
|
NULL
|
NULL
|
2
|
L-2
|
2
|
B-2
|
3
|
L-3
|
3
|
B-3
|
5
|
L-5
|
NULL
|
NULL
|
Bu erda barcha LeftTable satrlari qaytarildi, ular RightTable-dan olingan ma'lumotlar bilan to'ldirildi, buning uchun shart bajarildi (l.LCode=r.RCode)
RIGHT JOIN
SELECT l.*,r.*
FROM LeftTable l
RIGHT JOIN RightTable r ON l.LCode=r.RCode
LCode
|
LDescr
|
RCode
|
RDescr
|
2
|
L-2
|
2
|
B-2
|
3
|
L-3
|
3
|
B-3
|
NULL
|
NULL
|
4
|
B-4
|
Bu erda barcha satrlarni qaytarib berildi Lighttable, bu LeftTable-dan chiziqlar ma'lumotlari bilan to'ldirildi, buning uchun shart bajarildi (l.LCode=r.RCode)
aslida, Agar LeftTable va RightTable joylarini qayta tashkil qilsak, biz chap aloqa yordamida shunga o'xshash natijani qo'lga kiritamiz:
SELECT l.*,r.*
FROM RightTable r
LEFT JOIN LeftTable l ON l.LCode=r.RCode
LCode
|
LDescr
|
RCode
|
RDescr
|
2
|
L-2
|
2
|
B-2
|
3
|
L-3
|
3
|
B-3
|
NULL
|
NULL
|
4
|
B-4
|
Men tez-tez left JOIN-dan foydalanganimni payqadim, ya'ni.birinchi navbatda, qaysi jadval men uchun muhim bo'lgan ma'lumotlarni o'ylayman va keyin qaysi jadval/jadval qo'shimcha jadval rolini o'ynaydi deb o'ylayman.
FULL JOIN – это по сути одновременный LEFT JOIN + RIGHT JOIN
SELECT l.*,r.*
FROM LeftTable l
FULL JOIN RightTable r ON l.LCode=r.RCode
LCode
|
LDescr
|
RCode
|
RDescr
|
1
|
L-1
|
NULL
|
NULL
|
2
|
L-2
|
2
|
B-2
|
3
|
L-3
|
3
|
B-3
|
5
|
L-5
|
NULL
|
NULL
|
NULL
|
NULL
|
4
|
B-4
|
Barcha qatorlar LeftTable va RightTable-dan qaytib keldi. Buning uchun shartlar bajarildi (l.LCode=r.RCode) bir qatorda birlashtirildi. Chap yoki o'ng tarafdagi satrda mavjud bo'lmagan ma'lumotlar null qiymatlari bilan to'ldiriladi.
CROSS JOIN
SELECT l.*,r.*
FROM LeftTable l
CROSS JOIN RightTable r
LCode
|
LDescr
|
RCode
|
RDescr
|
1
|
L-1
|
2
|
B-2
|
2
|
L-2
|
2
|
B-2
|
3
|
L-3
|
2
|
B-2
|
5
|
L-5
|
2
|
B-2
|
1
|
L-1
|
3
|
B-3
|
2
|
L-2
|
3
|
B-3
|
3
|
L-3
|
3
|
B-3
|
5
|
L-5
|
3
|
B-3
|
1
|
L-1
|
4
|
B-4
|
2
|
L-2
|
4
|
B-4
|
3
|
L-3
|
4
|
B-4
|
5
|
L-5
|
4
|
B-4
|
LeftTable ning har bir satri RightTable ning barcha satrlari ma'lumotlariga ulanadi.
Savollar
1
SQL JOIN buyrug'i nima va u qachon kerak?
2
JOINning qanday turlari mavjud?
3
OUTER JOIN nima?
4
SQL INNER JOIN va SQL LEFT JOIN o'rtasidagi farq nima?
5
LEFT JOIN va FULL JOIN o'rtasidagi farq nima?
6
Ikkitadan ortiq jadvalga qanday qo'shilish mumkin?
7
Jadvalni o'ziga qanday qo'shishingiz mumkin?
8
JOIN sharti tenglik bo'lishi kerakmi?
9
SQL va MySQL o'rtasidagi farq nima?
10
SQL ning turli kichik to'plamlari qanday?
11
DBMS deganda nimani tushunasiz? Uning qanday turlari bor?
12
SQLda jadval va maydon deganda nimani tushunasiz?
13
SQL da birlashmalar nima?
14
SQL tilidagi CHAR va VARCHAR2 ma’lumotlar turi o‘rtasidagi farq nima?
15
Birlamchi kalit nima?
16
Cheklovlar nima?
17
DELETE va TRUNCATE operatorlarining farqi nimada?
18
Noyob kalit nima?
Foydalanilgan adabiyotlar
1 https://www.w3schools.com/sql/sql_join.asp
2 https://www.edureka.co/blog/interview-questions/sql-interview-questions
3 https://www.pluralsight.com/guides/querying-multiple-tables
4 https://www.educba.com/sql-joins-interview-questions/
5 SQL JOINS - Exercises, Practice, Solution -
w3resourcewww.w3resource.com
6 https://www.educative.io/blog/what-are-sql-joins
Do'stlaringiz bilan baham: |