INNER JOIN and LEFT JOIN - SQL Query

The Joins are used to combine rows from two or more tables, based on the common field between two tables.

The telecom company has a list of some clients and transactions of their paid bill payments.

Consider the following tables Table1 and Table2.

Table1: Clients

C_IDClient_NameClient_Phone
1Alex9022222222
2Mark9033333333

Table2: Payment

C_IDTransaction_IDAmount
1101355
2102255
3103300
4104400

INNER JOIN

Q. Write a query to display the record that contain columns such as C_ID, Client_Name, Amount from Table1 and Table2.

Answer:

The inner join selects all rows from both columns, if there is a match between them. The column 'C_ID' is common to both column.

inner join

SELECT Client.Client_Name, Payment.Transaction_ID, Payment.Amount
FROM Clients
INNER JOIN Payment
ON Payment.C_ID = Clients.C_ID


Output:

C_IDClient_NameAmount
1Alex355
2Mark255

LEFT JOIN or LEFT OUTER JOIN

Q. From given tables, write a query to display record that contain columns such as C_ID, Client_Name, Amount from Table1 and Table2.

Answer:

Consider table1 as left table and table2 as right table.

left join

SELECT Clients.Client_Name, Clients.Client_Phone, Payment.Amount
FROM Clients
LEFT JOIN Payment
ON Payment.C_ID = Payment.C_ID


Output:

C_IDClient_NameAmount
1Alex355
2Mark255