0% found this document useful (0 votes)
13 views

sql join

class 12 notes sql
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

sql join

class 12 notes sql
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL JOIN

A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.

Let's look at a selection from the "Orders" table:

hen, look at a selection from the "Customers" table:

Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the
"Customers" table. The relationship between the two tables above is the "CustomerID"
column.

Then, we can create the following SQL statement (that contains an INNER JOIN), that selects
records that have matching values in both tables:

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate


FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

and it will produce something like this:

Different Types of SQL JOINs


Here are the different types of the JOINs in SQL:

 (INNER) JOIN: Returns records that have matching values in both tables
 LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records
from the right table
 RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records
from the left table
 FULL (OUTER) JOIN: Returns all records when there is a match in either left or right
table

INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.

Let's look at a selection of the Products table:

We will join the Products table with the Categories table, by using the CategoryID field from
both tables:
SELECT ProductID, ProductName, CategoryName
FROM Products
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;

Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

SQL LEFT JOIN Keyword


The LEFT JOIN keyword returns all records from the left table (table1), and the matching
records from the right table (table2). The result is 0 records from the right side, if there is no
match.

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

SQL RIGHT JOIN Keyword


The RIGHT JOIN keyword returns all records from the right table (table2), and the matching
records from the left table (table1). The result is 0 records from the left side, if there is no
match.

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

SQL FULL OUTER JOIN Keyword


The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right
(table2) table records.

Tip: FULL OUTER JOIN and FULL JOIN are the same.

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

SQL Self Join


A self join is a regular join, but the table is joined with itself.

SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
The SQL UNION Operator
The UNION operator is used to combine the result-set of two or more SELECT statements.

 Every SELECT statement within UNION must have the same number of columns
 The columns must also have similar data types
 The columns in every SELECT statement must also be in the same order

SELECT column_name(s) FROM table1


UNION
SELECT column_name(s) FROM table2;

UNION ALL Syntax


The UNION operator selects only distinct values by default. To allow duplicate values, use UNION
ALL:

SELECT column_name(s) FROM table1


UNION ALL
SELECT column_name(s) FROM table2;

CARTESIAN JOIN: The CARTESIAN JOIN is also known as CROSS JOIN. In a


CARTESIAN JOIN there is a join for each row of one table to every row of another table.
This usually happens when the matching column or WHERE condition is not specified.
 In the absence of a WHERE condition the CARTESIAN JOIN will behave like a
CARTESIAN PRODUCT . i.e., the number of rows in the result-set is the product of the
number of rows of the two tables.
 In the presence of WHERE condition this JOIN will function like a INNER JOIN.
 Generally speaking, Cross join is similar to an inner join where the join-condition will
always evaluate to True

SELECT table1.column1 , table1.column2, table2.column1...


FROM table1
CROSS JOIN table2;

table1: First table.


table2: Second table

In the below query we will select NAME and Age from Student table and COURSE_ID from
StudentCourse table. In the output you can see that each row of the table Student is joined
with every row of the table StudentCourse. The total rows in the result-set = 4 * 4 = 16.
SELECT Student.NAME, Student.AGE, StudentCourse.COURSE_ID
FROM Student
CROSS JOIN StudentCourse;

You might also like