Monday, 18 June 2012

Some Interview Sql Query


 Table Design For Demo
1. //To only copy the Table structure of a Existing table in database
SELECT        * INTO              Demo_linkun   FROM            Demo   WHERE        (1 = 2)
2. //To only copy the data from a Existing table after creating a new table
INSERT  INTO   Demo_linkun (col1,col2…..)  SELECT        (col1,col2…..)   FROM            Demo
                                                           OR
Insert into Demo_Linkun Select * from Demo
3.//To copy both Table  structure and data from a  Existing table
SELECT        *   INTO              Demo_linkun    FROM            Demo        
4.//To retrieve the Table  structure as well data from the table
SELECT        *   FROM            Demo   WHERE        (1 = 1)
OR
SELECT        *   FROM            Demo   WHERE        (0 = 0)
5.//To retrieve the Table  structure with null value from the table
SELECT        *   FROM            Demo  WHERE        (1 = 2)
OR
SELECT        *    FROM            Demo  WHERE        (1 = 0)

4.  If we have two tables and the second table contains NULL value and we have to retrieve those records which are present in first table and not present in second table. And the First table has a Primary Key which Name Is Sid and That Sid is Foreign Key in Second Table.
1. Select Sid from St1 Where NOT EXISTS (Select * from St2 where St1.Sid=St2.Sid)
OR
2. SELECT Sid FROM St1 Left Join St2 ON St1.Sid=St2.Sid where St2.Sid IS NULL


The Table one is St1 where Primary Key is Sid.
 
The Second Table is St2 where Foreign Key is Sid




Here in Table St1 There is 6 Records but in Table St2 there is Three Records ond one Null.and there are 5 and 6 records are not Added.we want to Dispplay the Records which are not add in St2 Table and Null Value.

No comments:

Post a Comment