First I did:
CREATE TABLE Persons (
id INTEGER not null,
capital_loss INTEGER,
capital_gain INTEGER,
salary INTEGER,
PRIMARY KEY ( id )
);
I want to get the id and the salary of one row such that:
max(capital_gain-capital_loss) and salary =50
How about this
select id, salary, max(capital_gain - capital_loss)
from Persons
where salary = 50
group by id, salary
Having on mind your conditions (no matter which row is returned from all of maximum ones), and if you are using mysql:
SELECT id, salary FROM Persons
WHERE salary=50
ORDER BY (capital_gain - capital_loss) DESC LIMIT 1;
Related
I've one database and I try to find the solution for my one query.
I want to find the 5th person name who earn a max salary in my database. so can I do
For example this is my execute query. you can see 7 people name but i want to see only 5th person name like only 'Mahesh"
my table is like that
FirstName varchar (45),
LastName varchar (45),
Birthdate datetime,
position varchar (35),
DOJ datetime,
DeptID int,
Salary decimal (10,2)
and this is my execute query. you can see 7 people name but i want to see only 5th person name like only 'Mahesh"
Salary Name
90000.00 Amita
90000.00 Carla
89500.00 Sarah
89000.00 Gunjan
85000.00 **Mahesh**
96000.00 John
86000.00 Charles
So my question is
I want to find the only one person name and salary from my data who earn 5th max salary.
Assuming you want the user with the 5th highest salary, you could use an embedded query.
SELECT TOP 1 *
FROM (SELECT TOP 5 *
FROM YourTable
ORDER BY Salary DESC) tmp
ORDER BY tmp.Salary
Or if you want to include ties.
SELECT *
FROM YourTable
WHERE Salary = (SELECT TOP 1 Salary
FROM (SELECT TOP 5 Salary
FROM YourTable
ORDER BY Salary DESC) tmp
ORDER BY tmp.Salary)
I'm a novice. I have the following Employee table.
ID Name Country Salary ManagerID
I retrieved the 3rd max salary using the following.
select name , salary From (
select name, salary from
employee sort by salary desc limit 3)
result sort by salary limit 1;
How to do the same to display 3rd max salary for each country? can we use OVER (PARTITION BY country)? I tried looking in the languageManual Windowing and Analytics but I'm finding it difficult to understand. Please help!
You're definitely on the right track with windowing functions. row_number() is a good function to use here.
select name, salary
from (
select name
, salary
, row_number() over (partition by country order by salary desc) idx
from employee ) x
where idx = 3
when you order by salary, make sure that it is a numerical type, or it will not be sorted correctly.
I have a table called workers which includes a few persons by their names, their salary and their working station. The table looks something like the following:
|Name|Station|Salary|
|Kyle|1 |2200 |
|Lisa|2 |2250 |
|Mark|3 |1800 |
|Hans|4 |1350 |
This might sound like a very obvious beginner question but I cannot get it work. I would like to select the name of the person with the highest salary. Thank you for reading, and have a nice one.
Select name
from table
where salary = (select max(salary) from table)
I dont know if you want to include ties or not (if two people have the same salary and it is the max salary.
What this does is find the max salary and then uses that in the query to find all people with that salary. You will need to replace the word table with whatever your table name is.
Try this
SELECT top 1 Name
FROM tableName
ORDER BY Salary DESC
You don't mention DBMS:
select name
from table
order by salary desc
fetch first 1 rows only
If your DBMS support OLAP functions:
select name from (
select name, row_number() over (order by salary desc) as rn
from table
) where rn = 1
Try to do:
SELECT TOP name
FROM yourtable
WHERE salary = (SELECT MAX(salary) FROM yourtable)
You must use subquery to get name and highest salary:
select Name, Salary from tb_name where salary=(select max(salary) from tb_name);
I have a table named employee_salary, with three columns: empsal_id, empsal_name and empsal_sal. The data is as follows:
empsal_id empsal_name empsal_sal
1 dilip 14000
2 santosh 20000
3 amit 32000
4 dilip 22000
5 amit 38000
6 santosh 25000
7 dilip 30000
The empsal_id is an Identity column with a Seed and an Increment of 1, and is the Primary Key. I want to return the name and current salary of each employee. Salary can decrease as well as increase, so current does not necessarily mean highest.
So I need the following output:
empname emp_sal
dilip 30000
amit 38000
santosh 25000
I am using Microsoft SQL Server, and I have to do this in a single query.
This query will return each employee, along with their highest salary:
SELECT
empsal_name, MAX(empsal_sal)
FROM
employee
GROUP BY
empsal_name
This query will return each employee, along with their current salary (i.e. the salary with the highest empsal_id:
SELECT
empsal_name, empsal_sal
FROM
employee e1
WHERE
empsal_id =
(SELECT MAX(empsal_id)
FROM employee e2
WHERE e1.empsal_name=e2.empsal_name)
Personally, I think you would be better off using an effective date column (e.g. empsal_effectivedate) to determine which record is the most current, so this query will return each employee, along with their current salary (i.e. the salary with the most recent empsal_effectivedate), assuming there is an empsal_effectivedate field:
SELECT
empsal_name, empsal_sal
FROM
employee e1
WHERE
empsal_effectivedate =
(SELECT MAX(empsal_effectivedate)
FROM employee e2
WHERE e1.empsal_name=e2.empsal_name)
Assuming there is also an ID in the table and also assuming that the larger this ID is the most recent the salary is for that employee you can do
SELECT DISTINCT
e.empname,
(SELECT TOP 1
emp_sal
FROM
employee s
WHERE
s.empname = e.empname
ORDER BY
recid DESC) AS emp_sal
FROM
employee e
(also assuming that empname is unique for an employee)
because of the many assumptions though : you should probably post all the columns of the table and what they mean ..
SELECT empname, MAX(emp_sal)
FROM employee
GROUP BY empname
UPDATED:
SELECT DISTINCT EmpA.empname,
EmpA.emp_sal
FROM Employee AS EmpA
INNER JOIN ( SELECT EmpName, MAX(recID) AS recid
FROM Employee
GROUP BY EmpName
) AS EmpB ON EmpA.recid = EmpB.recid;
You need a date field to determine the latest inserted row . In case if the table is linked to some other table which has date column in it .Then its pretty easy to fetch the current data .
For Example
Employee Table
{
EmpName varchar(30) PK,
EmpAddress varchar(255) ,
Company varchar(30),
CurrentTimeStamp Datetime
}
Salary Table
{
EmpName varchar(30) FK,
EmpSalary int
}
To get the Latest record use the CTE function
With LatestSal(EmpName ,EmpSalary)
AS
(
Select row_number() over (PARTITION BY b.[EmpName], order by CurrentTimestamp DESC) as seq
b.EmpName,b.EmpSalary
From Employee as a,
Salary as b
on a.[EmpName]=b.[EmpName]
)
Select EmpName,EmpSalary
from LatestSal
where seq=1
I have a salary table, were I want to find the 2nd largest salary record, 3rd largest salary record and so on.
To retrieve 2nd largest record I use the following query
Select Top 1 * from SalaryTable
where salary < (Select Max(Salary) from SalaryTable)
order by Salary desc
Likewise, how can I find 3rd largest record or fourth largest record and so on? Is there a way to retrieve the specific records?
you can get using RANK () function in SQL Server
;WITH CTE AS
(
SELECT ..., RANK() OVER (ORDER BY emp_salary) AS rn
FROM myTable
)
SELECT ...
FROM CTE
WHERE rn = n -- (value of should be replace with numberic number for ex. 1, 2, 3)
Use RANK() function
SELECT *
FROM
(SELECT *
,RANK() OVER (ORDER BY salary) AS SalRnk
FROM SalaryTable) AS tblSal
WHERE tblSal.SalRnk = 2 -- for second highest record. change this value to 1,2,3,4 etc... for various rank records
http://msdn.microsoft.com/en-us/library/ms189798.aspx
SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP n salary
FROM employee
ORDER BY salary DESC) a
ORDER BY salary
where n > 1 (n is always greater than one)
You can put any value instead of n, it gives you required largest salary.
select top 1 salary
from(Select Distinct top n salary from Salary order by desc)a
order by salary Asc