Wednesday, January 31, 2024

What is Right Join?

 The SQL right join performs an inner join first with the matching values from the left table and the right table and then it returns all other records that are present in the right table and not present in the left table.




Example




Table_1


create table Table_1 (A varchar);

Insert into Table_1 (A)

values ('x'),('x'),('z'),('0');


select * from Table_1;


Table_2

create table Table_2 (A varchar);

Insert into Table_2 (A)

values ('y'),('x'),('0'),('x');

 

select * from Table_2;


Syntax 

SELECT table1.column1, table1.column2, table2.column1,....
FROM table1 RIGHT JOIN table2 
ON table1.matching_column = table2.matching_column;


Query for Right Join

--Right join--

select *from Table_1 T1

right join Table_2 T2

on T1.A=T2.A;






Tuesday, January 30, 2024

Left Join

 What is Left Join?

The SQL left join performs an inner join first with the matching values from the right table and it returns all the values from the left table even if there is no matching join value, it will return a corresponding NULL based on a matching row.















Example
























Table_1

create table Table_1 (A varchar);

Insert into Table_1 (A)

values ('x'),('x'),('x'),('0'),('0'),('z');

 

select * from Table_1;



Table_2

create table Table_2 (A varchar);

Insert into Table_2 (A)

values ('y'),('y'),('0'),('x'),('x');

 

select * from Table_2;



Syntax for Left Join

SELECT table1.column1, table1.column2, table2.column1,....

FROM table1

LEFT JOIN table2

ON table1.matching_column = table2.matching_column;


--left join query--

select *from Table_1 T1

left join Table_2 T2

on T1.A=T2.A;


Monday, January 29, 2024

What is Inner Join?

INNER JOIN selects records that have matching values in both tables as
It returns the combination of all rows from both tables based on a related column.


 

Example 

Table_1

create table Table_1 (A varchar);
Insert into Table_1 (A)
values ('x'),('x'),('z'),('0'),('null');


select * from Table_1;

 Table_2

create table Table_2 (A varchar);
Insert into Table_2 (A)
values ('y'),('x'),('0'),('x');


select * from Table_2;

Syntax for Inner Join


SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
LEFT JOIN table2ON table1.matching_column = table2.matching_column;

--Inner join--

select *from Table_1 T1
join Table_2 T2
on T1.A=T2.A;



Friday, January 26, 2024

Create table in SQL

Create Table in postgre SQL

It is always advisable to make small tables in SQL to practice queries. Below is an explanation of How to make a simple table in SQL.


CREATE command is used to create a new table in the database


Syntax: create table TABLE_NAME (COLUMN_NAME data type[,....]);

INSERT  command is used to create a new table in the database

Syntax: insert into TABLE_NAME (col1, col2, col3,.... col N) values (value1, value2, value3,... valueN);


Example table ;





QUERY ; 

  Syntax:

create table Table_1 (A varchar); Insert into Table_1 (A)

values ('x'),('x'),('y'),('z');

select * from Table_1;