MS Access limit syntax - sql

I have a very basic Access DB table called SCHEDULE. When I make the following SQL Query entry, things work perfectly.
SELECT *
FROM schedule
ORDER BY start;
Now what I want is to skip the first 3 records and view the remaining 10 like so:
SELECT *
FROM schedule
ORDER BY start
LIMIT 3,7;
But I receive a "SYNTAX ERROR (MISSING OPERATOR) IN QUERY EXPRESSION 'START LIMIT 3'
So as a test I tried the following:
SELECT *
FROM schedule
ORDER BY start
LIMIT 0,3;
Same error.
What is the correct syntax?

You can do this with subqueries is MS Access:
select top 7 *
from (select top 10 *
from schedule
order by start
) as s
order by start desc;
If you want the final results in ascending order rather than descending order, use this as a subquery and add order by start.
EDIT:
If you just want to avoid the first three:
select *
from schedule
where start not in (select top 3 start from schedule order by start);

There is no LIMIT operator in MS Access, that's why you are getting the syntax error, but you can get this result using TOP and a subquery, like the following:
SELECT *
FROM schedule
WHERE schedule.ID IN
(
SELECT TOP 7 SUB.ID
FROM [
SELECT TOP 10 schedule.start, schedule.ID
FROM schedule
ORDER BY schedule.start, schedule.ID
]. AS SUB
ORDER BY SUB.start DESC, SUB.ID DESC
)
ORDER BY schedule.start, schedule.ID

Related

Is there an efficiency problem with my query to select middle rows?

I am trying to select rows 3 - 5 of:
SELECT *
FROM Finance_User
ORDER BY email DESC
I originally had just:
SELECT
ROW_NUMBER() OVER (ORDER BY email DESC) AS RowNum, *
FROM
Finance_User
WHERE
RowNum BETWEEN 3 AND 5
But this did not work as RowNum was an invalid column.
Instead I did the below:
WITH OrderedUsers AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY email DESC) AS RowNum, *
FROM
Finance_User
)
SELECT *
FROM OrderedUsers
WHERE RowNum BETWEEN 3 AND 5
This works perfectly fine. However, I am concerned that there might be performance issues with this as it seems to be selecting from the table twice?
ROW_NUMBER() with a CTE (or a subquery) won't scan the table twice. However using the window function might incur additional processing for the RDBMS.
You could achieve the same results with ORDER BY ... OFFSET ... FETCH ..., available starting SQL-Server 2012, that are provided specifically for the purpose of paging a resultset:
SELECT *
FROM Finance_User
ORDER BY email DESC
OFFSET 2 ROWS FETCH NEXT 3 ROWS ONLY
From the documentation:
We recommend that you use the OFFSET and FETCH clauses instead of the TOP clause to implement a query paging solution and limit the number of rows sent to a client application.
Your query is fine.
WITH AS clause is a Common Table Expression which means that this query may be reused later and should be cached if needed and possible. So there should not be any problem with "selecting from the table twice".
The same result you can get with this query:
SELECT * from
(SELECT ROW_NUMBER() OVER (ORDER BY email DESC) AS RowNum, * FROM Finance_User)
where RowNum between 3 and 5
And finally, you can always check execution plan and make sure of it as well.

Issue with SQL "Order by"

I would like to compose a SQL query that for example selects the top 2 values of a sorted table using ORDER BY with a query below, but I'm getting unexpected results
select top 2 * from (select top 100 percent * from events order by dates desc) a ;
let's say for example, if the values of the table were 1,3,4
using
select top 100 percent * from events order by dates desc;
would give me 4,3,1
but with
select top 2 * from (select top 100 percent * from events order by dates desc) a;
I get 1,3 instead of 4,3
I was wondering why is that the case and am I missing something?
use order by in outer query as well
select top 2 * from (select top 100 percent * from events order by dates desc) a order by dates desc

Outer Query OrderBy does not depends on Inner Query's Resultset

I am not having deep knowledge of SQL execution order.
When I Execute a Query
select top 2 * from Configuration
It Gives me
ABC1,100,Data001
ABC2,200,Data002
When I Execute a Query
select top 2 * from Configuration order by 1 desc
It Gives me
XYZ1,400,Data100
XYZ2,300,Data099
When I Execute a Query
select * from (select top 2 * from Configuration) as a order by 1 desc
It gives me
XYZ1,400,Data100
XYZ2,300,Data099
My problem is why i am getting
XYZ1,400,Data100
XYZ2,300,Data099
as output instead
ABC1,100,Data001
ABC2,200,Data002
As per my knowledge Inner Query Will Return two rows to Outer Query.
Outer Query Will process those two rows with
From->Where->group by->having->Select->Order by
order of Execution and Will give output as two rows which I mentioned as expected. But Outer Query's order by is affecting on whole table of Inner Query.
Please comment where I am making mistake.
SQL Server has no any sense to populate by default order by.
In your inner query you have not specified order by so it returned wrong result.
Now use below code for SQL Server 2008 R2
SELECT * FROM (SELECT TOP 2 * FROM Configuration ORDER BY 1) AS a ORDER BY 1 DESC
One other alternative is : Comman Table Expression , Like below,
;WITH cteTest AS
(
SELECT TOP 2 *
FROM Configuration ORDER BY 1
)
SELECT * FROM cteTest ORDER BY 1 DESC

What is the best and simplest SQL Query to get the Best 3 scores and their corresponding details.

I have a table 'results' in which the scores are stored. Its ordered by the date of `test'. Now i need to get the details of the users who have secured 1st , 2nd and 3rd in the test.
i suppose that to get the 1st score i can use:
select *
from RESULTS
where SCORE=max(SCORE)
But what about for 2nd and 3rd.
Also is it possible to get the three of them together in a single query.
You could try (MySql)
SELECT * FROM results
ORDER BY score DESC
LIMIT 3
or (MS-SQL)
SELECT TOP 3 * FROM results
ORDER BY score DESC
or (Oracle)
SELECT * FROM
(SELECT * FROM results
ORDER BY score DESC) t
WHERE ROWNUM < 4

sql query to find fifth record

How can i find the fifth record in a table using sql query?
If you are feeling argumentative, consider using "SELECT * FROM table LIMIT 1" and arguing that since SQL does not promise to return results in any particular order, the row returned is spiritually equivalent to the fifth, then show your tattoo: "The nth element of an unordered set is meaningless!"
SELECT TOP 1 * FROM (SELECT TOP 5 * FROM Table T ORDER BY Column ASC) ORDER BY Column Desc
If you are using SqlServer you could use the TOP keyword to achieve this.
select top 1 * from(
select top 5 * from myTable order by orderingColumn) as A
order by orderingColumn desc
If you are using Oracle this should work (however i am not able to test this now)
select *
from (
select *, row_number() over (order by orderingColumn) r
from items
)
where r = 5;
SELECT * FROM table LIMIT 1 OFFSET 4;
Fifth record only in MySQL
SELECT * FROM anytable LIMIT ORDER BY id LIMIT 4,1
For SQL Server (recent-ish incarnations, anyway) something like this should work:
SELECT
*
FROM
(
SELECT
*,
ROW_NUMBER() OVER (ORDER BY the_table.the_column) 'row_num'
FROM
the_table
) numbered_rows
WHERE
row_num = 5
However, I'd actually put my vote with Thomas L Holaday's answer :)