Sort order for sqlite index - sqlite

There is sort order when creating index for sqlite.
https://sqlite.org/lang_createindex.html
Each column name or expression can be followed by one of the "ASC" or "DESC" keywords to indicate sort order.
So, there are three options: no-sort, ASC, DESC when creating index. Where is info about how I should use them? I cant find it.
I guess, if I use ASC or DESC in queries, I should add them to index. But should I add them both if they are both in different queries? Or should I just don't set sort order and it will select it itself?
What is a general rule for it?

SQLite can scan an index in both directions. For an index on a single column, this means it can be used to sort in both ASC and DESC order.
Reference:
The Idx1 index is scanned from top to bottom (or from bottom to top if "ORDER BY fruit DESC" is used) in order to find the rowids for each item in order by fruit.
But for an index on more than one column, there are more than two directions that one might wish to sort the data. For instance, on a two column index, one might wish to sort via ASC, ASC; ASC, DESC; DESC, DESC; or DESC, ASC order.
If the index was created in ASC, ASC order, then sorting by ASC, ASC or DESC, DESC will be able to fully use the index. But sorting by ASC, DESC and DESC, ASC will not be possible solely using the index. That is why the index order may be specified.
For more info: https://use-the-index-luke.com/sql/sorting-grouping/order-by-asc-desc-nulls-last

SQLite can step through an index in both directions, so in general, the index order does not matter.

Related

Using an SQLite SELECT with ORDER BY and OFFSET and LIMIT for fast display, BUT HOW do I find the OFFSET of a row WHERE name = x?

Instead of downloading a whole table, we use OFFSET and LIMIT to fetch rows as needed, so we can quickly display large tables. We can click columns to change SORT BY. When we start, we want to display the row of the last-selected customer using the previous sort string, so we need to find the OFFSET value, which may have changed.
Obviously I can run the SELECT without OFFSET and LIMIT, counting rows until I find name = x.
BUT is there some faster way that sqlite can directly tell me what OFFSET to use on that sorted SELECT for customer x ?
Use ROW_NUMBER() window function:
SELECT rn
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY columnname) rn
FROM tablename
)
WHERE name = 'x';
Change columnname to the name of the column that you use to sort the table and maybe add DESC to sort descending.

Retrieve sorted data based on one numeric column in SQLITE

I have a table like attached image in SQLITE database.
I am trying to retrieve sorted data based on SUM(freight) column. For this i have used the below query.
SELECT ShipCountry
FROM CountryDetails
GROUP BY ShipCountry
ORDER BY SUM(freight) ASC
When i run this i am getting result like below.
If i run the below query i am getting result like below. It's fine.
SELECT ShipCountry, ShipCity
FROM CountryDetails
GROUP BY ShipCountry, ShipCity
ORDER BY SUM(Freight), ShipCity ASC
Instead of this i need a result like below. In order by clause SUM(Freight) should consider only ShipCountry. It should not consider both ShipCountry and ShipCity. My Expected result is
How to achieve this result through SQLITE query?
in SQL we can achieve like below query.
Select ShipCountry, ShipCity from Countrydetails group by ShipCountry, ShipCity Order by SUM(SUM(freight)) over(partition by ShipCountry), Shipcity Asc.
We need equivalent query like this in Sqlite.
Use a subquery to find the frieght sum for each country, then order using only this country level sum:
SELECT
cd1.ShipCountry,
cd1.ShipCity
FROM Countrydetails cd1
INNER JOIN
(
SELECT ShipCountry, SUM(freight) AS freight_sum
FROM Countrydetails
GROUP BY ShipCountry
) cd2
ON cd1.ShipCountry = cd2.ShipCountry
ORDER BY
cd1.freight_sum,
cd1.ShipCountry, -- needed in case two countries happen to have the same sum
cd1.ShipCity
We could add another sort level for the frieght of each city, to order within a given country. But your expected output does not imply that you want this.

Getting the nth highest value row in sqlite

My problem sounds simple enough but I havent been able to find a solution that works.
I need to get the row with say 5th highest value of an attribute with sqlite.. Entries are random of course. So its not sorted.
I did find a few solutions with sql but apparently not all sql functionalities are supported by sqlite.
Thanks in advance
To remove duplicates, use DISTINCT.
To get only the fifth value, use the OFFSET clause:
SELECT DISTINCT SomeAttribute
FROM MyTable
ORDER BY 1 DESC
LIMIT 1 OFFSET 4
Try the 'Limit' keyword.
The query below will eliminate the first four rows by using 'not in'.
Then the last 'Limit 1' will select the 5th row.
Select * from Programs
Where ProgramId not in
(Select ProgramId From Programs order by programId limit 4 )
order by programId Limit 1
EDIT:
To add in CV's 'Distinct' and 'OFFSET' suggestions, the finished query would look something like...
Select StudentName From Students Where Marks in (
Select distinct Marks from Students Order By Marks desc Limit 1 offset 4)

How to read the last record in SQLite table?

Is there a way to read the value of the last record inserted in an SQLite table without going through the previous records ?
I ask this question for performance reasons.
There is a function named sqlite3_last_insert_rowid() which will return the integer key for the most recent insert operation. http://www.sqlite.org/c3ref/last_insert_rowid.html
This only helps if you know the last insert happened on the table you care about.
If you need the last row on a table, regardless of wehter the last insert was on this table or not, you will have to use a SQL query
SELECT * FROM mytable WHERE ROWID IN ( SELECT max( ROWID ) FROM mytable );
When you sort the records by ID, in reverse order, the last record will be returned first.
(Because of the implicit index on the autoincrementing column, this is efficient.)
If you aren't interested in any other records, use LIMIT:
SELECT *
FROM MyTable
ORDER BY _id DESC
LIMIT 1

How can we fetch top 3 data value from database

There is three field in my data base id(primary key),name, salary
I want fetch top 3 salary from the database.
Use LIMIT to get top 3 after ordering, as such:
SELECT *
FROM myTable
ORDER BY salary DESC
LIMIT 3;
SELECT * FROM your_table ORDER BY id DESC;
SELECT [column(s)]
FROM [table]
ORDER BY [column(s)] [ASC, DESC];
For more information check here:
http://www.sqlite.org/lang_select.html
SQL has an ORDER BY clause that allows you do order the result set by any column/columns, ascending and descending.
For your particular question:
SELECT Id, Name
FROM myTable
ORDER BY Id DESC;
See this SO question (SQLite - sorting a table).

Resources