Skip to main content

Highest salary query in sql server

DECLARE @dtDepartment TABLE
(
Id int,
Name varchar(100)
)
DECLARE @dtEmployee TABLE
(
Id int,
Name varchar(100),
DeptId int
)
DECLARE @dtEmpSalary TABLE
(
Id int,
EmpId int,
Salary int
)

INSERT INTO @dtDepartment VALUES(1,'.NET')
INSERT INTO @dtDepartment VALUES(2,'Java')

INSERT INTO @dtEmployee VALUES(1,'Brijesh',1)
INSERT INTO @dtEmployee VALUES(2,'Parth',1)
INSERT INTO @dtEmployee VALUES(3,'Ruchir 1',1)
INSERT INTO @dtEmployee VALUES(4,'Jainik 1',1)
INSERT INTO @dtEmployee VALUES(5,'Ruchir',2)
INSERT INTO @dtEmployee VALUES(6,'Jainik',2)
INSERT INTO @dtEmployee VALUES(7,'Jainik',2)
INSERT INTO @dtEmployee VALUES(8,'Jainik',2)

INSERT INTO @dtEmpSalary VALUES(1,1,10000)
INSERT INTO @dtEmpSalary VALUES(2,2,15000)
INSERT INTO @dtEmpSalary VALUES(3,3,12000)
INSERT INTO @dtEmpSalary VALUES(4,4,12000)
INSERT INTO @dtEmpSalary VALUES(5,5,12000)
INSERT INTO @dtEmpSalary VALUES(6,6,15000)
INSERT INTO @dtEmpSalary VALUES(7,7,255555)
INSERT INTO @dtEmpSalary VALUES(8,8,1121313)

SELECT * FROM @dtDepartment
SELECT * FROM @dtEmployee
SELECT * FROM @dtEmpSalary3

;WITH temp as
(
SELECT emp.Id as EmpId,emp.Name as EmpName,
dept.Id as DeptId,dept.Name as DeptName,emps.Salary,
DENSE_RANK() OVER(PARTITION BY dept.Id ORDER BY Salary DESC) AS 'SalaryRank'
FROM @dtDepartment dept
JOIN @dtEmployee emp on emp.DeptId = dept.Id
JOIN @dtEmpSalary emps on emps.EmpId = emp.Id
)
SELECT * FROM temp
WHERE SalaryRank <= 10

SELECT * FROM
@dtDepartment dept
JOIN @dtEmployee emp on emp.DeptId = dept.Id
JOIN @dtEmpSalary emps on emps.EmpId = emp.Id


;WITH Salaries AS
(
    SELECT Id,Salary,
       DENSE_RANK() OVER(ORDER BY Salary DESC) AS 'SalaryRank'
    FROM
        @dtEmpSalary
)
SELECT distinct Id,Salary
FROM Salaries
WHERE SalaryRank = 1

SELECT TOP 1 * FROM
(
SELECT distinct top 1 es.Salary FROM @dtEmpSalary es order by es.Salary desc
) t order by Salary


Comments

Popular posts from this blog

Message Helper for MVC Application

Message Helper for MVC Application public static class MessageExtensions { public enum MessageType { Success = 0, Info = 1, Warning = 2, Error = 3 } public static void ShowMessage( this Controller controller, MessageType messageType, string message, bool showAfterRedirect = false ) { string messageTypeKey = messageType.ToString(); if (showAfterRedirect) { controller.TempData[messageTypeKey] = message; } else { controller.ViewData[messageTypeKey] = message; } } public static HtmlString RenderMessages( this HtmlHelper htmlHelper, bool clearMessage = true ) { string messages = String .Empty; foreach ( string messageType in Enum .GetNames( t

Pass Values Between ASP.NET Web Pages without Session or any State Management

If the source page and target page are both ASP.NET Web pages in the same Web application, and if you transfer execution from the source page to the target page on the server by using the transfer method, the target page can access public properties in the source page. Page One <asp:TextBox ID="textCity" runat="server" Text="Brjesh"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />  public String CurrentCity         {             get             {                 return textCity.Text;             }         }         public List<String> Current         {             get             {                 return _Current;             }         }         private List<String> _Current = null;         protected void Page_Load(object sender, EventArgs e)         {             _Current = new List<String>();             _Current.Add("1

CRUD in EF

 public int Create(Users entity)         {             using (var context = new K305DataEntities())             {                 var newEntity = Mapper.Map<User>(entity);                             context.Users.Add(newEntity);                 context.SaveChanges();                 return newEntity.Id;             }         }         public int Update(Users entity)         {             using (var context = new K305DataEntities())             {                 var dbEntity = context.Users.FirstOrDefault(dc => dc.Id == entity.Id);                 if (dbEntity == null) throw new ApplicationException("Entity not found.");                 dbEntity.Name = entity.Name;                 context.SaveChanges();                 return dbEntity.Id;             }                 }         public List<Users> GetAll()         {             using (var context = new K305DataEntities())             {                 return context.Users.Select(