SQL Server Interview Questions by topic



Basic SQL Server Interview Questions

What is the difference between Having and Where clause

SQL Server Interview Questions on Temporary Tables


SQL Server Interview Questions on Indexes - Part 1

SQL Server Interview Questions on Indexes - Part 2

What is the difference between a Temporary Table and a Table Variable

What is the use of COALESCE in SQL Server

SQL Server Interview Questions on triggers

Difference between User Defined Function and Stored Procedure

SQL Server Interview Questions on Views - Part 1

SQL Server Interview Questions on Views - Part 2

Basic SQL Server Interview Questions on Joins

Explain Inner Join with an example

Explain Left Outer Join with an example

Explain Right Outer Join with an example

Explain Full Outer Join with an example

Explain Self Join with an example

What is the difference between Index Scan and Index Seek

Write a SQL Query to delete from a table that is involved in a SQL join

What are the advantages of using stored procedures?

What are the different ways to replace NULL values in SQL Server?

SQL Server interview questions on string manipulation functions

Write a Stored Procedure that takes column name as a parameter and returns the result sorted by the column that is passed

What is deferred name resolution in SQL Server?

37 comments:

  1. Can u please add more questions on SQL Server 2008? Thanks

    ReplyDelete
  2. Select * from tblA, tblB
    What is O/P.

    Ans: Cross product.
    It is a Cross Join.

    ReplyDelete
    Replies
    1. two tables data will be displayed

      Delete
  3. what is the order of execution of where having and groupby clause

    ReplyDelete
    Replies
    1. The order is first, WHERE gets executed, followed by GROUP BY and then Having clause.
      WHERE - Filters the rows to be selected from the tables
      GROUP BY - Groups the filtered rows
      HAVING - Filters the Groups
      ORDER BY - Finally sorts the groups

      Delete
  4. write a sql which will return nth(2nd highest salary) value from Employee table?

    ReplyDelete
    Replies
    1. hi

      you can find in 3 ways.

      its very easy to find any number of salary using CTE(Common table expression)

      second highest salary:

      with result as(

      select distinct salary,dense_rank() over (order by salary desc) as dens from tblempsal
      )
      select salary from result where dens=2

      just change dens=3 then it will be third highest salary so on. so that u can find wt number salary u want to find ..

      2.you can also find using < (lesser than symbol) or using NOT IN

      select max(salary) from tblempsal where salary < (select max(salary) from tblempsal)
      or
      select max(salary) from tblempsal where salary not in (select max(salary) from tblempsal)

      3. using top function
      select top 1 salary from tblempsal where salary in (select top 2 salary from tblempsal order by salary desc) order by salary asc

      Thank you
      Sunil H A
      HUNSUR
      sunilgowda_91@yahoo.com


      Delete
  5. nth highest salary from Emp table

    select * from Emp E1 where (n-1) =
    (select count(*) from Emp E2 where E1.salary <= E2.salary)

    please make a different link for all question i added as anonymous.
    Please correct my ans

    ReplyDelete
    Replies
    1. elect salary from Employees1 where salary<(SELECT MIN(salary) salary FROM ( select top n salary from Employees1 order by salary desc) AS A)

      Delete
  6. Assume you have been asked to design the database structure for job site .what NORMALIZED table data model/table design would you recommended for storing the personal data of the candidate like name , address, and phone no. and their skill in multiple langauge so as to ensure that when a company searches based on a particular set of skill. the query is quick to return the result .
    Note that each candidate would have multiple skills

    Also write a query to retrive a list of all candidate along with their skill names

    ReplyDelete
  7. A friend of mine was asked a question in an interview. Here is the question. What is coalesce in SQL and when do you use this function?

    ReplyDelete
  8. Can u please explain what is ql tunning(database tunning)?

    ReplyDelete
  9. Excuse me, how to convert a type FLOAT to INT in SQL Server, but this field that was updated, should be insert into the table where comes. How do it?
    For example: I got a field into the table XYZ of FLOAT type. I need convert this field with all his data to INT, but this registers should appear in the table XYZ. When I use: "select * from XYZ" show or print the new field with the all datas updated.

    ReplyDelete
  10. show report header above the report parameters in report view using SSRS?
    Is it possible ? pls suggest.

    ReplyDelete
  11. I have been asked a question, what are Concurrent Subqueries.

    ReplyDelete

  12. What is difference between Is Null,IsNull,= Null,Null ,' '(Empty Statement) & When it’s use ?

    ReplyDelete
  13. Find the second Max salary from max of salaries from a table having employee id and salary as columns

    ReplyDelete
    Replies
    1. select top 1 id,sal from
      (select top 2 id,sal from table_name order by sal) temp
      order by sal desc

      Delete
    2. select top 1 sal from(select distinct top 2 sal from emp order by sal desc) a order by sal

      Delete
  14. How to avoid deadlock

    ReplyDelete
  15. Sir How to connect sql server database from C# programming form

    ReplyDelete
  16. ---How to avoid Divide by zero error ?
    Create table tblcompute
    (
    Numerator int not null,
    Denominator int not null,
    Result as (Numerator / Denominator)
    )
    Go
    insert into tblcompute values (3,0)

    when we issue a select querry
    Select * from tblcompute
    Msg 8134, Level 16, State 1, Line 1
    Divide by zero error encountered.

    even after adding new row also getting the same error
    insert into tblcompute values (10,2)

    Finally i comeup with these querys!
    Select * from tblcompute where Result !=0
    SELECT Numerator , Denominator, NULLif(Result, 0) as Results from tblcompute
    SELECT Numerator , Denominator, NULLif(Result, null) as Results from tblcompute

    Can you plz make a video session for clear explanation how it works & also i wanted to show the null value results as null

    ReplyDelete
  17. How to retrieve in between records from table without using where condition for eg in 100records would like to get 20-30 records only...is it possible???

    ReplyDelete
    Replies
    1. ya it is possible ..

      solution:

      select top 30.* from table order by NEWID()

      Thank You
      Sunil H A
      Hunsur
      sunilgowda_91@yahoo.com

      Delete
  18. Mouni, try group by with having. Where and having statement have different performance with queries. You might wanna check which is more effective

    ReplyDelete
  19. sir join 2 tables employee and department and
    i want result insuch a way that

    coloumns:deptname,maxsalar,employeename

    ReplyDelete
    Replies
    1. Select DEPTNAME ,
      NAME Employeename, SAL from
      TBLEmp E
      inner join TBLDEPT D
      on E.DEPTID = D.DPTID
      Where E.Sal in (Select max(sal) From TBLemp Group by DEPTID)

      Delete
  20. write a sql which will return nth(2nd highest salary) value from Employee table?

    Answer
    select top 1 sal from(select distinct top 2 sal from emp order by sal desc) a order by sal

    Thanks.
    Sandeep nath

    ReplyDelete
  21. how to set identity column to random number generate.

    ReplyDelete
  22. can u please explain mirroring concepts?

    ReplyDelete
  23. Hi Venkat,

    When im trying to add new column in tblperson table its created with the null type.

    But when im creating the same using not null its shows in error. (Error mention below)

    Msg 4901, Level 16, State 1, Line 1
    ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'place' cannot be added to non-empty table 'tblperson' because it does not satisfy these conditions.

    ReplyDelete
  24. hello sir,
    how can avoid errors during creating tables... how can i coded it..explain me sir

    ReplyDelete
  25. Hello Sir,
    I have 2 tables T1(Id,Name) T2(Id,Name) I want to swap T1.Name to T2.Name and T2.Name to T1.Name Column values how to do it Please Answer me

    ReplyDelete
  26. Please define instances in SQL.

    ReplyDelete
  27. Hello Sir,

    Recently I attended a interview and I was asked, if a procedure is taking more time for executing what will you do and how will you do to reduce the execution time. Running each query is not an option because the procedure has more than 50 queries. Can you please tell me is there any tool or option in sql server which we can use for this.

    Thanks in advance.

    ReplyDelete
    Replies
    1. Database Engine tuning advisor tool will help you to reduce the execution time, It would suggest you indexing on columns which are part Where Clause in Select statement.

      Delete
  28. Hi Venkat my Name Is Dattatreya , I have one doubt in View
    can we delete table without deleting table related view

    ReplyDelete

If you are aware of any other sql server questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us