SQL – Interview Questions

Table of Content : here
Goto Category SQL : here

Q. You have the below SQL Code in your application.

Example:

DECLARE @TABLE TABLE(col INT)

BEGIN TRAN
INSERT INTO
@TABLE values(1)
INSERT INTO @TABLE values(2)

BEGIN TRAN A
INSERT INTO @TABLE values(3)
INSERT INTO @TABLE values(4)
SAVE TRAN A

BEGIN TRAN B
INSERT INTO @TABLE values(5)
INSERT INTO @TABLE values(6)
COMMIT TRAN B

ROLLBACK TRAN

SELECT * FROM @TABLE

Ans.
Transaction

Q. What will be the value of @TodayDate in the below script –

Example:
DECLARE @TodayDate DATETIME
SET @TodayDate = ’12/25/2005′

SET @TodayDate = @TodayDate + 5

SELECT @TodayDate

Ans.
GetDate()

Q. Description : Supppose you have a table named Detail that have two columns : id & gender.
_______________________
ID | Gender
_______________________
1 | Male
2 | Male
3 | Female
4 | Male
And you want to change the value from ‘Male’ to ‘Female’ and from ‘Female’ to ‘Male’.
Ans.

UPDATE Detail
SET gender = CASE gender WHEN ‘male’ THEN ‘female’ ELSE ‘male’ END




Q. Suppose you are executing a Transaction in SQL. In which you have 2 lines of code of inserting data into a table. After executing first line, when your control is on second line to execute. So at this moment of time when only 1 line is executed – Where do the values of first line of code is inserted?? Is it get inserted in the original table itself or some other temporary kind of table.. ?? Remember you haven’t commit it.

Ans. In the original table itself.

Example:
Begin tran
Insert into
table1 values(2, ‘Sudha’); // Line 1 – Execute till this line only..
Insert into table2 values(3, ‘Arvind’); // Line 2
Commit // Line3

Data will get stored in table1

Q6. How to Display ‘?’ (or a specific value) if a column contains a NULL value?

Ans.
SELECT id, name, title CASE WHEN title is NULL THEN '?' ELSE title END
as new_title from Customer

NOTE:
Here if title contains NULL then a ‘?’ will appear instead of NULL. You can show any value you want to display.

Q. What is the difference between Delete and Truncate?
Q. Difference between Functions and Stored Procedures.
Q. How many types of Joins in SQL.
Q. Explain Inner Join.