TRICKY SQL INTERVIEW QUERIES QUESTIONS ANSWERS FOR EXPERIENCED SQL DEVELOPERS

Hey guys, I am writing this post after many days, after many comment by SQL Guys, Every one want to see new post related to SQL Interview questions, I have already written many Sql interview queries, In the bunch which are related, So the visitor looking for new queries so in this post, at a time I cant post many but, keep posting new query which I will face.
So for new tricky queries I need you help, If you have any tricky Sql queries then please post as a comment. It will be helpful for other.

So lets discuss new a query, This is not new but little bit tricky.

Query : There are two tables T1 and T2 with two columns, I want to swap the name of both tables, based on ID column. How you will do that.
Tables:


Ans:
--First copy T1 data in to temp table
SELECT * INTO #T1 FROM T1
--Then Update T1 with T2
UPDATE T1 SET T1.NAME = T2.NAME FROM T1 INNER JOIN T2 ON T1.ID = T2.ID

--Then Update T2 With Temp talbe(#T1)
UPDATE T2 SET T2.NAME = #T1.NAME  FROM T2 INNER JOIN #T1 ON T2.ID = #T1.ID

DROP TABLE #T1 --Drop temp table Table

Output after run the above queries.
If you have another solution then please comment.. I will post here..

Comments

  1. Would you please email me the PDF for Set 15, theoretical answers PDF. My email is fatima.s417@gmail.com. Your site is very informative.

    ReplyDelete
  2. Great list of questions! Here's a few more I think you should consider adding to your list too:
    http://cc.davelozinski.com/general/tricky-csharp-and-sql-interview-questions

    _

    ReplyDelete
  3. your article is very nice i like this article and i would like to get from more information about this article please keep sharing this. i am a writer working with phd thesis writing service. thesis writing help to all students. this is very effective one.

    ReplyDelete
  4. hai
    i have t1 and t2 table
    t1 have
    a
    b
    c
    d
    =====
    t2 have
    c
    d
    e
    f

    i want to join two tables .....
    so i want innerjoin, left,right join output....

    ReplyDelete
  5. No need for temp table you can simply do this.

    update t1
    set Name = (select t22.Name from t2 t22 where t1.ID=t22.ID)

    ReplyDelete
  6. They are tricky indeed. It was nice to read this article about them. Nice job.

    ReplyDelete

Archive

Contact Form

Send