You are on page 1of 17

JOINS

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

SELECT Orders.OrderID, Customers.CustomerName, Inner Join


Shippers.ShipperName
FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID =
Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID =
Shippers.ShipperID);
Let’s take an example…
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
Solve this one!
SELECT ProductID, ProductName, CategoryName
FROM Products
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;
LEFT JOIN Syntax RIGHT JOIN Syntax

SELECT column_name(s SELECT column_name(s


) )
FROM table1 FROM table1 SQL left and
LEFT JOIN table2
ON table1.column_name
RIGHT JOIN table2
ON table1.column_name
right join
= table2.column_name; = table2.column_name;
Let’s take an example…
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
What if it were a right join?
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
The FULL OUTER JOIN keyword returns all records
when there is a match in left (table1) or right (table2)
table records.

Note: FULL OUTER JOIN can potentially return


very large result-sets!
Full outer Tip: FULL OUTER JOIN and FULL JOIN are the
same.

join
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
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;

Self Join

SELECT e.Name EmployeeName, m.name AS


ManagerName
FROM Employee e INNER JOIN Employee m
ON e.ManagerID = m.EmployeeID
Write an SQL query to print details of workers excluding
first names, “Vipul” and “Satish” from Worker table.
The required query is:
Select * from Worker where FIRST_NAME not in
('Vipul','Satish’);

SQL DML Write an SQL query to print details of Workers with


DEPARTMENT name as “Admin”.
The required query is:
exercises Select * from Worker where DEPARTMENT like 'Admin
%';

Write an SQL query to print details of the Workers whose


FIRST_NAME contains ‘a’.
The required query is:
Select * from Worker where FIRST_NAME like '%a%';
Write an SQL query to fetch the no. of workers for each
department in the descending order.
The required query is:
SELECT DEPARTMENT, count(WORKER_ID) No_Of_Workers
FROM worker GROUP BY DEPARTMENT ORDER BY
No_Of_Workers DESC;

SQL DML Write an SQL query to fetch the list of employees with the same
salary.
The required query is:

exercises Select distinct W.WORKER_ID, W.FIRST_NAME, W.Salary


from Worker W, Worker W1
where W.Salary = W1.Salary and W.WORKER_ID !=
W1.WORKER_ID;

Write an SQL query to show the second highest salary from a table.
The required query is:
Select max(Salary) from Worker where Salary not in (Select
max(Salary) from Worker);
For a video recap of
today’s content:
https://www.youtube.com/watch?v=9yeOJ0ZMUYw

You might also like