• Home
  • All Articles
  • SQL Interview Q & A
  • Blog Stats
  • Contact
    • Resume
    • Performance
    • Community Rules
    • Copyright
  • Tools
    • Pluralsight
    • NuoDB
    • Idera
    • Embarcadero
    • Red Gate
    • Devart SQL Server Tools
    • Melissadata
    • Koenig Solutions
    • Manage Engine
    • SQL Backup and FTP
  • SQL Books
    • SQL Interview Q & A
    • SQL Wait Stats
    • SQL 2012 J2P Vol 1
    • SQL 2012 J2P Vol 2
    • SQL 2012 J2P Vol 3
    • SQL 2012 J2P Vol 4
    • SQL 2012 J2P Vol 5
    • SQL Queries 2012 Joes 2 Pros Combo Kit
    • Learn SQL Server 2008
      • SQL Joes 2 Pros Vol 1
      • SQL Joes 2 Pros Vol 2
      • SQL Joes 2 Pros Vol 3
      • SQL Joes 2 Pros Vol 4
      • SQL Joes 2 Pros Vol 5
      • SQL Joes 2 Pros – Combo 5 Books
  • >>Search<<

SQL Server Journey with SQL Authority

Personal Notes of Pinal Dave

Feeds:
Posts
Comments
« SQL Server Interview Questions and Answers – Part 2
SQL Server Interview Questions and Answers – Part 4 »

SQL Server Interview Questions and Answers – Part 3

April 17, 2007 by pinaldave

SQL Server Interview Questions and Answers
Print Book Available (207 Pages) | Sample Chapters

UPDATE : Interview Questions and Answers are now updated with SQL Server 2008 Questions and its answers. New Location : SQL Server 2008 Interview Questions and Answers.

What is a NOLOCK?
Using the NOLOCK query optimiser hint is generally considered good practice in order to improve concurrency on a busy system. When the NOLOCK hint is included in a SELECT statement, no locks are taken when data is read. The result is a Dirty Read, which means that another process could be updating the data at the exact time you are reading it. There are no guarantees that your query will retrieve the most recent data. The advantage to performance is that your reading of data will not block updates from taking place, and updates will not block your reading of data. SELECT statements take Shared (Read) locks. This means that multiple SELECT statements are allowed simultaneous access, but other processes are blocked from modifying the data. The updates will queue until all the reads have completed, and reads requested after the update will wait for the updates to complete. The result to your system is delay(blocking).

What is difference between DELETE & TRUNCATE commands?
Delete command removes the rows from a table based on the condition that we provide with a WHERE clause. Truncate will actually remove all the rows from a table and there will be no data in the table after we run the truncate command.

TRUNCATE
TRUNCATE is faster and uses fewer system and transaction log resources than DELETE.
TRUNCATE removes the data by deallocating the data pages used to store the table’s data, and only the page deallocations are recorded in the transaction log.
TRUNCATE removes all rows from a table, but the table structure and its columns, constraints, indexes and so on remain. The counter used by an identity for new rows is reset to the seed for the column.
You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint.
Because TRUNCATE TABLE is not logged, it cannot activate a trigger.
TRUNCATE can not be Rolled back using logs.
TRUNCATE is DDL Command.
TRUNCATE Resets identity of the table.

DELETE
DELETE removes rows one at a time and records an entry in the transaction log for each deleted row.
If you want to retain the identity counter, use DELETE instead. If you want to remove table definition and its data, use the DROP TABLE statement.
DELETE Can be used with or without a WHERE clause
DELETE Activates Triggers.
DELETE Can be Rolled back using logs.
DELETE is DML Command.
DELETE does not reset identity of the table.

Difference between Function and Stored Procedure?
UDF can be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section where as Stored procedures cannot be.
UDFs that return tables can be treated as another rowset. This can be used in JOINs with other tables.
Inline UDF’s can be though of as views that take parameters and can be used in JOINs and other Rowset operations.

When is the use of UPDATE_STATISTICS command?
This command is basically used when a large processing of data has occurred. If a large amount of deletions any modification or Bulk Copy into the tables has occurred, it has to update the indexes to take these changes into account. UPDATE_STATISTICS updates the indexes on these tables accordingly.

