Wednesday 29 February 2012

Sql Quries

Create Table
     Create table tablename(sNo int , name nvarchar(250))

Set Primarykey
    Create table tablename(sNo int identity Primary Key, name nvarchar(250))

Set ForeignKey
   Create table tablename(sNo int identity Primary Key, name nvarchar(250) foreign key references secondTableName(columnName))

Insert query
      Insert into tableName(colName1,colName1)values(value1,value2);

Update query
     Update tableName set ColName1=value1,ColName1=value1 where Condition;

Delete query
     Delete from tableName where condition; //condition -->columnName=value        like this

Select query
     Select  * from tableName; //get all column value
     Select  tableName.columnName1,tableName.columnName1 from tableName; //get specified column only
     Select  * from tableName where Condition;// get all column value based on the condition
     Select  tableName.columnName1 from tableName; //get specified column value based on the condition

Inner  join
    Select tableName1.columnName1,tableName1.columnName2,tableName2.columnName from tableName1 inner join tableName1 on tableName1.columnName1=tableName2.columnName1

Left Outer join
Use this when you only want to return rows that have matching data in the left table, even if there's no matching rows in the right table.


   SELECT * FROM TableName1 LEFT JOIN TableName2 AS ON TableName1.Id = TableName1.Id


Right Outer join
Use this when you only want to return rows that have matching data in the right table, even if there's no matching rows in the left table.

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

Full Outer join
Use this when you want to all rows, even if there's no matching rows in the right table.

SELECT * FROM TableName1 FULL JOIN TableName2 ON TableName1.Id = TableName2.Id

Count(*)
 If we only want to see how many records are in a table (but not actually view those records), we could use COUNT(*). COUNT(*) returns everything - including null values and duplicates.

SELECT COUNT(*) FROM Individual

Sql Ordey By
   Select * from Tablename Order By columnName

Between  for date
   select columnName from Tablename where DatecolumnName  between '4/6/2010' and '4/10/2010'

No comments:

Post a Comment