Dbms Notes
Dbms Notes
Syntax :
SELECT column_name(s)
FROM table1
LEFT JOIN Table2
ON Table1.Column_Name=table2.column_name;
2. Right Outer Join : The right join operation returns all record from right
table and matching records from the left table. On a matching element not
found in left table, NULL is represented in that case.
Syntax :
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
3. Full Outer Join : The full outer Join keyword returns all records when
there is a match in left or right table records.
Syntax:
SELECT column_name
FROM table1
FULL OUTER JOIN table2
ON table1.columnName = table2.columnName
WHERE condition;
Example :
Creating 1st Sample table students.
CREATE TABLE students (
id INTEGER,
name TEXT NOT NULL,
gender TEXT NOT NULL
);
-- insert some values
INSERT INTO students VALUES (1, 'Ryan', 'M');
INSERT INTO students VALUES (2, 'Joanna', 'F');
INSERT INTO students Values (3, 'Moana', 'F');
Creating 2nd sample table college.
CREATE TABLE college (
id INTEGER,
classTeacher TEXT NOT NULL,
Strength TEXT NOT NULL
);
-- insert some values
INSERT INTO college VALUES (1, 'Alpha', '50');
INSERT INTO college VALUES (2, 'Romeo', '60');
INSERT INTO college Values (3, 'Charlie', '55');
Performing outer join on above two tables.
SELECT College.classTeacher, students.id
FROM College
FULL OUTER JOIN College ON College.id=students.id
ORDER BY College.classTeacher;
The above code will perform a full outer join on tables students and college
and will return the output that matches the id of college with id of students.
The output will be class Teacher from college table and id from students
table. The table will be ordered by class Teacher from college table.
Class Teacher Id
Alpha 1
Romeo 2
Charlie 3
The RIGHT JOIN keyword in SQL returns the all matching records(or
rows) and the records(or rows) which are present in the right table but
not in the left table .That means that, if a certain row is present in the right
table but not in the left, the result will include this row but with a NULL value
in each column from the left . If a record from the left table is not in the right,
it will not be included in the result.
RIGHT JOIN
E1 Varun Singhal D1
E2 Amrita Aggarwal D2
E3 Ravi Anand D3
department table :
dept_no d_name location
D1 IT Delhi
D2 HR Hyderabad
D3 Finance Pune
D4 Testing Noida
D5 Marketing Mathura
To perform right- join on these two tables we will use the following SQL
query:
select emp_no , emp_name ,d_name, location
from employee
right join dept on employee.dept_no = department.dept_no;
The output that we will get is as follows :
emp_no emp_name d_name location
As right join gives the matching rows and the rows that are present in the
right table but not in the left table. Here in this example, we see that the
department that contains no employee contains [NULL] values of emp_no
and emp_name after performing the right join.
The LEFT JOIN keyword in SQL returns all matching records(or rows) and the
records(or rows) that are present in the left table but not in the right table.
That means that, if a certain row is present in the left table but not in the right, the
result will include this row but with a NULL value in each column from the right. If a
record from the right table is not on the left, it will not be included in the result.
LEFT JOIN
Syntax
SELECT column_name(s)
FROM tableA
LEFT JOIN tableB ON tableA.column_name = tableB.column_name;
1`Example
In this example, we will consider two tables Emp containing details of the
Employee working in the particular department, and department table
containing the details of the department
Employee Table
Query:
CREATE TABLE Emp (
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
Country VARCHAR(50),
Age INT,
Salary INT,
department_id INT
);
Department Table
Query:
To perform left-join on these two tables we will use the following SQL
query :
SELECT Emp.EmpID, Emp.Name, department.
department_name, department.department_head,
department.location
FROM Emp
LEFT JOIN department ON Emp.department_id
= department.department_id;
Output: