SQL Server: Database Developer (T-SQL) Interview Questions and Answers (Day-3)

Why is This SQL 3624 Error Caused?

This error is basically caused due to bulk insertion in the MS SQL Server which consequently corrupts the transaction log file of that particular database. Ultimately the database is marked as SUSPECT.

This SQL error 3624 issue can be a consequence of following situations;

  • Case 1: If a table has a non-clustered index.
  • Case 2: A TABLOCK query hint is used.
  • Case 3: Prior bulk insertion, table was not truncated.

How you can rename the SQL Server database?

Using sp_renamedb system stored procedure, we can rename the SQL Server database.

Tell us, most useful FIVE string manipulation functions.

CHARINDEX( findTextData, textData, [startingPosition] ) – Returns the starting position of the specified expression in a character string. The starting position is optional.

REPLACE( textData, findTextData, replaceWithTextData ) – Replaces occurrences of text found in the string with a new value.

SUBSTRING( textData, startPosition, length ) – Returns portion of the string.

STUFF( textData, start , length , insertTextData ) – Deletes a specified length of characters and inserts another set of characters at a specified starting point.

REVERSE( character_expression ) – Returns the reverse of a character expression.

LEN( textData ) – Returns integer value of the length of the string, excluding trailing blanks.

What is the difference between TRUNCATE and DELETE?


  • DELETE is a DML Command.
  • DELETE statement is executed using a row lock, each row in the table is locked for deletion.
  • We can specify filters in where clause
  • It deletes specified data if where condition exists.
  • Delete activates a trigger because the operation are logged individually.
  • Slower than truncate because, it keeps logs.
  • Rollback is possible.


  • TRUNCATE is a DDL command.
  • TRUNCATE TABLE always locks the table and page but not each row.
  • Cannot use Where Condition.
  • It Removes all the data.
  • TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions.
  • Faster in performance wise, because it doesn’t keep any logs.
  • Rollback is not possible.

How you are executing your dynamic SQL?

  • Writing a query with parameters.
  • Using EXEC.
  • Using sp_executesql.

Can you use TOP clause with UPDATE or DELETE statement?

Yes: we can use TOP clause with UPDATE or DELETE statement.

What is a TOP WITH TIES option?

SQL Server: The Awesome TOP clause WITH TIES option

What is the use of the OUTPUT clause?

  • OUTPUT clause is extension to INSERT, DELETE and UPDATE commands.
  • It returns rows information those affected by INSERT,UPDATE or DELETE statement.
  • Its result you can store into variables or other tables for processing.
  • For INSERT and UPDATE operation use OUTPUT INSERTED.*
  • For DELETION operation use OUTPUT DELETED.*
  • is used to select all columns, if you want to select particular column then OUTPUT DELETED.Col1.

What do you know about Comman Table Expression (CTE)?

  • A CTE is essentially a disposable view, and can be used in place of derived tables.
  • CTE is defined by WITH-Keyword, and it is best practice to begin CTE with Semi-column, since CTE is not single-statement it is batch of statement.
  • CTE can be used in SELECT, INSERT, UPDATE or DELETE statement. But SELECT is desired.
  • CTE is single-time visible to sub-sequent query only.
  • CTE can be used to define VIEWS as part of View’s SELECT query.
  • CTE can be defined as Recursive-CTE or Non-Recursive-CTE;
  • Recursive-CTE calls itself whereas Non-Recursive does not. It is best practice to use MAXRECURSION to prevent infinite loops in Recursive-CTE.
  • CTE (Recursive) mainly used for navigating hierarchical data.
  • CTE (Non-Recursive) is substitutable to derived-table (or Inline-view or Sub-Query); Since CTE and Derived Table are non-persistent and have same visibility.
  • CTE is not substitutable to Temp-Table or Table-Variable; Because CTE and TempTable are differ based on: Visibility and Persistency; CTE visibility is limited to only a single SQL statement that would very first to it; whereas Temp-Table or Table-Variable is visible to at-least current scope where they defined and Temp-Table are persistent to Db.
  • CTE can be defined in SUB-ROUTIENS such as user-defined functions, stored procedures triggers or views.
  • You can define cursor on CTE-referenced Select Query.

What are the advantages of CTE?

Light-weight: CTEs are light-weight than Temporary-tables (or Table-Variable). Since, they are non-persistent and do not write any schema information or business-data to disk. The second thing is that CTEs get cleaned up automatically by SQL Server. Therefore, CTE does not create any maintenance over-head and no performance impact.

Recursion: A recursive-CTE is ultimate for navigating hierarchical data;

Readability: CTE improves readability; this is awesome aspect that developers always look for. CTE separates data extraction from your main query. Consider a complex query containing multiple joins, data filter operations, group by etc. Using CTE, you can put all JOIN-operations in one CTE-variable; and all filters in another CTE variable then conveniently access them in your query. This will rapidly increase readability of your query. Hence, CTE is substitutable to Derived Table.

What are the limitations of CTE?

  • CTE-Select cannot include following clauses:
  • INTO
  • ORDER BY (except when a TOP clause is specified)
  • OPTION clause with query hints
  • CTE-cannot include PIVOTed or Un-PIVOTed data.

What is a Recursive Common Table Expression?

A common table expression (CTE) provides the significant advantage of being able to reference itself, thereby creating a recursive CTE. A recursive CTE is one in which an initial CTE is repeatedly executed to return subsets of data until the complete result set is obtained.

A query is referred to as a recursive query when it references a recursive CTE. Returning hierarchical data is a common use of recursive queries.

What do you please tell me something about, RANK() and DENSE_RANK()?


  • Finds rank of for given column. It increments each row by 1 but skips sub-sequent increments for duplicates.
  • If repetitive values exist, the same rank would be assigned to all of them.
  • For each duplicate value, the RANK() function skips the subsequent value so that the next non-duplicate value remains in its rightful position.


  • Finds rank of for given column with subsequent increment even for duplicates.
  • The main difference between RANK() and DENSE_RANK() is that for duplicates(repetitive) values, RANK() skips sub-sequent rank value
  • whereas DENSE_RANK() does not skips and continues with next sub-sequent rank.

We require to find a sequential number for each row within a result set. How is this possible?

Using ROW_NUMBER() OVER(). It returns the sequential number for each row within a result set, starting from 1.


ROW_NUMBER ( ) OVER ([<partition_by_clause>] <order_by_clause>)

Have you ever tried to concat string using GROUP BYclause?

SQL Server: Concatenate Strings using the GROUP BY clause

How to find maximum value from multiple columns?

SQL Server: Find MAX value from Multiple Columns

I want to remove all extra spaces from a string, how is it possible?

SQL Server: Script to Remove Extra Spaces from a String

Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.

I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.

More from dbrnd.com

Leave a Reply

Be the First to Comment!

Notify of