Microsoft SQL Server

Logical Interview Questions for SQL Server

SELECT statements covering all the options.

 SELECT select_list  
 [INTO new_table_] 
 FROM table_source 
 [WHERE search_condition] 
 [GROUP BY group_by__expression] 
 [HAVING search_condition] 
 [ORDER BY order__expression [ASC | DESC] ] 

Identity Insert Syntax for SQL Server

 SET IDENTITY_INSERT IdentityTable ON  
 INSERT IdentityTable(TheIdentity, TheValue) VALUES (3, 'First Row') 
 SET IDENTITY_INSERT IdentityTable OFF 


Deleting duplicate records from table using CTE (Common Table Expression)

 WITH [CTE DUPLICATE] AS   
(
SELECT
RN = ROW_NUMBER() OVER (PARTITION BY CompanyTitle ORDER BY LastContactDate DESC),
Id, CompanyTitle, ContactName, LastContactDate
FROM Suppliers
)
DELETE FROM [CTE DUPLICATE] WHERE RN > 1

Microsoft SQL Server

TRIGGER in SQL Server

A trigger is a special kind of a store procedure that executes in response to certain action on the table like insertion, deletion or updating of data. It is a database object which is bound to a table and is executed automatically. You can’t explicitly invoke triggers. The only way to do this is by performing the required action no the table that they are assigned to.

Types Of Triggers
There are three action query types that you use in SQL which are INSERT, UPDATE and DELETE. So, there are three types of triggers and hybrids that come from mixing and matching the events and timings that fire them.

After Triggers
These triggers run after an insert, update or delete on a table. They are not supported for views.
AFTER TRIGGERS can be classified further into three types as:
(a) AFTER INSERT Trigger.  : This trigger is fired after an INSERT on the table.
(b) AFTER UPDATE Trigger. : This trigger is fired after an update on the table.
(c) AFTER DELETE Trigger. : This trigger is fired after a delete on the table.

Instead Of Triggers
These can be used as an interceptor for anything that anyone tried to do on our table or view. If you define an Instead Of trigger on a table for the Delete operation, they try to delete rows, and they will not actually get deleted (unless you issue another delete instruction from within the trigger)
INSTEAD OF TRIGGERS can be classified further into three types as:-
(a) INSTEAD OF INSERT Trigger.
(b) INSTEAD OF UPDATE Trigger.
(c) INSTEAD OF DELETE Trigger.

Microsoft SQL Server

CURSOR in SQL Server

Cursor is a database object used by applications to manipulate data in a set on a row-by- row basis, instead of the typical SQL commands that operate on all the rows in the set at one time.

In order to work with a cursor we need to perform some steps in the following order:
Declare cursor
Open cursor
Fetch row from the cursor
Process fetched row
Close cursor
Deallocate cursor

 DECLARE @UserId int -- column name    
DECLARE db_cursor CURSOR FOR
SELECT UserId FROM dbo.Users
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @UserId
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE dbo.Users SET name='Himen' where UserId=@UserId
FETCH NEXT FROM db_cursor INTO @UserId
END
CLOSE db_cursor
DEALLOCATE db_cursor
Microsoft SQL Server

JOINS in SQL Server

Cross Join A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table. The common example is when company wants to combine each product with a pricing table to analyze each product at each price.

Inner Join A join that displays only the rows that have a match in both joined tables is known as inner Join. This is the default type of join in the Query and View Designer.

Outer Join A join that includes rows even if they do not have related rows in the joined table is an Outer Join. You can create three different outer join to specify the unmatched rows to be included:

Left Outer Join
: In Left Outer Join all rows in the first-named table i.e. “left” table, which appears leftmost in the JOIN clause are included. Unmatched rows in the right table do not appear.

Right Outer Join
: In Right Outer Join all rows in the second-named table i.e. “right” table, which appears rightmost in the JOIN clause are included. Unmatched rows in the left table are not included.

Full Outer Join
: In Full Outer Join all rows in all joined tables are included, whether they are matched or not.

Self Join
This is a particular case when one table joins to itself, with one or two aliases to avoid confusion. A self join can be of any type, as long as the joined tables are the same. A self join is rather unique in that it involves a relationship with only one table.

The common example is when company has a hierarchal reporting structure whereby one member of staff reports to another. Self Join can be Outer Join or Inner Join.