Skip to main content

Posts

Showing posts with the label 2nd-3rd-4th-nth highest salary

SQL temp table vs table variable

There are some differences between “ Temporary Tables ” (#tempTable) and “ Table Variables ” (@tempTable). Point 1: A Temp table (#tmp) can do all the DDL operations and it allows creating the indexes, altering and dropping. A Table variable (@tmp) is not allowed doing the DDL operations but can create the clustered index only. Point 2: A Temp table (#tmp) is easy to create and back up your data. A Variable table (@tmp) is easy to create but involves the extra effort for create the normal tables and then back up your data. Point 3: A Temp table (#tmp) result can be used by multiple users. A Variable table (@tmp) result can be used by the current user only. Point 4: A Temp table (#tmp) will be stored in the tempdb and create network traffic. If we have large amount of data in the temp table and it will create performance issue. A Table variable (@tmp) will be store in the physical memory for some of the data, and then later when the size increases it w...

How To Find The Highest Salary In SQL Server using MAX, DENSE_RANK, and SUB QUERY?

Method 1  SQL:- SQL Server 2nd, 3rd, 4th... Highest salary using MAX ,   DENSE_RANK, and  SUB QUERY ! We can find the 2 nd , 3 rd , 4 th   ... n th   highest salary using SQL Server the below query,  In the below query use top 1 for the 2 nd   highest salary, top 2 for the 3 rd   highest salary, top 3 for the 4 th   highest salary,.... n th   for the (n+1) highest salary. Method 2 for MySQL:- You can use LIMIT  to get 2 nd , 3 rd , 4 th   ... n th   highest salary! Examples using  MAX ,   DENSE_RANK in SQL ==================================== DECLARE @Employees TABLE (   employee_id INT,   name VARCHAR(25),   salary INT ) INSERT INTO @Employees (employee_id, name, salary) VALUES  (1, 'Anil', 62000), (2, 'Alok', 55000), (3, 'Ajay', 70000), (4, 'Rahul', 62000), (5, 'Diya', 75000); SELECT  SALARY,     DENSE_RANK() OVER (ORDER BY SALARY DESC) AS SAL...