LIMIT does not work with UNION [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 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;

Related

Maximum number 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 last year.
I'm having these schema
STUDENT (Sid, Name, City, Adm_date, Paper1, Paper2)
And I want to find the maximum number of students were admitted in which date and the city where maximum number of students lives.
I'm using these queries in ORACLE 11g Express Edition:
For City:
SELECT COUNT(Sid) AS No_of_students, City
FROM STUDENT
GROUP BY City
ORDER BY No_of_students DESC
LIMIT 1;
And For Date:
SELECT COUNT(Sid) AS No_of_students, Adm_date
FROM STUDENT
GROUP BY Adm_date
ORDER BY No_of_students DESC
LIMIT 1;
But here I'm getting error for both these tables:
ORA-00933: SQL command not properly ended
LIMIT is not part of Oracle's SQL dialect. In Oracle you would use the standard SQL FETCH FIRST clause instead, but in the old version you are using this is not available either.
And whatever approach you are following, keep in mind that there can be ties, i.e. more than one city or date with the maximum number of students. So LIMIT 1 would jump to short. FETCH FIRST does have a ties clause, but as mentioned, it is not available in your old Oracle version.
In Oracle 11g I'd use a window function to solve this:
SELECT city, no_of_students
FROM
(
SELECT
city,
COUNT(sid) AS no_of_students,
MAX(COUNT(sid)) OVER () AS max_no_of_students
FROM STUDENT
GROUP BY city
) counted
WHERE no_of_students = max_no_of_students;
Please try rownum with subquery (Since Limit is not available in Oracle):
For city wise:
select * from
(SELECT COUNT(Sid) AS No_of_students, City
FROM STUDENT
GROUP BY City
ORDER BY No_of_students DESC) where rownum=1
For Adm_date wise:
select * from
(SELECT COUNT(Sid) AS No_of_students, Adm_date
FROM STUDENT
GROUP BY Adm_date
ORDER BY No_of_students DESC) where rownum=1
Found a great article explaining the mechanism of selecting top-N rows using rownum:
https://blogs.oracle.com/oraclemagazine/post/on-rownum-and-limiting-results

Oracle SQL Finding the 5 lowest salaries

I am trying to answer the following question. Show ID_Number and name for the five lowest paid employees.
This is the table with employees:
CREATE TABLE Employees
(ID_No CHAR(4) NOT NULL,
Name VARCHAR(50) NOT NULL,
Hire_Date DATE NOT NULL,
Position VARCHAR(20) CHECK(Position IN('CHAIRMAN','MANAGER','ANALYST','DESIGNER','PROGRAMMER','SALES REP','ADMIN','ACCOUNTANT')),
Salary NUMERIC(8,2) NOT NULL,
Mgr_ID_No CHAR(4) NULL,
Dept_No SMALLINT NULL);
I will add that I've been trying a few methods and "limit" and "top" do not work for some reason.
In Oracle 12c :
-- more than 5 rows being returned, if multiple rows
-- match the value of the 5th row
SELECT e.ID_No, e.Name
FROM Employees e
ORDER BY e.Salary
FETCH FIRST 5 ROWS WITH TIES;
-- only 5 rows being returned, even if multiple rows
-- match the value of the 5th row
SELECT e.ID_No, e.Name
FROM Employees e
ORDER BY e.Salary
FETCH FIRST 5 ROWS ONLY;
-- NEXT clause may be replaced with FIRST
SELECT e.ID_No, e.Name
FROM Employees e
ORDER BY e.Salary
FETCH NEXT 5 ROWS ONLY;
Prior to Oracle 12c :
SELECT e.ID_No, e.Name
FROM ( SELECT ID_No, Name, row_number() over (order by salary) seq FROM Employees ) e
WHERE e.seq <= 5
ORDER BY e.seq;
queries may be used for Top-N Queries
SELECT ID_NO, NAME
FROM EMPLOYEES
ORDER BY SALARY
FETCH FIRST 5 ROWS ONLY
The row_number() window function should work here (note that window functions can't be used in WHERE/HAVING clauses).
SELECT ID_No, Name
FROM (SELECT ID_No, Name, Row_Number() OVER (ORDER BY Salary) RN
FROM Employees)
WHERE RN <= 5;
In Oracle ROWNUM could be used.
SELECT *
FROM (SELECT ID_No,
Name
FROM Employees
ORDER BY Salary) x
WHERE ROWNUM <= 5;
Another method could be a subquery counting the rows with lower or equal salary.
SELECT EO.ID_No,
EO.Name
FROM Employees EO
WHERE (SELECT COUNT(*)
FROM Emplyoees EI
WHERE EI.Salary <= EO.Salary) <= 5;

top n queries using the offset clause

SELECT last_name
FROM employees
order by last_name DESC
fetch first 5 rows only;
I am a beginner in SQL. I am trying to run this query but keep getting an error
sql command not properly ended
What am I doing wrong?
oracle version < 12
select * from
(select last_name, row_number() over(order by last_name desc nulls last) rnm
from employees)
where rnm<=5;
oracle version 12
SELECT last_name
FROM employees
ORDER BY last_name DESC
OFFSET 1 ROWS FETCH NEXT 5 ROWS ONLY;

ORACLE 12C - SELECT query using ROWNUM

I have to get second five (6-10) best salaries records from sorted table using ROWNUM.
Using ROWNUM is necessary.
When I execute query:
SELECT ROWNUM AS position, name, salary
FROM (SELECT name, salary
FROM employees
ORDER BY salary DESC)
WHERE ROWNUM <= 10;
I get a first 10 best records.
And now when I try execute query:
SELECT ROWNUM AS position, name, salary
FROM (SELECT name, salary
FROM employees
ORDER BY salary DESC)
WHERE ROWNUM >= 6 AND ROWNUM <= 10;
I get a empty table. Why doesn't it work?
As explained in the documentation, rownum is evaluated as the rows are fetched. If you never fetch the first row, you never get to the second. Hence, no rows are fetched:
Conditions testing for ROWNUM values greater than a positive integer
are always false. For example, this query returns no rows:
SELECT * FROM employees
WHERE ROWNUM > 1;
But, more importantly, you are using Oracle 12C. So, use fetch first instead of rownum. This has multiple advantages. Besides being standard SQL, you don't need a subquery:
SELECT name, salary
FROM employees
ORDER BY salary DESC
FETCH FIRST 10 ROWS ONLY;
And for your second:
SELECT name, salary
FROM employees
ORDER BY salary DESC
OFFSET 5 ROWS
FETCH FIRST 5 ROWS ONLY;
I write in queston that using ROWNUM is necessary, because it's
academic task.
In such a case use a subquery
SELECT name, salary
FROM (
SELECT name, salary, ROWNUM as my_rownum
FROM employees
ORDER BY salary DESC
)
WHERE my_rownum BETWEEN 6 AND 10
You can use below query and try....
SELECT name,salary from
( SELECT name,salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rn
from employees )
where rn between 6 and 10;

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