Select last 2 record against record ID in SQL - sql

I want to know if there is a query for selecting last two inserted records against record ID.
For example we can select only 1 top record by using this query:
select max(colName) from tableName
But what query could be for this:
select "Last two records of" colName where id = 1
so if we have 100 records in the table and we have 10 records against id number 1, then we should get the last two inserted records against the id number 1.
Please help me if anybody understood my question.
Note: id is not unique key OR primary key in the table from where I want to get the record.

Script should be something like below-
SELECT TOP 2 *
FROM tableName
WHERE id = 1
ORDER BY colName DESC

what about
select colName from tablename where id in (x,x-1)
Assuming you are using auto increment for the primary key

if you have the column, created_at you can do something like
select * from table where id = 1 order by created_at desc limit 2
that should give you the most recent inserted elements. Are you using postgres, mysql, oracle?

select top (2) * from <myTable> where id = 1 order by id DESC
This will return last 2 inseted rows where Id = 1

Related

Get unique value as per sorting key up to certain count (SQL)

I would like to get 5 unique value as per sorted key.
Below is the table example:-
Expected result is 5,10,3,9,1. First unique 5 value from table.
Thanks.
You can do the following for Oracle:
select * from (
select value
from table
group by value
order by min(serial)
) where rownum <=5
Try this(For MySQL):
SELECT DISTINCT value FROM <table-name> ORDER BY serial LIMIT 5;
For SQL Server
select TOP 5 value from table1
group by value
order by min(Serial)

Select previous record in sqlite

I have this syntax sql who catches the record whose id is equal to what I set:
SELECT * FROM table one WHERE id = '128'
So I select the previous record by id I could just take that number and subtract by 1 (128-1 = 127), but we assume that the record 127 is excluded from the table? In this case he would select an id that does not exist.
How I can change my syntax sql to select previous record?
You can do it like this:
SELECT *
FROM table one
WHERE id < 128
ORDER BY id DESC
LIMIT 1;
Or, if you just want the id:
SELECT MAX(id)
FROM table one
WHERE id < 128;

SQL query exclude rows if having two different values

Basically my select statement returns below:
ID Status
100 1
100 2
101 1
What i'm looking for is to return if a ID having status as 1 and if the same ID has another status ID as 2 then exclude both
In Short results as below:
ID Status
101 1
Thanks in advance !
The following query returns ID values that occur only once.
SELECT ID
FROM t
GROUP BY ID
HAVING COUNT(*) = 1
It should be sufficient for the sample data you provided. If there are other cases then let me know.
SQL Fiddle
You gonna need subquery and NOT IN here.
The following would work if you have column status as INT datatype
SELECT *
FROM table
WHERE status = 1
AND ID NOT IN (
SELECT ID
FROM table
WHERE status = 2
);
Making a generic query, which will remove all duplicated rows, not only for a particular ID :
select ID
from table where ID NOT IN
(select ID from table GROUP BY ID HAVING count(Status) > 1)
/* Subquery will fetch ID's having multiple entries*/
SQL Fiddle
The CTE 'IDs' retrieves all IDs which have single record in DB. This is then joined to original table to return the result as a pair (ID, Status)
;with IDs as
(
select ID
from yourtable
group by ID
having count(*) = 1
)
select i.ID, y.Status
from yourtable y
inner join IDs i on y.ID = i.ID
order by i.ID

Getting the last 30 rows

How can you get the last 30 rows (i.e. the last 30 rows to be inserted) into a database using SQL?
SELECT * FROM table LIMIT x,y ?
An example table:
USER: id (auto increment, int, pk), username, email
SELECT * FROM table ORDER BY id DESC LIMIT 30
you would have to have some key to go with... is there an auto-incrementing column, then order by THAT column descending... Ex:
select * from YourTable order by TheAutoIncrementColumn DESC limit 30

Select records older than latest 100 records from database

My issue here is that i want to populate all the records except latest 100 records in the database. What could be the best approach to accomplish this?
Try using limit cause
SELECT *
FROM Table
order by id desc
limit 101 , totalrecords
Here id is auto increment field of your table
How about something like
SELECT t.*
FROM Table t LEFT JOIN
(
SELECT ID
FROM Table
ORDER BY ID DESC
LIMIT 100
) top100 ON t.ID = top100.ID
WHERE top100.ID IS NULL
Where ID would be the column to identify the order (latest) and Table from where you wish to select
SELECT *
FROM Table
WHERE ID < (SELECT ID
FROM Table
ORDER BY ID DESC
LIMIT 99, 1)