What types of Joins are possible with Sql Server?
Joins are used in queries to explain how different tables are related. Joins also let you select data from a table depending upon data from another table.
Types of joins: INNER JOINs, OUTER JOINs, CROSS JOINs. OUTER JOINs are further classified as LEFT OUTER JOINS, RIGHT OUTER JOINS and FULL OUTER JOINS.

What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?
Specifies a search condition for a group or an aggregate. HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like 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. HAVING criteria is applied after the the grouping of rows has occurred.

What is sub-query? Explain properties of sub-query.
Sub-queries are often referred to as sub-selects, as they allow a SELECT statement to be executed arbitrarily within the body of another SQL statement. A sub-query is executed by enclosing it in a set of parentheses. Sub-queries are generally used to return a single row as an atomic value, though they may be used to compare values against multiple rows with the IN keyword.

A subquery is a SELECT statement that is nested within another T-SQL statement. A subquery SELECT statement if executed independently of the T-SQL statement, in which it is nested, will return a result set. Meaning a subquery SELECT statement can standalone and is not depended on the statement in which it is nested. A subquery SELECT statement can return any number of values, and can be found in, the column list of a SELECT statement, a FROM, GROUP BY, HAVING, and/or ORDER BY clauses of a T-SQL statement. A Subquery can also be used as a parameter to a function call. Basically a subquery can be used anywhere an expression can be used.

Properties of Sub-Query
A subquery must be enclosed in the parenthesis.
A subquery must be put in the right hand of the comparison operator, and
A subquery cannot contain a ORDER-BY clause.
A query can contain more than one sub-queries.

What are types of sub-queries?
Single-row subquery, where the subquery returns only one row.
Multiple-row subquery, where the subquery returns multiple rows,.and
Multiple column subquery, where the subquery returns multiple columns.

Complete Series of SQL Server Interview Questions and Answers
SQL Server Interview Questions and Answers -Introduction
SQL Server Interview Questions and Answers – Part 1
SQL Server Interview Questions and Answers – Part 2
SQL Server Interview Questions and Answers – Part 3
SQL Server Interview Questions and Answers – Part 4
SQL Server Interview Questions and Answers – Part 5
SQL Server Interview Questions and Answers – Part 6
SQL Server Interview Questions and Answers Complete List Download

