In SQL, the ( ) operator is used to merge query result sets, filling NULL values in unmatched rows. It allows performing outer joins, avoiding Cartesian products, and comes in two types: left outer join and right outer join. Left and right outer joins will return all rows from the left or right table, filling in NULL values for unmatched rows.
Usage of ( ) in SQL
In SQL query, ( ) operator is used to combine two The query result sets are merged and missing rows are filled with NULL values.
Syntax:
<code>SELECT column_list
FROM table1
LEFT|RIGHT (+) JOIN table2
ON join_condition;</code>
Function:
-
Outer connection: ( ) operation operator allows you to perform an outer join, which returns a result set that contains matching rows from both tables and all rows from one or both tables.
-
Fill missing rows: For unmatched rows, the () operator inserts NULL values in the missing table.
-
Avoid Cartesian product: In an inner join, if there are no matching rows, the query will return an empty result set. Using the ( ) operator you can fill in missing rows and avoid the Cartesian product.
Type:
-
Left Outer Join: LEFT ( ) JOIN returns all rows from the left table and No matching left table rows in the right table are populated with NULL values.
-
Right Outer Join: RIGHT ( ) JOIN returns all rows from the right table and fills NULL values for right table rows that do not have a match in the left table.
Example:
Suppose we have the following two tables:
students |
id |
name |
1 |
John |
2 |
Mary |
##courses |
##id
course_name |
|
1
Math |
|
2 Science |
|
3
History |
|
##The following query uses LEFT ( ) JOIN to join these two Tables are joined and missing rows are filled in:
<code>SELECT *
FROM students
LEFT (+) JOIN courses
ON students.id = courses.id;</code>
Result:
##id
name course_name |
|
|
1
John
Math |
| ##2 | Mary
Science |
| NULL | NULL
History |
|
The above is the detailed content of Usage of (+) in sql. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn