Sunday, 6 August 2017

SQL Joins:

SQL Joins:

What is SQL JOIN?
A SQL JOIN is clause/condition used to combine rows from two or more tables, based on a related column between tables.

Different Types of SQL JOINs:
There are four different types of the JOINs in SQL:


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

1)SQL INNER JOIN:
The INNER JOIN, Returns records that have matching values in both tables.

Syntax:
SELECT column_names
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;

Example:
SELECT world.city.Name,world.city.District,world.city.Population
FROM world.city
INNER JOIN world.country ON world.city.Name=world.country.Name;


2)SQL LEFT OUTER JOIN:
The LEFT OUTER JOIN, Return all records from the left table, and the matched records from the right table.

Syntax:
SELECT column_nameS
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;

Example:
SELECT world.city.Name,world.city.District,world.city.Population
FROM world.city
LEFT JOIN world.country ON world.city.Name=world.country.Name;



3)SQL RIGHT OUTER JOIN:
The RIGHT OUTER JOIN, Return all records from the right table, and the matched records from the left table.

Syntax:
SELECT column_nameS
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;

Example:
SELECT world.city.Name,world.city.District,world.city.Population
FROM world.city
RIGHT JOIN world.country ON world.city.Name=world.country.Name;


4)SQL FULL OUTER JOIN:
The FULL OUTER JOIN, Return all records from the right and the left table when there is either match.


Syntax:
SELECT column_nameS
FROM table1
FULL JOIN table2 ON table1.column_name = table2.column_name;

Example:
SELECT *
FROM world.city FULL JOIN world.country;


No comments:

Post a Comment

Fundamentals of Python programming

Fundamentals of Python programming: Following below are the fundamental constructs of Python programming: Python Data types Python...