SHOW TOP 1 in ORACLE [duplicate] - sql

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 2 months ago.
Here's the query
SELECT concat(FIRSTNAME,LASTNAME) AS NAME, HIREDATE from EMPLOYEE ORDER BY HIREDATE
WHERE ROWNUM = 1;
I've been trying to use rownum function but I keep getting the error - "ORA-00933: SQL command not properly ended".

You can try like this:
SELECT *
FROM (SELECT concat(FIRSTNAME,LASTNAME) AS NAME, HIREDATE from EMPLOYEE ORDER BY HIREDATE )
WHERE ROWNUM = 1;

Related

fetch the 1st row from the table for an employee with highest salary [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 3 years ago.
I am searching for a query to find the details of highest paid employee in the entire organization. I am using oracle DB.
Queries which I tried are:
select * from EMP order by SAL desc FETCH FIRST 1ROW ONLY;
ERROR: SQL command not properly ended
select * from EMP order by SAL desc where rownum = 1;
ERROR: SQL command not properly ended
select * from EMP order by SAL desc LIMIT 0 , 1;
ERROR: SQL command not properly ended
You could use:
SELECT *
FROM (SELECT * FROM emp ORDER BY sal DESC)
WHERE ROWNUM = 1
Please note that if rows have identifical salaries, ROWNUMBER will rank them randomly. To handle ties, you could use RANK() instead (or maybe DENSE_RANK(), depending on your needs):
SELECT *
FROM (
SELECT
e.*,
RANK() OVER(ORDER BY sal DESC) rn
FROM emp
)
WHERE rn = 1
There is an issue with all three statements.
Try this:
select * from EMP order by SAL desc FETCH FIRST ROW ONLY;
or use an analytical function ROW_NUMBER:
SELECT T.* FROM
(SELECT
T.*, ROW_NUMBER() OVER(ORDER BY SAL DESC NULLS LAST) RN
FROM EMP) T
WHERE RN = 1
Cheers!!

To delete duplicate rows in Oracle SQL [duplicate]

This question already has answers here:
Removing duplicate rows from table in Oracle
(24 answers)
Closed 3 years ago.
I've used this code to select the duplicate rows, it went right,
SELECT name, COUNT(name)
FROM emp
GROUP BY name
HAVING COUNT>1;
But it doesn't help me in deleting the same selected rows....
DELETE emp
WHERE name IN ( SELECT name, COUNT(name)
FROM emp
GROUP BY name
HAVING COUNT >1);
Remove the second column from the inner SELECT, something like this:
DELETE
FROM emp
WHERE name IN( SELECT name FROM emp GROPU BY name HAVING COUNT(name) > 1)

LIMIT does not work with UNION [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 5 years ago.
What is wrong with below code?
SELECT first_name, last_name
FROM employees
UNION ALL
SELECT first_name, last_name
FROM dependents
ORDER BY last_name
LIMIT 6 OFFSET 1;
Even a simple query with LIMIT doesn't seem to be working :
SELECT first_name, last_name
FROM dependents
ORDER BY last_name
LIMIT 6 OFFSET 1;
Getting this error repeatedly: ORA-00933: SQL command not properly
ended
I believe oracle does not support LIMIT. Try this
ORDER BY last_name
OFFSET 1 ROWS FETCH NEXT 6 ROWS ONLY;
Note this syntax supports from Oracle 12c
What is wrong with the code is that Oracle does not support limit. Period. The most recent versions support the ANSI standard fetch first <n> rows only.
Perhaps you intend:
SELECT e.*
FROM (SELECT e.*, ROW_NUMBER() OVER (ORDER BY last_name) as seqnum
FROM (SELECT first_name, last_name FROM employees
UNION ALL
SELECT first_name, last_name
FROM dependents
) e
ORDER BY last_name
) e
WHERE seqnum BETWEEN 1 and 7;

Select 2 lowest values from a query in SQL [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 7 years ago.
I am trying get information of 2 lowest salary holders from default "scott" table.
This is the query I am trying:
SELECT TOP 2 * FROM emp ORDER BY sal ASC
But I'm getting this error:
ORA-00923: FROM keyword not found where expected
Screenshot:
In the most recent versions of Oracle, you can use the ANSI standard:
SELECT emp.*
FROM emp
ORDER BY sal ASC
FETCH FIRST 2 ROWS ONLY;
In older versions:
SELECT e.*
FROM (SELECT emp.*
FROM emp
ORDER BY sal ASC
) e
WHERE rownum <= 2;
You can use ROWNUM in Oracle to get two first rows of your query results.
SELECT EMP1.* FROM (SELECT * FROM EMP ORDER BY SAL ASC) EMP1 WHERE ROWNUM < 3;
TOP X is used by SQL Server
Fetch first is used by Oracle and DB2
(rownum is also available in Oracle)
Limit is used by mysql
Many can also use a windowing function (ROW_NUMBER()) in a sub query.
You want
select * from emp ORDER BY sal ASC
fetch first 2 rows only

SQL Query to find Nth highest salary [duplicate]

This question already has answers here:
How to fetch the nth highest salary from a table without using TOP and sub-query?
(18 answers)
SQL query to find Nth highest salary from a salary table
(11 answers)
Closed 9 years ago.
How to find Nth Highest Salary without using any subquery in MS SQL?
;WITH cte1
AS
(
SELECT ROW_NUMBER() OVER(ORDER BY SALARY DESC) AS RN, * FROM Salaries
)
SELECT *
FROM cte1
WHERE RN = 5 <-- Nth highest
Check out the row_number function. :)
http://msdn.microsoft.com/en-us/library/ms186734.aspx