Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 June SQL Server: Database Developer (T-SQL) Interview Questions and Answers (Day-3)

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

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

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:

  • 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:

  • 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:
  • DISTINCT
  • INTO
  • ORDER BY (except when a TOP clause is specified)
  • GROUP BY
  • OPTION clause with query hints
  • FOR BROWSE
  • 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()?

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.

DENSE_RANK():

  • 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.

Syntax:

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

Jun 14, 2016Anvesh Patel
SQL Server: Database Developer (T-SQL) Interview Questions and Answers (Day-2)SQL Server: Database Developer (T-SQL) Interview Questions and Answers (Day-4)
Comments: 1
  1. Scott
    August 8, 2017 at 2:11 pm

    This is wrong. You can ROLLBACK a TRUNCATE TABLE

Anvesh Patel
Anvesh Patel

Database Engineer

June 14, 2016 SQL Server Interviewanswers, Anvesh Patel, Common Table Expression, database, Database Developer, database research and development, dbrnd, delete, dense_rank(), interview, OUTPUT clause, questions, RANK, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, truncate, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....