Sunday, December 26, 2010

SQL Server Interview Questions and Answers : Series 9

Question: Write a query to find the nth minimum and maximum
Answer: For Minimum
Select * From table t1 Where
    (n-1) = (Select Count(Distinct(t2.column)) From table t2 Where
                                t2.column < t1.column)
For maximum
Select * From table t1 Where
    (n-1) = (Select Count(Distinct(t2.column)) From table t2 Where
                                t2.column > t1.column)
Question: Write a query to get the last record of the table.
Answer: select top 1 * from table e1 order by id desc

Question: Write a query to get the no rows of a table without using count or any other clause.
Answer: There is the view sys.partitions is the view which contains the information about the rows in the table. By providing the object id of table you can get rows for table.

Question: Write the create syntax for the procedure.
Answer: Create Procedure  PSample (p1 int ,p2 int ) AS
Begin
Select * from table
End

Question: Write the Create syntax for Views.
Answer: Create View Emap
AS
select FirstName,MiddleName,lastname from xyz


GO BACK TO MAIN QUESTIONS LIST


No comments:

Post a Comment