SELF JOIN in SQL
Each Join operation must have two tables, but there are cases where you have to perform JOIN on the same table, but you can call this JOIN operation SELF JOIN.
SELF JOIN in SQL is used to execute Join on the same table by concatenating a table with itself, considering it to be two tables, and replacing at least one temporary table name.
- JOIN clause in SQL
Syntax SELF JOIN in SQL
The basic syntax of the SELF JOIN is as follows:
SELECT a.ten_cot, b.ten_cot .
FROM bang1 a, bang1 b
WHERE a.cot_chung = b.cot_chung;
Here, the WHERE clause can be any expression at your request.
Example of SELF JOIN in SQL
Suppose the NHANVIEN table has the following records:
Table 1: NHANVIEN
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
Now, let's join these two tables using the SELF JOIN as follows:
SQL> SELECT a.ID, b.TEN, a.LUONG
FROM NHANVIEN a, NHANVIEN b
WHERE a.LUONGThe result is:
+----+----------+---------+ | ID | TEN | LUONG | +----+----------+---------+ | 2 | Thanh | 1500.00 | | 2 | Nga | 1500.00 | | 1 | Manh | 2000.00 | | 2 | Manh | 1500.00 | | 3 | Manh | 2000.00 | | 6 | Manh | 4500.00 | | 1 | Huy | 2000.00 | | 2 | Huy | 1500.00 | | 3 | Huy | 2000.00 | | 4 | Huy | 6500.00 | | 6 | Huy | 4500.00 | | 1 | Cao | 2000.00 | | 2 | Cao | 1500.00 | | 3 | Cao | 2000.00 | | 1 | Lam | 2000.00 | | 2 | Lam | 1500.00 | | 3 | Lam | 2000.00 | | 4 | Lam | 6500.00 | | 5 | Lam | 8500.00 | | 6 | Lam | 4500.00 | +----+----------+---------+See more types of JOIN:
- INNER JOIN - Returns records with matching values between two tables.
- LEFT JOIN - Returns all records from the left panel and matching records from the right panel.
- RIGHT JOIN - Returns all records from the right panel and the appropriate records from the left panel.
- FULL JOIN - Returns all records in the left panel and the table must be combined.
- CARTESIAN JOIN - Returns the Cartesian product of record sets from two or more combined tables.
3.5 ★ | 2 Vote
You should read it
May be interested
- How to cut and join MP3 files simply and quicklyhow to cut and join mp3 files simply and quickly. today tipsmake.com would like to introduce to you the ways to cut and join mp3 files in a simple, fast and easy way. let's find out what those are.
- Instructions for reviewing WhatsApp group participantsto secure the whatsapp group and the content in the whatsapp chat group, the admin should control who joins the group. at that time, those who want to join the whatsapp group must submit a request and admin review to be able to access the group.
- How to join Windows Insider to download and experience the latest Builds?each year microsoft will release some major windows 10 update updates. however, if you want to 'have the right' new updates available, you will have to join the windows insider program. as a member, you can download updates completely for free with just a few mouse clicks. then your computer will load new builds (aka versions).
- How to join Discord voice chat on PS5gone are the days when you had to join discord on your phone when playing on playstation 5. thanks to the ps5 game base update, you can now link discord to your ps5 and join live voice chat.
- Instructions for joining the domain on Windows 8.1 (Part 2)since nt 3.5, microsoft client operating systems are joined to the windows domain. each new client uses the same technique as in the previous article that the network administrator introduced to you. however, to the next version of the operating system, microsoft adjusted additional steps in the process of joining the domain to resolve dns connectivity issues and secure configuration settings.
- Instructions to join, join videos by Format Factorycurrently there are many software that supports you to join video files together, one of which is format factory software. format factory supports you to join video files quickly and effectively.
- Instructions for fastest online MP3 music mixingif you want to join mp3 music without installing software for trouble, use the online music matching feature of the websites below, the quality is not inferior to the professional software.
- How to Merge AVI Filesavi (audio video interleave) is a multimedia file format used to create and play back movies. you can merge avi files to join multiple short clips and create 1 final full-length video. there are many programs that allow you to join avi...
- How to split and join files with FFSJffsj can support users to cut files (splitting) and join files (joining) quickly, without file errors and can set security with very simple operation, easy-to-use interface.
- How to join PDF files simply and quicklymerge pdf files or merge pdf files together to help you manage small pdf files more easily or save time when having to send multiple files to others. there are many ways to join multiple pdf files together. tipsmake.com will guide