SQL Server Interview Questions: Part 3

  1. What are triggers?
    Triggers are special kind of stored procedures that get executed automatically when an INSERT, UPDATE or DELETE operation takes place on a table.
  2. How many triggers you can have on a table?
    In SQL Server 6.5 you could define only 3 triggers per table, one for INSERT, one for UPDATE and one for DELETE. From SQL Server 7.0 onwards, this restriction is gone, and you could create multiple triggers per each action.
  3. How to invoke a trigger on demand?
    In SQL Server 7.0 there's no way to control the order in which the triggers fire. In SQL Server 2000 you could specify which trigger fires first or fires last using sp_settriggerorder Triggers can't be invoked on demand. They get triggered only when an associated action (INSERT, UPDATE, DELETE) happens on the table on which they are defined. 
  4. What are the uses of Triggers?
    Triggers are generally used to implement business rules, auditing. Triggers can also be used to extend the referential integrity checks, but wherever possible, use constraints for this purpose, instead of triggers, as constraints are much faster. Till SQL Server 7.0, triggers fire only after the data modification operation happens. So in a way, they are called post triggers. But in SQL Server 2000 you could create pre triggers also. Search SQL Server 2000 books online for INSTEAD OF triggers.
  5. What is a self join? Explain it with an example.
    Self join is just like any other join, except that two instances of the same table will be joined in the query. Here is an example: Employees table which contains rows for normal employees as well as managers. So, to find out the managers of all the employees, you need a self join. 
  6. What is normalization?
    The Process of organizing relational data into tables is actually referred to as normalization.
  7. What is a Stored Procedure?
    Its nothing but a set of T-SQL statements combined to perform a single task of several tasks. Its basically like a Macro so when you invoke the Stored procedure, you actually run a set of statements.
  8. Can you give an example of Stored Procedure?
    sp_helpdb , sp_who2, sp_renamedb are a set of system defined stored procedures. We can also have user defined stored procedures which can be called in similar way.
  9. What is the basic difference between clustered and a non-clustered index?
    The difference is that, Clustered index is unique for any given table and we can have only one clustered index on a table. The leaf level of a clustered index is the actual data and the data is resorted in case of clustered index. Whereas in case of non-clustered index the leaf level is actually a pointer to the data in rows so we can have as many non-clustered indexes as we can on the db.
  10. When do we use the UPDATE_STATISTICS command?
    This command is basically used when we do a large processing of data. If we do a large amount of deletions any modification or Bulk Copy into the tables, we need to basically update the indexes to take these changes into account. UPDATE_STATISTICS updates the indexes on these tables accordingly.
  11. Which TCP/IP port does SQL Server run on?
    SQL Server runs on port 1433 but we can also change it for better security.
  12. From where can you change the default port?
    From the Network Utility TCP/IP properties –> Port number.both on client and the server.
  13. Can we use Truncate command on a table which is referenced by FOREIGN KEY?
    No. We cannot use Truncate command on a table with Foreign Key because of referential integrity.
  14. What is the use of DBCC commands?
    DBCC stands for database consistency checker. We use these commands to check the consistency of the databases, i.e., maintenance, validation task and status checks.
  15. What command do we use to rename a db?
    sp_renamedb ‘oldname’ , ‘newname’
  16. Sometimes sp_reanmedb may not work you know because if some one is using the db it will not accept this command so what do you think you can do in such cases?
    In such cases we can first bring to db to single user using sp_dboptions and then we can rename that db and then we can rerun the sp_dboptions command to remove the single user mode. 
  17. What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?
    Having Clause is basically used only with the GROUP BY function in a query. WHERE Clause is applied to each row before they are part of the GROUP BY function in a query.
  18. What do you mean by COLLATION?
    Collation is basically the sort order. There are three types of sort order Dictionary case sensitive, Dictonary - case insensitive and Binary.
  19. When do you use SQL Profiler?
    SQL Profiler utility allows us to basically track connections to the SQL Server and also determine activities such as which SQL Scripts are running, failed jobs etc..
  20. What is a Linked Server?
    Linked Servers is a concept in SQL Server by which we can add other SQL Server to a Group and query both the SQL Server dbs using T-SQL Statements.