select sal from emp order by sal desc limit 1,1;
SQL command not properly ended GIVES ERROR
I wan´t to get the employee with the NTH highest salary in each department.
I´m using SQL.
You can use the ANSI syntax offset ... fetch ...:
select sal from emp order by sal desc offset 1 rows fetch next 1 rows only;
I hope you find it useful Link
but you will need group by if you need it by section
select
department,
max(sal)
from emp
group by department
Related
I have a table on employees like this:
ENAME JOB
Jack Clerk
Adam Manager
Raphael President
And my idea is to get the names that have the maximum length and the minimum length (1 per each) and in case the length is the same, take the one that goes first alphabetically (for example in the case of Jack and Adam, both with 4 characters, it would take Adam):
ENAME LENGTH
Adam 4
Raphael 7
I couldn't find the way of doing it in one unique query so I tried to join two queries but it keeps giving me error ('ORA-00933: SQL command not properly ended') and I don't know why:
SELECT ENAME, LENGTH(ENAME) FROM EMP
GROUP BY ENAME
HAVING LENGTH(ENAME) = (SELECT MAX(LENGTH(ENAME)) FROM emp)
ORDER BY ENAME
FETCH FIRST 1 ROW ONLY
union all
SELECT ENAME, LENGTH(ENAME) FROM EMP
GROUP BY ENAME
HAVING LENGTH(ENAME) = (SELECT MIN(LENGTH(ENAME)) FROM emp)
ORDER BY ENAME
FETCH FIRST 1 ROW ONLY;
SELECT ENAME, LENGTH(ENAME) FROM EMP
GROUP BY ENAME
HAVING LENGTH(ENAME) = (SELECT MAX(LENGTH(ENAME)) FROM emp)
--ORDER BY 1
--FETCH FIRST 1 ROW ONLY
union
SELECT ENAME, LENGTH(ENAME) FROM EMP
GROUP BY ENAME
HAVING LENGTH(ENAME) = (SELECT MIN(LENGTH(ENAME)) FROM emp)
ORDER BY 1
FETCH FIRST 1 ROW ONLY;
One method uses aggregation and union all:
select min(name) keep (dense_rank first order by len(name) asc), min(len(name))
from emp
union all
select min(name) keep (dense_rank first order by len(name) desc, max(len(name))
from emp;
The keep syntax is Oracle's rather verbose way of having a "first" aggregation function.
This question already has answers here:
How to find the employee with the second highest salary?
(5 answers)
Closed 3 years ago.
I have to get the name of employee with the second highest salary the table name from where I am fetching is emp. I know the query for second highest salary which is
select max(sal)
from emp
where sal < (select max(sal) from emp)
it works and it returns the right answer.
But I have to get the name of the employee as well. I simply tried
select name, max(sal)
from emp
where sal < (select max(sal) from emp)
I get this error:
ORA-00937: not a single-group group function
how can i remove the error in order to get the name and salary both.
thank you to anyone who helps.
You can use
select name,sal from emp where sal = (select max(sal) from emp where sal < (select max(sal) from emp));
use this :
with cte (
select ROW_NUMBER() over(order by sal desc)rnum ,name,sal from emp )
select * from cte where rnum = 2
You can get this easily with a window function. Try something like this:
SELECT name, sal
FROM emp
QUALIFY RANK OVER(ORDER BY sal DESC) = 2
This will order your rows by Salary and then give each row a ranking. Then it will return the rows with ranking = 2.
If you want to ensure you only get one row back, change RANK to ROW_NUMBER.
I have tried checking all the duplicate answers provided in this site but couldn't able to come up with rectifying my error could you guys help me out on this?
Getting this error
ORA-00923: FROM keyword not found where expected
SELECT TOP 1 sal FROM emp WHERE sal in
(SELECT DISTINCT TOP 3 sal FROM emp ORDER BY sal DESC)
ORDER BY sal ASC;
I am solving this using oracle apex site
You appear to want to find thew record with the third-lowest salary.
You can use a subquery to assign an analytic rank to each salary, and then filter on that:
select * from (
select e.*, dense_rank() over (order by sal desc) as rnk
from emp e
)
where rnk = 3;
From 12c you can use the offset and fetch syntax to do the same thing:
select * from emp
order by sal desc
offset 3 rows
fetch first row only;
You need to decide how to deal with ties though - if more than one employee shares that third-lowest salary; or even if the lowest and second lowest are awarded to more than one person. Depending on what you want to happen you can look at variations of the 12c row-limiting syntax, and other analytic functions - row_number(), dense_rank() - and their windowing options.
I was able to get my query now
SELECT A.* FROM emp A where 3 =(SELECT COUNT(DISTINCT sal) FROM emp B WHERE A.sal <= B.sal);
Yet another option, using analytic function:
WITH ranks
AS (SELECT empno, DENSE_RANK () OVER (ORDER BY sal DESC) rnk FROM emp)
SELECT e.*
FROM emp e, ranks r
WHERE e.empno = r.empno
AND r.rnk = 3;
emp_name |salary
---------------
A |12568
B |3000
C |7852
D |2568
E |9852
F |1598
G |8569
I want a sql query to fetch the lowest 3 salaried employees
If you are using Oracle 12c or later you can make your query simpler with fetch. Instead of writing inner queries like this.
SELECT *
FROM
(SELECT * FROM EMPLOYEES EMP ORDER BY EMP.SALARY ASC
)
WHERE ROWNNUM <= 3
You can combine them into a single query.
SELECT * FROM employees emp ORDER BY emp.salary ASC
FETCH FIRST 3 ROWS ONLY;
More Information on the syntax and construct is available here.
http://www.dba-oracle.com/t_offset_fet_first_rows_only.htm
ORACLE:
SELECT emp_name
FROM ( SELECT *
FROM employees e
ORDER BY e.salary ASC)
WHERE ROWNUM < 4
Good luck!
You can use top 3 to get three record after ordering them in descending or ascending order. I have SQL server syntax but you can have idea from this for you target DBMS.
For top three max salaries
Select top 3 emp_name, salary
order by salary desc
For top three minimum salaries
Select top 3 emp_name, salary
order by salary asc
USE ASC AND LIMIT
Select emp_name, salary FROM TABLE_NAME
order by salary ASC LIMIT 3;
You didn't specify your DBMS, so this is ANSI SQL:
select emp_name, salary
from (
select emp_name, salary,
dense_rank() over (order by salary) as rnk
from employees
) t
where rnk <= 3;
This will also deal with employees that have the same salary. So the result might be more then three rows if more then one of the employees with the lowest salary have the same salary.
Say there's a table
Name Salary
Joe 4000
Steve 6000
I could just do this
select name from emp where salary = (select max(salary) from emp);
but is there a way to do this without using a subquery?? Please help.
EDIT: Sorry I forgot to mention that I'm using Oracle 10g and LIMIT doesn't work on it
You didn't mention the version of Oracle.
On Oracle 12 there is a new low limiting clause that can be used:
SELECT name
FROM emp
ORDER BY salary desc
FETCH FIRST 1 ROWS ONLY;
There are examples in documentation: https://docs.oracle.com/database/121/SQLRF/statements_10002.htm#BABEAACC
On earlier versions it can't be done without using a subquery, but if you must then create a view:
CREATE VIEW emp_ordered AS
SELECT *
FROM emp
ORDER BY salary desc;
and then query this view in this way:
SELECT * FROM emp_ordered
WHERE rownum <=1
ANSI SQL answer (no dbms specified):
select Name, Salary
from emp
order by Salary desc
fetch first 1 row only
Edit: Will work with newer Oracle versions.
In Oracle 12c, the top-n row limiting feature is introduced. Which allows to ORDER the rows without an additional subquery. So, no more dependency on ROWNUM and explicit sorting in a subquery.
For example,
SQL> SELECT ename, sal FROM emp ORDER BY sal DESC
2 FETCH FIRST 1 row only;
ENAME SAL
---------- ----------
KING 5000
SQL>
Update Regarding duplicate rows
There is an option WITH TIES which will include the duplicate rows.
For example,
SQL> insert into emp(empno, ename, sal) values(1234, 'LALIT', 5000);
1 row created.
SQL> SELECT ename, sal FROM emp ORDER BY sal DESC FETCH FIRST 1 ROWS WITH TIES;
ENAME SAL
---------- ----------
KING 5000
LALIT 5000
SQL>
Try this
SELECT name FROM emp
ORDER BY salary DESC
LIMIT 1
Try
select * from emp order by salary DESC limit 1