Use this space to put some text. Update this text in HTML

468x60 banner ad

Advertise with Us

Powered by Blogger.

Friday, 18 March 2016

Sql From Clause


FROM Clause

This SQL tutorial explains how to use the SQL FROM clause with definition, syntax and examples.

Definition:
The FROM clause specifies one or more tables or joining of the tables containing the data that the query retrieves from.
 
Syntax:

Select columnName1, columnName2,…N  
 From TableName

SELECT * FROM TableName1

INNER JOIN TableName2

ON TableName1.ID=TableName2.ID




SELECT * FROM TableName1

LEFT JOIN TableName2

ON TableName1.ID=TableName2.ID


SELECT * FROM TableName1
RIGHT JOIN TableName2
ON TableName1.ID=TableName2.ID

SELECT * FROM TableName1
FULL JOINTableName2
ON TableName1.ID=TableName2.ID


Example- With one table
 
CREATE TABLE [dbo].[Student]
(
      [StudentID] [int] IDENTITY(1,1) NOT NULL,
      [StudentName] [varchar](50) NULL,
      [StandardId] [int] NOT NULL,
)
 


INSERT [dbo].[Student] ([StudentName], [StandardId]) VALUES ('Pankaj', 1)

INSERT [dbo].[Student] ([StudentName], [StandardId]) VALUES ('Santosh', 1)

INSERT [dbo].[Student] ([StudentName], [StandardId]) VALUES ('Mahesh', 2)

INSERT [dbo].[Student] ([StudentName], [StandardId]) VALUES ('Deepak', 2)
 

Example1:
 

SELECT StudentID,StudentName
FROM Student

Example2:
SELECT StudentID,StudentName
FROM Student
WHERE StudentID=6

Example3:


SELECT StudentID,StudentName

FROM Student

WHERE StudentID >4
 

Example- Two tables with Inner Join
 

CREATE TABLE [dbo].[StudentAddress]

(

      [StudentID] [int] NOT NULL,

      [Address1] [varchar](50) NOT NULL,
      [Address2] [varchar](50) NULL,
      [City] [varchar](50) NOT NULL,
      [State] [varchar](50) NOT NULL
 )

INSERT [dbo].[StudentAddress] ([Address1], [Address2], [City], [State]) VALUES ('A-145, Rajendra Place, Street No.-3, Delhi', NULL, 'Delhi', 'Delhi')



INSERT [dbo].[StudentAddress] ([Address1], [Address2], [City], [State]) VALUES ('B-825, Gagan Marg, Street No-42, Rajsthan', NULL, 'Rajsthan', 'Udaypur')
 
  
Example1:


SELECT  Student.StudentName, StudentAddress.Address1

FROM Student

INNER JOIN StudentAddress

ON Student.StudentID=StudentAddress.StudentID
 

 


Example- Two tables with Left Join

SELECT Student.StudentName, StudentAddress.Address1 FROM Student

LEFT JOIN StudentAddress

ON Student.StudentID=StudentAddress.StudentID




 


Example- Two tables with Right Join


SELECT Student.StudentName, StudentAddress.Address1 FROM Student

RIGHT JOIN StudentAddress

ON Student.StudentID=StudentAddress.StudentID
 



Example- Two tables with Full Join

SELECT Student.StudentName, StudentAddress.Address1 FROM Student

FULL JOIN StudentAddress

ON Student.StudentID=StudentAddress.StudentID
 









No comments :

Post a Comment

Ask a Question?