T-SQL Interview Questions
T-SQL Interview Questions
Are
there any implications with the specific commands?
● The DELETE command.
● The TRUNCATE command.
● In terms of implications, a few different issues could occur:
○ With the DELETE or TRUNCATE command, you will lose all of your
data in a table.
○ A single DELETE command could fill up the transaction log since it is a
single transaction.
○ A TRUNCATE command could cause issues for Log Shipping since it
is a minimally logged operation.
Question: What are the three ways that Dynamic SQL can be issued?
● Writing a query with parameters.
● Using EXEC.
● Using sp_executesql
Question: True or False - SQL Server can format the date in over 10 different
patterns.
● True - With the CONVERT command there are over 15 different date formats
What is PatIndex?
Ans: Returns the starting position of the first occurrence of a pattern in a specified
expression, or zeros if the pattern is not found
Syntax - PATINDEX ( '%pattern%' , expression )
Consider you have a table with columns ID(primary key), Country and State.
Now if you have some rows with combination of country and state repeating,
ie, two rows with combination India, Kerala. Write a query for deleting
duplicate records?
Ans: With T1 as
(Select *,Row_Number() over (partition by Country, State order by ID)
as 'RowNo' From TableName)
Delete from T1 where RowNo > 1;
Ans. Two ways of creating temporary table with non clusterindex applied on it. Also
example shows how to apply "nolock". nolock is normally applied while querying on
production servers. This would make the records being queried sharable on the
table. ie, will not prevent other queries from querying the same record parallely on
same table. The risk will be nolock might return junk data some times because the
select query might be querying the table while some other insertion or updation
commands are performed on the table.
1.
CREATE TABLE #tmpTable
(
OfficeName varchar(50)
, officeid int
, CustID int
, AgentID int
, mlsid varchar(4)
, RequestMoreDetails int null
, Emails int null
)
CREATE NONCLUSTERED INDEX #IX_DW_Listings ON #DW_AllListings(AgentID)
2.
select
OfficeName
, officeid
, o.CustID
, AgentID -
, o.mlsid
, PrintBrochure_Views = null
, RequestMoreDetails = null
, Emails = null
into #ForOffices from #Offices o
LEFT JOIN dbo.planparts WITH (NOLOCK)
ON bppa.officeid = o.RID
CREATE NONCLUSTERED INDEX #IX_DW_Listings ON #ForOffices(AgentID)
1) Procedure can return zero or n values whereas function can return one value
which is mandatory.
2) Procedures can have input, output parameters for it whereas functions can have
only input parameters.
3) Procedure allows select as well as DML statement in it whereas function allows
only select statement in it.
4) Functions can be called from procedure whereas procedures cannot be called
from function.
5) Exception can be handled by try-catch block in a procedure whereas try-catch
block cannot be used in a function.
6) We can go for transaction management in procedure whereas we can't go in
function.
7) Procedures cannot be utilized in a select statement whereas function can be
embedded in a select statement.
From above Data: What will be the result of the query below?
SELECT * FROM runners WHERE id NOT IN (SELECT winner_id FROM races)
WHERE EXISTS (
WHERE id=envelope.id
);
FROM Invoices
3 Anne Jenkins 2
5 Pat Richards 1
6 Alice Barnes 2
Here is a query written to return the list of customers not referred by Jane Smith:
What will be the result of the query? Why? What would be a better way to write it?
SELECT * FROM users;
user_id username
1 John Doe
2 Jane Don
3 Alice Jones
4 Lisa Romero
1 1 1 "2015-08-02"
2 2 1 "2015-08-03"
3 3 2 "2015-08-02"
4 4 2 "2015-08-04"
5 2 2 "2015-08-03"
6 1 1 "2015-08-02"
7 3 2 "2015-08-04"
8 4 3 "2015-08-03"
9 1 4 "2015-08-03"
10 3 1 "2015-08-02"
11 4 2 "2015-08-04"
12 3 2 "2015-08-02"
13 1 1 "2015-08-02"
14 4 3 "2015-08-03"
SELECT
u.user_id,
username,
training_id,
training_date,
count( user_training_id ) AS count
FROM users u JOIN training_details t ON t.user_id = u.user_id
GROUP BY user_id,
training_id,
training_date
HAVING count( user_training_id ) > 1
ORDER BY training_date DESC;
user_id username training_id training_date count
4 Lisa Romero 2 August, 04 2015 00:00:00 2
4 Lisa Romero 3 August, 03 2015 00:00:00 2
1 John Doe 1 August, 02 2015 00:00:00 3
3 Alice Jones 2 August, 02 2015 00:00:00 2
What is an execution plan? When would you use it? How would you view the
execution plan?
An execution plan is basically a road map that graphically or textually shows the data
retrieval methods chosen by the SQL server’s query optimizer for a stored procedure
or ad hoc query. Execution plans are very useful for helping a developer understand
and analyze the performance characteristics of a query or stored procedure, since
the plan is used to execute the query or stored procedure.
In many SQL systems, a textual execution plan can be obtained using a keyword
such as EXPLAIN, and visual representations can often be obtained as well. In
Microsoft SQL Server, the Query Analyzer has an option called “Show Execution
Plan” (located on the Query drop down menu). If this option is turned on, it will
display query execution plans in a separate window when a query is run.