Getting User with Highest Amount - mysql

I have a table full of users and, using the below query, I'm able to get the result of the highest score divided by points:
SELECT MAX(points/score) FROM table
However, I'd like to grab the user associated with the result; any suggestions?

SELECT *
FROM table
ORDER BY points/score DESC
LIMIT 1

just order by points/score and limit the result to one:
SELECT
*
FROM
table
ORDER BY
points/score DESC
LIMIT 1

Related

Mysql select rows except first row

I have issue there with select from table. I want to select all rows except for first row. So .. There is my code
SELECT * FROM table ORDER BY id DESC
So this code select and order id's from table which give me id feedback "5>4>3>2>1". And there is issue .. How I can select and echo just 4>3>2>1 rows.
So if I had rows with id's 1,2,6,8,10 , echo will be 10,8,6,2,1 and I want select to echo just 8,6,2,1.
There is my full wrong code for select.
$other = mysql_query("SELECT * FROM table ORDER BY id DESC LIMIT 1, 1");
This should do it.
SELECT * FROM table WHERE id NOT IN (SELECT MAX(id) FROM table) ORDER BY id DESC
Try this:
SELECT *
FROM
(
SELECT *, row_number()
OVER (ORDER BY id DESC) row
FROM table
)
WHERE row != 1
It gives numbers to your selected rows and takes all of them without the one with row number 1
Try this
$other = mysql_query("SELECT * FROM table ORDER BY id DESC OFFSET 1");
THE ABOVE QUERY WONT WORK AS A LIMIT IS NEEDED
Refer to this answer
All you need is offset 1, but offset cannot be used without limit. So, I'd suggest something like:
SELECT * FROM table ORDER BY id DESC LIMIT 99999999 OFFSET 1
Warning: make sure your table doesn't contain lots of records, otherwise you will run into performance issues. Or, change the limit to something reasonable, like 10.
EDIT:
Read: How to use offset without limit
SELECT *
FROM table
WHERE id NOT IN ( SELECT id
FROM table
ORDER BY id DESC
LIMIT 1 )
ORDER BY id DESC;
You can try this.
In this case I am selecting all the rows, except the one with the biggest id.
In the example of [1,2,3,4,5], it will be :
SELECT *
FROM table
WHERE id NOT IN ( 5 )
ORDER BY id DESC;
Hope this helps!

how to orderby after applying limit in sql

I need to get the information from DB in such a way, the limit should be 3 rows and out of which i want to sort by descending order.
I used
select * from table where coloumn = 'Myfilter' order by serialNumber desc limit 3
after the execution I am not getting the latest three records rather the first three records ordered by descending.
Applying limit before order by
SELECT * FROM (SELECT * FROM table WHERE coloumn = 'Myfilter' ORDER BY serialNumber LIMIT 3) a ORDER BY serialNumber DESC
This query solves my question thank you all for suggestions,
SELECT * FROM (SELECT * FROM table WHERE coloumn='myFilter' ORDER BY serialnumber desc LIMIT 3) a ORDER BY serialnumber asc
the query uses to select the latest 3 rows ordered by big to small serial number then again the selected rows order where reversed, thnx #Kelvin Barsana
"SELECT * FROM table WHERE coloumn = 'Myfilter' ORDER BY serialNumber DESC LIMIT 3";

How to get Max of ID without use of MAX()

I want to get the maximum id from table with ought use of any function like max or else.I need simple pure query.Any one help me to solve this problem .
We simply write
select max(id) from table
but i don't want to use max()
Use ORDER BY and LIMIT
SELECT id
FROM table
ORDER BY id DESC
LIMIT 1
ORDER BY with LIMIT will do the job for u just fine
SELECT id FROM table
ORDER BY id DESC LIMIT 1;
But as you asked the question from interview's point of view , they may even ask you to do the same without using LIMIT , TOP or max() .
In thay case you should go with subquery approach . Here' s how u should do it :
SELECT id FROM table
WHERE id >= ALL
(SELECT id FROM table)
In this query an id is matched with all the id's in the table and it will be printed only if the value is greater than or equal to all the id's in the table. Only the max will satisfy the condition.
Use ORDER BY clause with LIMIT to fetch latest ID of table
Try this:
SELECT id
FROM table
ORDER BY id DESC
LIMIT 1;
SELECT id FROM table ORDER BY id DESC LIMIT 1
This should do it
I added 0 to solve the problem,
select max(id + 0) from table

How to get data from mysql db from nth row to nth row?

I am using this query but it is not working in mysql
SELECT TOP 1 * FROM (SELECT TOP 5 * FROM ads ORDER BY id DESC) ads ORDER BY id DESC
You can use ORDER BY ... LIMIT {[offset,] row_count | row_count OFFSET offset} (lets say you want to get 5 records from 10th - 10,11,12,13,14):
SELECT * FROM ads
ORDER BY id DESC
LIMIT 10,5
Although I assume you don't want to get them sorted by id but rather by views or similar criteria where ORDER BY views DESC would take a place (don't forget to to add index on views count).
The 'TOP' does not function on MySQL. You can edit the query in the following manner to get the job done
SELECT * FROM (SELECT * FROM ads ORDER BY id DESC LIMIT 5) ads ORDER BY id DESC LIMIT 5

Mysql - return last 3 results in table

i was wondering if there was an easy way with just an sql statement to return the last three results in the table but in that order i.e. if there are a hundered results it would return in the order of 98, 99, 100 not simply ordering by id DESC and limit 3 which would return in order 100, 99, 98
Any help much appreciated.
p.s. in this instance, lets say I don't know the amount of results and don't really want to send 2 sql requests just to find the amount ( for any OFFSET answers ).
One way would be to use DESC and then just sort them again:
SELECT * FROM (SELECT * FROM some_table ORDER BY id DESC LIMIT 3) a ORDER BY id
Two options, I guess.
You could use DESC to return them in reverse order as you stated, and just reverse the order again in your application code. This is potentially the most efficient, as you can do it in a single query and it can potentially only need to read three rows of an index.
You can first find out the number of results in the table, then do a LIMIT <results-3>, 3
Is it okay if you just flip it back to the original order?
SELECT * FROM (SELECT * FROM SOMETABLE ORDER BY ID DESC LIMIT 3) AS T ORDER BY ID;
Select *
FROM (SELECT * FROM yourTABLE ORDER BY ID DESC LIMIT 0,3) as TempTable ORDER BY ID