Monday 21 July 2014

Different Types of SQL Joins

S

ql joins are used to fetch/retrieve data from two or more data tables, based on a join condition. A join condition is a relationship among some columns in the data tables that take part in Sql join. Basically data tables are related to each other with keys.

I have 2 Tables  in sql server,
Table 1 with Primary Key

Table2 with Foreign Key

Types of Joins
In Sql Server we have only three types of joins. Using these joins we fetch the data from multiple tables based on condition.
Inner Join
Inner join returns only those records/rows that match/exists in both the tables. Syntax for Inner Join is as


Outer Join
  
    refer the  following url : Click here

Cross Join

Cross join is a cartesian join means cartesian product of both the tables. This join does not need any condition to join two tables. This join returns records/rows that are multiplication of record number from both the tables means each row on left table will related to each row of right table. Syntax for right outer Join is as :


Self Join 

Self join is used to join a database table to itself, particularly when the table has a Foreign key that references its own Primary Key. Basically we have only three types of joins : Inner join, Outer join and Cross join. We use any of these three JOINS to join a table to itself. Hence Self join is not a type of Sql join.
To understand Self Join, suppose we following two tables and data in these two tables is shown in figure.

CREATE TABLE emp
(
id int NOT NULL primary key,
name varchar(100) NULL,
designation varchar(50) NULL,
supid int foreign key references emp(id) ) -- In this table we have a Foreign key supid that references its own Primary Key id. We use it for Self Join
INSERT INTO emp(id,name,designation) VALUES(1,'mohan','Manger')
INSERT INTO emp(id,name,designation,supid) VALUES(2,'raj kumar','SE',1)
 INSERT INTO emp(id,name,designation) VALUES(3,'bipul kumar','Manager')
 INSERT INTO emp(id,name,designation,supid) VALUES(4,'mrinal kumar','SE',2)
 INSERT INTO emp(id,name,designation,supid) VALUES(5,'jitendra kumar','SE',2)

 select * from emp

Code for SelfJoin is : 



Regards: Suraj K Mad.

Basics of SQL Commands

SQL commands are a set of instructions that are used to interact with the database like Sql Server, MySql, Oracle etc. SQL commands are responsible to create and to do all the manipulation on the database. These are also responsible to give/take out access rights on a particular database

Sql Commands Category

We have different sql commands for different-different purpose. We can grouped Sql Commands into five major categories depending on their functionality.

01.       Data Definition Language (DDL)

These SQL commands are used to create, modify, and drop the structure of database objects like table, view, procedure, indexes etc. In this category we have CREATE, ALTER, DROP and TRUNCATE commands.

Note:-
1.            Only with DDL commands we need to write keyword (like table, procedure, view, index, function) with the syntax of command.
2.            These commands are used to create/modify the structure of the database object.

Example:
CREATE TABLE TABLE_NAME
(
COL1 VARCHAR(10),
COL2 VARCHAR(20),
);
--Here "TABLE" is a keyword that is used to create table "TABLE_NAME"
CREATE VIEW VIEW_NAME
AS
BEGIN
 SELECT * FROM EMP_TABLE
END
--Here "VIEW" is a keyword that is used to create VIEW "VIEW_NAME"

02.      Data Manipulation Language (DML)

These SQL commands are used to store, modify, and delete data from database tables. In this category we have INSERT, UPDATE, and DELETE commands.

03.     Data Query Language (DQL)

These SQL commands are used to fetch/retrieve data from database tables. In this category we have only SEELCT command.

04.     Transaction Control Language (TCL)

These SQL commands are used to handle changes which affect the data in database. Basically we use these commands with in the transaction or to make a stable point during changes in database at which we can rollback the database state if required. In this category we have SAVEPOINT, ROLLBACK and COMMIT commands.

05.      Data Control Language (DCL)

These SQL commands are used to implement security on database objects like table,view,stored procedure etc. In this category we have GRANT and REVOKE commands.

Note:-

Grant Command : This command is used to give permission to specific users on specific database objects like table, view etc.

Revoke Command : This command is used to take out permission from specific users on specific database objects like table, view etc.