SQLite - remove row with a lowest id - sqlite

How can I remove a row from a table that has lowest rowid?
Thank you.

DELETE FROM
MyTable
WHERE
Id = (SELECT MIN(Id) FROM MyTable);

In most cases like this, rowid is an indexed column. If that is the case the much faster solution is:
DELETE FROM
tablename
WHERE
rowid= (SELECT rowid FROM tablename order by rowid limit 1)
If rowid is NOT indexed then:
DELETE FROM
tablename
WHERE
rowid= (SELECT MIN(rowid) FROM tablename)
I'd still test the first one as it will often be faster, even if rowid is not indexed.

DELETE FROM
tablename
WHERE
rowid= (SELECT MIN(rowid) FROM tablename)

Related

Get LIMIT value from subquery result

I would like to use the LIMIT option in my query, but the number of expected rows is stored in another table. This is what I have, but it doesn't work:
select * from table1 limit (select limitvalue from table2 where id = 1)
When I only run the subquery, the result is 6, as expected.
I prefer working with a WITH statement if possible, but that didn't work eiter.
Thank you in advance!
You could use a prepared statement to get the limit of queries from the other table because the limit clause does not allow non constant variables as parameter:
PREPARE firstQuery FROM "SELECT * FROM table1 LIMIT ?";
SET #limit = (select limitvalue from table2 where id = 1);
EXECUTE firstQuery USING #limit;
The source of the sql query from another post
You can make use of MariaDB's ROW_NUMBER function in a CTE to count the rows to be output, comparing that against the limitvalue. For example:
WITH rownums AS (
SELECT *,
ROW_NUMBER() OVER () AS rn
FROM table1
)
SELECT *
FROM rownums
WHERE rn <= (SELECT limitvalue FROM table2 WHERE id = 1)
Note Using LIMIT without ORDER BY is not guaranteed to give you the same results every time. You should include an ORDER BY clause in the OVER part of the ROW_NUMBER window function. With the sample data in my demo, you might use something like:
ROW_NUMBER() OVER (ORDER BY mark DESC)
Demo on dbfiddle

How do I add a delete statement to this code?

I need to understand how to add a statement that will delete the results of the following query:
I understand that a DELETE statement with a WHERE clause would normally be used, but because I'm SELECTING two different columns, the where clause doesn't accept the comma. I've not yet been able to figure out how to turn this into a CTE (but maybe that's overkill?) and then call it in a DELETE statement later (assuming that's even an option). Examples always have the DELETE FROM..., or WHICH statements, neither of which seem implementable under this code. Do I have to rewrite my code so it includes a WHICH statement?
SELECT field1, field2
FROM table
GROUP BY field1, field2
HAVING SUM(field3) IS NULL
Expecting to be able to institute a DELETE statement to delete the results of the query.
I believe that you could use (assuming that the table is not a WITHOUT ROWID table) :-
DELETE FROM mytable WHERE rowid IN (SELECT rowid FROM mytable GROUP BY field1, field2 HAVING SUM(field3) IS NULL);
An alternative using a CTE (where the CTE has been given the name deletions) would be :-
WITH deletions(rowid) AS (SELECT rowid
FROM mytable
GROUP BY field1, field2
HAVING SUM(field3) IS NULL
)
DELETE FROM mytable WHERE rowid IN (SELECT rowid FROM deletions);
note that mytable has been used as the table name instead of table.
Considering the comment
Primary key is field1
then :-
DELETE FROM mytable WHERE field1 IN (SELECT field1 FROM mytable GROUP BY field1, field2 HAVING SUM(field3) IS NULL);
could be used, this would then work whether or not the table is defined as a WITHOUT ROWID table, a similar change could be applied to the CTE version.
Notes
Using GROUP BY on a PRIMARY KEY, as it is UNIQUE, will result in as many groups and therefore rows, as there are rows. Effectively the query could be SELECT field1 FROM mytable WHERE field3 IS NULL and therefore the deletion could simply be DELETE FROM mytable WHERE field3 IS NULL.
If this were not the case and field1 was not the PRIMARY KEY, then the complication is that values per group that are not aggregated values are values from an arbitrarily selected row. In short you would delete 1 from a number of the rows that where grouped.

Insert random row to table and then delete

I've got two tables: tableA and tableB. I would like to insert random row from tableA to tableB and then delete this row from tableA. How can i do it? Is it possible at all? Below is my insert code but i dont know how to delete this row.
INSERT INTO tableB
SELECT * FROM tableA ORDER BY RANDOM() LIMIT 1;
You can use rowid:
DELETE from tableA
WHERE elementA = (
SELECT elementA from tableB
WHERE rowid = (SELECT MAX(rowid) FROM tableB)
)
Provided that elementA is unique in both tables, if this is executed after your INSERT statement it will find elementA from the inserted row and delete the row from TableA with that elementA.

How to "update" the _id column in SQLite Database Browser

The _id column in my database is an INTEGER PRIMARY KEY, so it is an auto-incrementing column.
The problem is that now I deleted a row, and the column didn't update the auto-incrementing number.
Is there a way to make the _id column update, so there wouldn't be holes in the sequence?
Thank you very much in advance.
No. This is not how it is intended to be used. Don't mess with the primary key! There will be gapes. The id is just a unique identifier.
If you need a rank then you can do that
select t.*, #rank := #rank + 1 as gapless_rank
from your_table t
cross join (select #rank := 0) r
order by id
To get the nth ID from the table, use a query like this:
SELECT _id
FROM MyTable
ORDER BY _id
LIMIT 1
OFFSET n-1

SQLite: Selecting the maximum corresponding value

I have a table with three columns as follows:
id INTEGER name TEXT value REAL
How can I select the value at the maximum id?
Get the records with the largest IDs first, then stop after the first record:
SELECT * FROM MyTable ORDER BY id DESC LIMIT 1
Just like the mysql, you can use MAX()
e.g. SELECT MAX(id) AS member_id, name, value FROM YOUR_TABLE_NAME
Try this:
SELECT value FROM table WHERE id==(SELECT max(id) FROM table));
If you want to know the query syntax :
String query = "SELECT MAX(id) AS max_id FROM mytable";

Resources