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

Dbms Notes

Uploaded by

Prasad nikam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Dbms Notes

Uploaded by

Prasad nikam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Outer joins are of following three types.

1. Left outer join


2. Right outer join
3. Full outer join
Creating a database : Run the following command to create a database.
Create database testdb;
Using the database : Run the following command to use a database.
use testdb;
Adding table to the database : Run the following command to add tables to
a database.
CREATE TABLE Students (
StudentID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Inserting rows into database :
INSERT INTO students (
StudentID,
LastName,
FirstName,
Address,
City
)
VALUES
(
111,
'James',
'Johnson',
'USA',
california
);
Output of database :
Type the following command to get output.
SELECT * FROM students;

Types of outer join :


1.Left Outer Join : The left join operation returns all record from left table
and matching records from the right table. On a matching element not found
in right table, NULL is represented in that case.

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

SQL Right Join


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

The syntax for a RIGHT JOIN is :-


SELECT column_name(s)
FROM tableA
RIGHT JOIN tableB ON tableA.column_name = tableB.column_name;
SQL RIGHT JOIN EXAMPLE :
In this example we will consider two tables employee table containing
details of the employees working in the particular department the
and department table containing the details of the department
employee table :
emp_no emp_name dept_no

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

E1 Varun Singhal IT Delhi

E2 Amrita Aggarwal HR Hyderabad


emp_no emp_name d_name location

E3 Ravi Anand Finance Pune

[NULL] [NULL] Testing Noida

[NULL] [NULL] Marketing Mathura

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.

SQL Left 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
);

INSERT INTO Emp (EmpID, Name, Country, Age, Salary, department_id)


VALUES (1, 'Shubham', 'India', 23, 30000, 101),
(2, 'Aman', 'Australia', 21, 45000, 102),
(3, 'Naveen', 'Sri Lanka', 24, 40000, 103),
(4, 'Aditya', 'Austria', 21, 35000, 104),
(5, 'Nishant', 'Spain', 22, 25000, 101);
Output:

Department Table
Query:

CREATE TABLE department (


department_id INT PRIMARY KEY,
department_name VARCHAR(50),
department_head VARCHAR(50),
location VARCHAR(50)
);

INSERT INTO department (department_id, department_name,


department_head, location)
VALUES (101, 'Sales', 'Sarah', 'New York'),
(102, 'Marketing', 'Jay', 'London'),
(103, 'Finance', 'Lavish', 'San Francisco'),
(104, 'Engineering', 'Kabir', 'Bangalore');
Select * from department;
Output:

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:

You might also like