get first and last element from sqlite database - sql

Following example here -> https://www.tutorialspoint.com/how-to-get-the-first-and-last-record-of-the-table-in-mysql
To get first and last element of database we can use UNION.
I have a table called location with three queries.
And I would like to get rows with id 1 and in this case 3 (db will be much larger)
So I tried
(select * from location order by id ASC LIMIT 1)
UNION
(select * from location order by id DESC LIMIT 1);
But It doesn't work in my example. Does sqlite allow to use UNION operation?

SQLite does support UNION but does not like the queries inside the parentheses.
Use each of your queries as a subquery:
select * from (select * from location order by id ASC LIMIT 1)
UNION
select * from (select * from location order by id DESC LIMIT 1);

Related

How to use a fast way for oracle sql to find the last log column in Jobs run log

The run log have too much data that i can't search it in fast ways.Now i only want to get the last log data in this log table in oracle jobs. Pls give me help.Best!
There are various options to return a sinlge row but these will do it with only a single table scan:
SELECT *
FROM ( SELECT *
FROM log_table
ORDER BY entry_date DESC
)
WHERE ROWNUM = 1;
or
SELECT *
FROM ( SELECT l.*,
ROW_NUMBER() OVER ( ORDER BY entry_date DESC ) rn
FROM log_table l
)
WHERE rn = 1;
or (for Oracle 12)
SELECT *
FROM log_table
ORDER BY entry_date DESC
FETCH FIRST 1 ROW ONLY;
Better to use max(date) like this
select * from log where date=(select max(date) from log)
It'll use indexes (if they exists)
If you have no date field you can use, and this can be even better:
select * from log where rowid=(select max(rowid) from log)

Fetch first 5 and last 5 records through a single statement

I am working on python sqlite3.
This statement gets records 5 - 14;
SELECT * FROM something LIMIT 5, 10;
But how do I get, lets say the first five and last five records through a single statement?
You can combine output of two select statement like this:
(SELECT * FROM `something` order by some_column_name
limit 0,5)
union
(SELECT * FROM `something` order by some_column_name desc
limit 0,5
)
Specify some ordering of rows, so that it will select rows accordingly.
Maybe the better way is using rowid in your order by clause to get the first and last rows based on inserting the rows:
select test from
(select test,rowid from table1 order by rowid asc limit 0,5)t1
union all
select test from
(select test,rowid from table1 order by rowid desc limit 0,5)t2;
Here is a sample in SQL Fiddle

Select entire row based on a max value of a field - without nesting

select *
from webOrders
where lastModifiedDate in (select max (lastModifiedDate) from webOrders)
Is there a simpler way without nesting the selects?
Doing something like this also results in an error:
select id, amount, quantity, max(lastModifiedDate) from webOrders.
There are a couple of approaches, depending on your needs.
If you only need one row returned, you can sort on the column of interest and return the top row. For example:
select top 1 *
from `order`
order by last_modified_date desc
if you use SQL Server or
select *
from `order`
order by last_modified_date desc
limit 1
if you use MySQL.
If you need to get one row per group, then you do typically have to use a subquery or a join.
select TOP 1 * from [order] order by lastModifiedDate desc
You can do select
SELECT TOP 1 * FROM XXX
http://www.w3schools.com/sql/sql_top.asp

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 :)

Equivalents to SQL Server TOP

In SQL Server, TOP may be used to return the first n number of rows in a query. For example, SELECT TOP 100 * FROM users ORDER BY id might be used to return the first 100 people that registered for a site. (This is not necessarily the best way, I am just using it as an example).
My question is - What is the equivalent to TOP in other databases, such as Oracle, MySQL, PostgreSQL, etc? If there is not an equivalent keyword, what workarounds can you recommend to achieve the same result?
To select first 100 rows:
MySQL and PostgreSQL:
SELECT *
FROM Table
ORDER BY
column
LIMIT 100
Oracle:
SELECT *
FROM (
SELECT t.*
FROM table
ORDER BY
column
)
WHERE rownum <= 100
Note that you need a subquery here. If you don't add a subquery, ROWNUM will select first 10 rows in random order and then sort them by column.
To select rows between 100 and 300:
MySQL:
SELECT *
FROM TABLE
ORDER BY
column
LIMIT 100, 200
PostgreSQL:
SELECT *
FROM Table
ORDER BY
column
OFFSET 100 LIMIT 200
Oracle:
SELECT *
FROM (
SELECT t.*, ROW_NUMBER() OVER (ORER BY column) AS rn
FROM table
)
WHERE rn >= 100
AND rownum <= 200
Note that an attempt to simplify it with ROWNUM BETWEEN 100 AND 200 (as opposed to rn BETWEEN 100 AND 200 in the outer query) will return nothing in Oracle!
RN BETWEEN 100 AND 200 will work in Oracle too but is less efficient.
See the article in my blog for performance details:
Oracle: ROW_NUMBER vs ROWNUM
For Postgres and MySQL it's the LIMIT keyword.
SELECT *
FROM users
ORDER BY id
LIMIT 100;
This is standard SQL (Oracle and SQL Server implement it). This is an example of returning up to 100 rows:
SELECT ID_CONTROL FROM (SELECT ROW_NUMBER() OVER (ORDER BY ID_CONTROL)
ROWNUMBER, ID_CONTROL FROM IWS_CONTROL WHERE
CURRENT_STATE = 15 AND CURRENT_STATUS=0) A WHERE ROWNUMBER <= 100)
In SQL Anywhere, it's the same as SQL Server:
SELECT TOP 100 * FROM users ORDER BY id
You can even start in the middle of the result set if you want:
SELECT TOP 100 START AT 50 * FROM users ORDER BY id
gets the 50th through 150th rows of the result set.
LIMIT 100
as in
SELECT * FROM foo ORDER BY bar LIMIT 100
You can use RANK() and DENSE_RANK() in Oracle. Here is a link to AskTom website explaining how to to pagination and top-n queries with DENSE_RANK in Oracle.
Oracle:
select * from (select * from foo ORDER BY bar) where rownum < 100
With a nice explanation on how to make it work in AskTom.
In Ingres the same query would by:
select First 100 * from foo ORDER BY bar
Ingres question was already answered in StackOverflow before.
In DB2 you would make your query look like this:
SELECT * FROM tblData FETCH FIRST 10 ROWS ONLY;
In Oracle you want to use a TOP-N query.
For example:
select *
from (SELECT *
FROM foo
where foo_id=[number]
order by foo_id desc)
where rownum <= 3
This will get you the top three results (because I order by desc in the sub query)