SQL Server - Common Interview Questions and Answers

  1. What is the difference between a Local and a Global temporary table?
    1. A local temporary table exists only for the duration of a connection or, if defined inside a compound statement, for the duration of the compound statement.
    2. A global temporary table remains in the database permanently, but the rows exist only within a given connection. When connection is closed, the data in the global temporary table disappears. However, the table definition remains with the database for access when database is opened next time.
  2. What is the STUFF function and how does it differ from the REPLACE function?
    STUFF function is used to overwrite existing characters. Using this syntax, STUFF (string_expression, start, length, replacement_characters), string_expression is the string that will have characters substituted, start is the starting position, length is the number of characters in the string that are substituted, and replacement_characters are the new characters interjected into the string. REPLACE function to replace existing characters of all occurrences. Using the syntax REPLACE (string_expression, search_string, replacement_string), where every incidence of search_string found in the string_expression will be replaced with replacement_string.
  3. What is PRIMARY KEY?
    A PRIMARY KEY constraint is a unique identifier for a row within a database table. Every table should have a primary key constraint to uniquely identify each row and only one primary key constraint can be created for each table. The primary key constraints are used to enforce entity integrity.
  4. What is UNIQUE KEY constraint?
    A UNIQUE constraint enforces the uniqueness of the values in a set of columns, so no duplicate values are entered. The unique key constraints are used to enforce entity integrity as the primary key constraints.
  5. What is FOREIGN KEY?
    A FOREIGN KEY constraint prevents any actions that would destroy links between tables with the corresponding data values. A foreign key in one table points to a primary key in another table. Foreign keys prevent actions that would leave rows with foreign key values when there are no primary keys with that value. The foreign key constraints are used to enforce referential integrity.
  6. What is CHECK Constraint?
    A CHECK constraint is used to limit the values that can be placed in a column. The check constraints are used to enforce domain integrity.