how do i select the last 10 records added? - sql

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

Related

Sqlite get max id not working (?)

Im using this:
SELECT *
WHERE id=MAX(id)
FROM history;
But my query is empty. I have also tried this (This one works):
SELECT MAX(id) AS max_id
FROM history;
But obviusly my query only contains the max_id key. What am I doing wrong with the first one?
You need to add another level of select for the MAX, like this:
SELECT *
WHERE id=(SELECT MAX(id) from history)
FROM history;
A better approach would be to order by id in descending order, and limit the output to a single row:
SELECT *
FROM history
ORDER BY id DESC
LIMIT 1
Actually most simple query to get the max value of a column in sqlite is:
select MAX(id) from table;
SELECT top 1
FROM history
ORDER BY id DESC

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

getting list of maximum values in sql

I am working with mysql. I need queries that fetch a list of maximum values.
I know how to get one maximum value i.e using max(colName) in select query.
Is there some similar query that can fetch more than 1 maximum values like the top 10 values.
select * from table order by colName desc limit 10;
SELECT * FROM table
ORDER by price DESC
LIMIT 10
try
select distinct colName from tablename order by colName desc limit 10;

How to select the last three rows of a table in ascending order?

I want to select the last three rows of a table in ascending order. What is the query for that?
SELECT * FROM table ORDER BY DESC LIMIT 3
SELECT *
FROM (
SELECT *
FROM tblname
ORDER BY rowid DESC
) WHERE rownum < 4;
Rowid is the physical address of every row.
Rownum is the psuedo column which is used to generate sequence number of rows.

How to select a random record from a MySQL database?

I am using the following query to select 1 random record -
SELECT name FROM table WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM table ) ORDER BY id LIMIT 1
but it gives me the same set of records every time I call it. How do I get better random record?
Try this:
SELECT * FROM tableName ORDER BY RAND() LIMIT 1