Posts

Showing posts from June, 2018

nth Highest Salary using window function in sql server

Image
A very common question which you will hear in interviews. How to find nth highest salary? Let's do it: The first step, create a table create table #Employee(id int, name varchar(15), salary int); Insert Dummy Data insert into #Employee(id,name,salary) values(1,'A',100) insert into #Employee(id,name,salary) values(2,'B',102) insert into #Employee(id,name,salary) values(3,'C',106) insert into #Employee(id,name,salary) values(4,'D',102) insert into #Employee(id,name,salary) values(5,'E',108) insert into #Employee(id,name,salary) values(6,'F',110) insert into #Employee(id,name,salary) values(7,'G',117) Let's see what we have in the table select * from #Employee; Query to find 3rd Highest Salary using row_number() select a.id,a.name,a.salary from ( select e.id,e.name,e.salary, row_number() over(order by salary) as rn  from #Employee e) as a where rn=3; output: 

Running Total In SQL Server

Running total refers to the sum of values in all cells of a column that precedes the next cell in that particular column. Using Window Function Assuming, the table has 2 columns, col1 is primary key with data type integer and col2 is integer data type values. To calculate running total on col2 write a query as select col1, col2,sum(col2) over(order by col1) as runnintotal from tablename using window function is more performance efficient.

Delegates in C#

Let's start with a very basic definition of Delegates in the real world. Delegates in the real world refer to transferring the work/tasks assigned to self to someone else. A team leader, supervisor, the manager can delegate work to his subordinate. A  delegate in C#  is similar to a function pointer in C or C++. Using a  delegate  allows the programmer to encapsulate a reference to a method inside a  delegate  object. The  delegate  object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. When to use Delegate : Source: https://msdn.microsoft.com/en-us/library/ms173173.aspx Use a delegate in the following circumstances: An eventing design pattern is used. It is desirable to encapsulate a static method. The caller has no need to access other properties, methods, or interfaces on the object implementing the method. An easy composition is des...