SQL Join 157a (6) Flashcards
SQL Join
● As the name shows, JOIN means to combine something. In case of SQL, JOIN means “to
combine two or more tables”.
● The SQL JOIN clause takes records from two or more tables in a database and combines it
together.
Why is SQL join used?
● If you want to access more than one table through a select statement.
● If you want to combine two or more table then SQL JOIN statement is used .it
combines rows of that tables in one table and one can retrieve the information by a
SELECT statement.
● The joining of two or more tables is based on common field between them.
● SQL INNER JOIN also known as simple join is the most common type of join.
What are the 4 different SQL joins?
For examples look at slides
- Inner join
- left join
- right join
- full join
Inner Join
For examples look at slides
● INNER JOIN selects records that have matching values in both tables as long as the condition is satisfied. It returns the combination of all rows from both the tables where the condition satisfies.
- In other words combines the things that they have in common
Syntax: SELECT table1.column1,table1.colmun2, table2.column1,…FROM table1 INNER JOIN table2 ON table1.matching_column = table2.matching_column;
Left Join
For examples look at slides
● The SQL left join returns all the values from left table and the matching values from the right table. If there is no matching join value, it will return NULL.
- In other words gets everything from the left + what the right side has in common with the left side.
Syntax:
SELECT table1.column1, table1.column2, table2.column1,…. FROM table1
LEFT JOIN table2 ON table1.matching_column = table2.matching_column;
Right Join
For examples look at slides
● The SQL RIGHT JOIN returns all the values from the values from the rows of right table and the matched values from the left table. If there is no matching in both tables, it will return NULL
- In other words gets everything from the right plus what the left side has in common with the right side.
Syntax - SELECT table1.column1, table1.column2, table2.column1,…. FROM table1 RIGHT JOIN table2 ON table1.matching_column = table2.matching_column;
Full Join
For examples look at slides
● In SQL, FULL JOIN is the result of a combination of both left and right outer join. Join tables have all the records from both tables. It puts NULL on the place of matches not found.
- Combines everything from both tables
Syntax - SELECT table1.column1, table1.column2, table2.column1,…. FROM table1 FULL JOIN table2 ON table1.matching_column = table2.matching_column;