Reference : Pinal Dave (http://blog.SQLAuthority.com)

About these ads

Share:

  • More

Like this:

Like Loading...

Posted in Database, Pinal Dave, Software Development, SQL, SQL Authority, SQL Constraint and Keys, SQL Cursor, SQL Download, SQL Function, SQL Index, SQL Interview Questions and Answers, SQL Joins, SQL Performance, SQL Query, SQL Scripts, SQL Security, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, SQL Trigger, T SQL, Technology | Tagged SQL Sub Query | 44 Comments

44 Responses

  1. on April 18, 2007 at 7:07 am Sham

    Why and How to use Create Statistic command?


  2. on April 18, 2007 at 8:56 am pinaldave

    Sham,
    CREATE STATISTIC Creates a histogram and associated density groups (collections) over the supplied column or set of columns of a table or indexed view. String summary statistics are also created on statistics built on char, varchar, varchar(max), nchar, nvarchar, nvarchar(max), text, and ntext columns. The query optimizer uses this statistical information to choose the most efficient plan for retrieving or updating data.
    Thank you,
    Pinal Dave
    (http://www.SQLAuthority.com)


  3. on April 30, 2007 at 4:28 am Sumeet Haldankar

    Hi
    you have mentioned that trucnate table cannot be rolled back.
    Inside a transaction on truncating a table the dellocation of the data pages is logged and hence truncation can be rolled back .


  4. on May 3, 2007 at 11:12 pm Faisal

    hi,
    what are the commands which are not allowed in SQL Server Triggers.

    Regards


  5. on May 19, 2007 at 2:35 pm ramesh mishra

    please solve my all queries which is listed below

    To find the 5th row of a table.
    To find 2nd highest salary
    To find 3 highest salary
    To delete duplicate records of a table.
    Group functions
    Types of join, describe self join and equi join
    special operators
    Delete records where ename is duplicated
    To find 10th lowest salary
    union operator


  6. on June 4, 2007 at 11:17 pm Ramesh Hothur

    please solve my all queries which is listed below

    To find the 5th row of a table.
    To find 2nd highest salary
    To find 3 highest salary
    To delete duplicate records of a table.
    Group functions
    Types of join, describe self join and equi join
    special operators
    Delete records where ename is duplicated
    To find 10th lowest salary
    union operator


  7. on June 5, 2007 at 6:19 am pinaldave

    Ramesh Hothur ,

    Please visit Search SQLAuthority and search for your questions, you will find answers to most of your questions.

    Kind Regards,
    Pinal Dave (SQLAuthority.com)


  8. on June 12, 2007 at 7:04 am Marco

    I don’t agree your statement that “TRUNCATE cannot be rolled back”.

    This example demonstrates that a TRUNCATE statement can be rolled back in an explicit transaction.

    create table test (id int identity(1,1), f int)

    declare @i int
    set @i = 100
    while @i>0
    BEGIN
    set @i = @i-1
    insert into test(f)values(@i)

    end
    – the table contains 100 rows
    select * from test

    begin tran

    truncate table test
    select * from test
    – now the table is empty

    rollback

    – the table has again 100 rows
    select * from test

    It should be stressed that TRUNCATE is a DDL statement! From BOL: “TRUNCATE TABLE permissions default to the table owner, members of the sysadmin fixed server role, and the db_owner and db_ddladmin fixed database roles, and are not transferable.”


  9. on June 12, 2007 at 7:14 am pinaldave

    TRUNCATE is DDL Statement is already mentioned in the article.


  10. on June 19, 2007 at 3:21 am srikanth

    How to eliminate duplicate rows in a table


  11. on June 22, 2007 at 2:06 am Seetharaman.K

    Can u tell me the difference between select and choose statement in SQL?


  12. on July 4, 2007 at 2:59 am sta

    Hi,

    I need to delete a row with primary key = 17
    but wen I reenter informatio into the table it displays 16,18 the deleleted.

    How to delete but retain the identity


  13. on July 17, 2007 at 1:20 am Abid SPM

    Sir
    Could you clear my doubt regarding the possibility of TRUNCATE being rolled back?Plz get me a solid answer.I could find “TRUNCATE cannot be rolled back” statement in some resources.At the same time,some other resources say “Ofcourse TRUNCATE can be rolled back”.Which is the correct one?Iam Confused..Plz help me out


  14. on August 2, 2007 at 10:46 pm pavanich

    Hi srikanth,
    we can delete the duplicate data by use of distinct keyword


  15. on August 2, 2007 at 10:48 pm pavanich

    Hi pinaldev ,
    can you give me a brief information of what is a trigger,its use,
    how it works and in what situation we use.

    thanks,
    Pavanich


  16. on August 2, 2007 at 10:48 pm pinaldave

    hi pavanich,

    distinct will hide the duplicate data it will not delete it.

    Kind Regards,
    Pinal Dave(SQLAuthority.com)


  17. on August 9, 2007 at 3:04 am deepthisantosh

    how can we get the maximum salary from a table without using max keyword in oracle


  18. on August 17, 2007 at 2:59 am pavanich

    Hi pinaldev ,
    can you give me a brief information of what is a trigger,its use,the types,and how it works and in what situation we use.
    please explain me with an example

    thanks,
    Pavanich


  19. on August 27, 2007 at 3:13 pm balaji.b

    How to avoid Cursors?


  20. on August 28, 2007 at 8:56 am pinaldave

    Hi balaji,

    Cursors are not good and alternative is mentioned here with examples.
    http://blog.sqlauthority.com/2007/08/15/sql-server-insert-data-from-one-table-to-another-table-insert-into-select-select-into-table/


  21. on September 14, 2007 at 5:43 pm shiva

    Colud you please explain .. How to interprete the Query Excecution plan to improve the performance


  22. on September 17, 2007 at 12:29 am abhay

    Dear Pinal,

    just to add some differences between a sp and a table :

    1. when you add any exec statement which executes sps or any dynamic query in a fn , you will get this msg from the execution engine :

    Only functions and extended stored procedures can be executed from within a function.

    2. you cannot access temporary tables from within a function.

    Regards,
    Abhay


  23. on October 20, 2007 at 6:23 am Robert Vaz

    Hi,

    Suppose I have a table of all the salaries of employees and want to to find out the 5th highest record in the table then what will the query be like?

    Thanks

    Robert Vaz


  24. on November 19, 2007 at 8:21 pm sandeep

    select * from t1 where n= (select count(*) from t2 where t1.[ColumnName] <=t2.[ColumnName])

    this is generailzed Query for Finding Nth highest Records Just replace n for the position.


  25. on December 13, 2007 at 4:58 pm lavanya

    Hi
    pavanich i saw your sql server interview questions its really good.I have one problem can you please help me.

    The details are

    i have 4 tables named as A,B,C,D

    the table A has two columns EMPID,REFNo.
    In The remaining all tables EMPID Column is there.this is common column in all the tables

    BUt I will authenticate the user by using REFNo.
    Depending on that REFNO i have to get the EMPID From A table . comparing EMPID in the remaining all 3 tables and
    want to get the data from these 3 tables .

    please help me


  26. on December 31, 2007 at 6:13 pm NINGARAJ

    Error message in Oracle 8i in windows xp

    ora-01034

    please help me


  27. on January 8, 2008 at 10:51 pm Brijesh

    hiii…,
    ningaraj…
    try this…it’s perfect for u…

    ORA-01034:

    Cause:
    Oracle was not started up. Possible causes are

    *The SGA requires more space than was allocated for it.

    **The operating-system variable pointing to the instance is improperly defined.

    Action:

    Refer to accompanying messages for possible causes and correct the problem mentioned in the other messages. If Oracle has been initialized, then on some operating systems, verify that Oracle was linked correctly. See the platform specific Oracle documentation.


  28. on February 21, 2008 at 2:02 am santra

    Hi,

    I would like to know how can we connect Sql server to visual basic?……
    How many types of connections are there to connect visual basic to sql server?

    please reply

    santra


  29. on March 10, 2008 at 10:49 pm Sandeep

    Hi Sir,

    Nice website, but the question about Difference between Truncate and Delete Table you have mentioned that truncate table can not be rollback, it is not true, i can be rolled back i have done it myself


  30. on March 10, 2008 at 11:05 pm Sandeep

    Hi,

    You have mentioned in question “Difference between having and where clause’ that

    when Group by is not used it behaves like a where claluse this statement is wrong because without group by clause you can not use having clause.


  31. on April 8, 2008 at 8:08 am Meena

    I wan to know what is the differance between Sql Sever & Oracle?


  32. on April 23, 2008 at 5:22 pm Sachin

    Question is difference between Truncate and Delete. One of the Difference is Truncate can not roll back. but it can roll back. how this DDL statement can roll back.


  33. on April 27, 2008 at 5:31 am Venkateshwar Thota

    An addition:

    UDF – User Defined Function
    UDF – Subroutines made up of one or more Transact-SQL statements that can be used to encapsulate code for reuse


  34. on May 16, 2008 at 1:46 pm Buna

    please solve my all queries which is listed below

    To find the 5th row of a table.
    To find 2nd highest salary
    To find 3 highest salary
    To delete duplicate records of a table.
    Group functions
    Types of join, describe self join and equi join
    special operators
    Delete records where ename is duplicated
    To find 10th lowest salary
    union operator


  35. on June 4, 2008 at 6:46 am Xyz

    Consider you have table called product and column prod_price with n rows.

    if you have the following query you can get the 2nd,3rd,4th highest values just changing the top number in the sub query.

    select top 1 prod_price from product where prod_price in(select top 2 prod_price from product
    order by prod_price desc)

    Above qery resut the 2nd highes value . if you change the top 2 to top 3 in the sub query it will give the 3rd highest ..


  36. on June 7, 2008 at 4:36 pm Amit

    select name,roll,marks from student;
    union
    select name,roll,marks from marks;

    means union is used for select data from two table where column name should be same, column number should be same and column data type should also be same and column format also should be same than union works other wise it will not work you can try this query which displayed above

    Thanks
    Amit Sinha


  37. on June 18, 2008 at 2:43 pm anju

    sir this is very good collection of sel server interview question

    its clear all my qustion
    thank for this help


  38. on June 19, 2008 at 3:39 am Madhavi

    this is awesome!!! thank you. i have one question. what do we do when we have a deadlock when executing a query? can somebody pls answer my question?


  39. on July 15, 2008 at 10:17 am neha rane

    Awesome posts.Really helping


  40. on July 20, 2008 at 8:44 pm krishna

    i want sql server oracle diffence


  41. on July 21, 2008 at 4:52 am Imran Mohammed

    @Krishna,

    This should help

    Google on “sql server 2005 compare oracle”

    The first link ( from microsoft ….. )

    Thanks,
    Imran.


  42. on September 20, 2008 at 11:35 am pinaldave

    UPDATE : Interview Questions and Answers are now updated with SQL Server 2008 Questions and its answers. New Location : SQL Server 2008 Interview Questions and Answers.

    Please continue with your questions and answers at new location.


  43. on February 24, 2009 at 6:22 pm SQL SERVER - Database Interview Questions and Answers Complete List Journey to SQL Authority with Pinal Dave

    [...] Server Interview Questions and Answers – Part 1 SQL Server Interview Questions and Answers – Part 2 SQL Server Interview Questions and Answers – Part 3 SQL Server Interview Questions and Answers – Part 4 SQL Server Interview Questions and Answers – [...]


  44. on April 20, 2013 at 7:01 am SQL SERVER – Weekly Series – Memory Lane – #025 | SQL Server Journey with SQL Authority

    [...] Interview Questions and Answers – Part 1 SQL Server Interview Questions and Answers – Part 2 SQL Server Interview Questions and Answers – Part 3 SQL Server Interview Questions and Answers – Part 4 SQL Server Interview Questions and Answers [...]



Comments are closed.

  • Community Initiatives

    pluralsight
    SQL Complete
    Idera
    RedGate
    Embarcadero
    koenig-solutions
    Melissa Data
    ManageEngine
    SQL Backup and FTP
  • About Pinal Dave

    Pinal Dave is a Pluralsight Developer Evangelist. He has authored 9 SQL Server database books and have written over 2500 articles on the database technology on his blog at a http://blog.sqlauthority.com. Along with 9+ years of hands on experience he holds a Masters of Science degree and a number of certifications, including MCTS, MCDBA and MCAD (.NET). His past work experiences include Technology Evangelist at Microsoft and Sr. Consultant at SolidQ. Prior to joining Microsoft he was awarded the Microsoft MVP award for three continuous years for his contribution in the community. Here is the list of the Pinal Dave's books.
    Twitter - Pinal Dave facebook Feed LinkedIn - Pinal Dave Youtube

    Follow @pinaldave
    Send +Pinal Dave an email at pinal@sqlauthority.com

    • 63,516,185 (63 Million+)
  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 44,159 other followers

  • SQL in Sixty Seconds

  • SQL Books

    Amazon | 1 | 2 | 3 | 4 | 5
    Flipkart | 1 | 2 | 3 | 4 | 5

    SQL Interview Q and A
    Amazon | Kindle Flipkart
    SQL Wait Stats
    Amazon | Kindle Flipkart
  • Funny Index Video

  • SQLAuthority Links

    My Homepage
    Windows Live Blog
           --------------------
    Top Downloads
       PDF Downloads
       Script Downloads

    Script Bank
       Favorite Scripts
       All Scripts - 1
       All Scripts - 2
       All Scripts - 3

    Top Articles
       Best Articles
       Favorite Articles - 1
       Favorite Articles - 2
           --------------------
    > SQL Interview Q & A <
    SQL Coding Standards
    SQL FAQ Download
           --------------------
    Jobs @ SQLAuthority
    Bookmark and Share
    AddThis Feed Button
  • About Nupur Dave

    Nupur Dave loves technology simply because it makes life more convenient. She is devoted to technology because it touches our heart makes our daily lives easier. Among the many technological programs she uses and embraces Windows Live most because she can do lots of things with ease – from photo management to movies; business emails to personal social media connections.

  • Top 3 Commenters

      2857 - Madhivanan
      474 - Imran Mohammed
      301 - Ramdas Jaya
  • Page copy protected against web site content infringement by Copyscape

Blog at WordPress.com.

Theme: Customized MistyLook by WPThemes.


loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.
%d bloggers like this: