Friday 14 February 2014

SQL basic Query used in asp.net

1)Create table Query
-------------------------------------------------------------------------------------------
CREATE TABLE tbl_StudentData(Id number(3) primary key identity(1,1),
                            Roll number(3),
                            Name varchar2(50),
                           Age number(2),
                           Address varchar2(100)
                           desc varachar2(200)
                           )

2)insert data in table
------------------------------------------------------------------------------------------------ 
 INSERT INTO tbl_StudentData (Roll,Name,Age,Address,desc) VALUES(1,'Hitesh Kaklotar',22,'Surat Gujarat','problemaspdotnet.blogspot.in')

3)Display data from table
------------------------------------------------------------------------------------------------
     --> display all row and all column
    SELECT * FROM tbl_StudentData

     --> display selected column and all row
    SELECT Name,Address FROM tbl_StudentData

    --> display selected roew and all column
    SELECT * FROM tbl_StudentData WHERE Id=1    Or
    select * FROM tbl_StudentData WHERE Age>21
   
    -->display selected row and selected column
    SELECT Name,Age FROM tbl_StudentData WHERE Id=1

4)Update data in table
------------------------------------------------------------------------------------------------
   --> Update all record
    UPDATE tbl_StudentData SET Desc='problemaspdotnet.blogspot.in'       

    --> update record with condition
    UPDATE tbl_StudentData SET name='kaklotar hitesh', Age=23, Address="Tared Mahuva Bhavnagar" WHERE Id=1        

5)Delete data in table
------------------------------------------------------------------------------------------------
   --> delete all record
    DELETE FROM tbl_StudetData

    --> delete record with condition
    DELETE FROM tbl_StudetData WHERE Id=2
   

6)Destroying table
------------------------------------------------------------------------------------------------
   DROP TABLE tbl_tablename

7)Truncate table
------------------------------------------------------------------------------------------------
    --> You can also uset TRUNCATE command for all redord delete in table
    TRUNCATE TABLE tbl_StudentData

8)Display data with sorting asscnding or descending
------------------------------------------------------------------------------------------------   
    --> display data ascending order
    SELECT * FROM tbl_studentData ORDER BY Name

    --> display data descending order
    SELECT * FROM tbl_studentData ORDER BY Name DESC

9)Group by Query
    --> Here table tbl_StudentData also add one column name city.
    --> Suppose you want to get record how many student's city is same OR
    --> how many student comes for different diffrent city , then use following Query
------------------------------------------------------------------------------------------------
SELECT COUNT(Id) AS student_For_one_City ,City  FROM tbl_StudentData GROUP BY (City)

10)Types of  Join

    --> Add one table name tbl_Hostel(Field:id,Hostel_Name)
   
    -->Simple join
------------------------------------------------------------------------------------------------
    SELECT tbl_StudentData.Name,tbl_Hostel.Hostel_Name FROM tbl_StudentData WHERE tbl_StudentData.Id=tbl_Hostel .Id
   
    -->Inner join
------------------------------------------------------------------------------------------------
    SELECT ts.Name,th.Hostel_Name FROM tbl_StudentData ts INNER JOIN tbl_Hostel th ON ts.Id=th.Id

    -->Left join
------------------------------------------------------------------------------------------------
    SELECT ts.Name,th.Hostel_Name FROM tbl_StudentData ts LEFT JOIN tbl_Hostel th ON ts.Id=th.Id

    -->Right join
------------------------------------------------------------------------------------------------
    SELECT ts.Name,th.Hostel_Name FROM tbl_StudentData ts RIGHT JOIN tbl_Hostel th ON ts.Id=th.Id

    -->Full join(Cross join)
------------------------------------------------------------------------------------------------
    SELECT ts.Name,th.Hostel_Name FROM tbl_StudentData ts FULL OUTER JOIN tbl_Hostel th ON ts.Id=th.Id

       
11)Aggregate function(max,min,top,Count)
------------------------------------------------------------------------------------------------
   -->if you want to get max id in table that time use max() function select Query
    SELECT MAX(id) as max_id FROM tbl_StudentData

    --->if you want to get min id in table that time use min() function select Query
    SELECT MIN(id) as min_id FROM tbl_StudentData

    -->if you want to get top 10 record from the table then use following query
    SELECT TOP(10),* FROM tbl_StudentData    

    --> count the no. of record in table then use following query       
    SELECT COUNT(id) as Total_Record FROM tbl_StudentData

 12)Query operator
    --> You can also use Query operator like or,and ,>,<,is,In,between,is null,not etc.
           where condition in your select ,delete and update query
-----------------------------------------------------------------------------------------------

  SELECT Name FROM tbl_StudentData WHERE id<10 AND Age IS NULL 

No comments:

Post a Comment



Asp.net tutorials