Get unique value as per sorting key up to certain count (SQL) - 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)

Related

Select last 2 record against record ID in 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

select max value and another column

id value
2 20
1 30
3 15
5 25
I have this table and want to get max value and id. When i use select id,max(value) i've got 2,30 but the right answer is 1,30. I really need to get your attention.
Thank you very much
select id, value from `table` order by value desc limit 1
You can try using subquery
select * from tablename
where value in (select max(value) from tablename)
SELECT *
FROM `tablename`
WHERE value=(SELECT
MAX(value) as value
FROM tablename)
You can check this query. Giving result according your requirement.
SELECT top 1 id,max(value) FROM table
GROUP BY id
ORDER BY max(value) desc
Use subquery
select *
from t
where value = (select max(value) from table)
From the #Thorsten comments i noted that 1st query will return all ties of max value.
Or you can use order by with limit if it is mysql
select *
from table
order by value desc
limit 1
And 2nd query will return only the single row of highest value

I would like to select the max field in a sql column but list the rest in order as well

My sql table only has one column and it's "ID"
select MAX(id) from eve.db.dde
it only returns the 1 field (the highest)
How do i return all the fields in order from highest to lowest?
SELECT id FROM eve.db.dde ORDER BY ID DESC

How do i filter a sql table by the TOP 1?

I have a query that is filtered by SO Number. It also has a column that has a unique number generated each time the SO is updated. How can i alter my code so that not only will it be filtered by the SO Number, but also filter by the TOP 1, or highest count of the updated key?
Thank you!
This is on SQL Server. Should have specified earlier
SELECT whatever_you_want
FROM whereever_it_is
WHERE your_criteria
ORDER BY so_number DESC
LIMIT 1
which will give you the "highest" so_number, returning only one record even if there are several with the same value
or
SELECT whatever_you_want
FROM whereever_it_is
WHERE your_criteria
AND so_number == MAX(so_number)
which will give all rows with that maximum value, returning all if there are more than one.
SELECT TOP 1 SONumber
FROM ExampleTable
ORDER BY SONumber DESC
Or
SELECT MAX(SONumber)
FROM ExampleTable

how do i select the last 10 records added?

i am running mysql and i would like to display the last 10 records that were added. what is the select statement for this?
If you have an auto-incrementing ID, you can do this:
SELECT * FROM my_table ORDER BY id DESC LIMIT 10;
If you don't, you'll need some criteria you can order by. An insertion date or something. The LIMIT 10 clause is what you're looking for here.
If you have an autoincrementing ID you can use this:
SELECT *
FROM yourtable
ORDER BY id DESC
LIMIT 10
If you have a column added_datetime that is set to the time of the insert then you can use that instead:
SELECT *
FROM yourtable
ORDER BY added_datetime DESC
LIMIT 10