Table of Contents
This guide covers the clause to combine join data in SQL with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
In this article, TipsMake will give you detailed instructions on how to use the JOIN clause in SQL with specific examples to make it easier to visualize and capture commands better.
Assuming there are two tables, NHANVIEN And TIENTHUONG Have 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 | +----+----------+-----+-----------+----------+
Table 2: TIENTHUONG
+-----+---------------------+-------------+--------+ |TT_ID| NGAY | NHANVIEN_ID | SOTIEN | +-----+---------------------+-------------+--------+ | 102 | 2019-01-08 00:00:00 | 3 | 3000 | | 100 | 2019-01-08 00:00:00 | 3 | 1500 | | 101 | 2019-02-20 00:00:00 | 2 | 1560 | | 103 | 2018-12-20 00:00:00 | 4 | 2060 | +-----+---------------------+-------------+--------+
Now, we combine these two tables in the SELECT statement as follows:
SQL> SELECT ID, TEN, TUOI, SOTIENFROM NHANVIEN, TIENTHUONGWHERE NHANVIEN.ID = TIENTHUONG.NHANVIEN_ID;
The result is:
+----+----------+-----+--------+ | ID | TEN | TUOI| SOTIEN | +----+----------+-----+--------+ | 3 | Nga | 23 | 3000 | | 3 | Nga | 23 | 1500 | | 2 | Loan | 25 | 1560 | | 4 | Manh | 25 | 2060 | +----+----------+-----+--------+
You can see here that JOIN is executed in the WHERE clause. Some operators can be used to combine tables: =, <, >, <>, <=, > =, ! =, BETWEEN, LIKE, And NOT. However, the most commonly used operator is The equal sign (=).
JOIN Types in SQL
In SQL, there are several types of JOINs available:
JOIN typeMeaning 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. The result is NULL from the right side if there is no match. RIGHT JOIN Returns all records from the right panel and the appropriate records from the left panel. The result is NULL from the left side if there is no matching result. FULL JOIN Returns all records in the left table and the table must be combined. SELF JOIN Used to combine a table with itself as if the table is considered two tables, replace at least one temporary table name in the SQL statement. CARTESIAN JOIN Returns the Cartesian product of record sets from two or more combined tables.
In the following articles, Quantum will discuss with you more about each of the JOIN types mentioned above. Have you remembered to it!
Previous article: Constraints in SQL
Next lesson: UNION clause in SQL
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.