selecting all rows except top row using offset in MySQL - mysql

How can I use offset clause to get all rows except the first row. I am using query like
SELECT * FROM EMPLOYEES ORDER BY SALARY OFFSET 1;
But this is not working in MySQL. what am I doing wrong here?

Sadly in MySQL OFFSET only works together with the LIMIT clause. So you need to use
SELECT * FROM EMPLOYEES ORDER BY SALARY LIMIT 18446744073709551615 OFFSET 1;
or
SELECT * FROM EMPLOYEES ORDER BY SALARY LIMIT 1, 18446744073709551615;
I have chosen that limit number from a different question

MySQL 8.0 can use a window function to do this:
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY SALARY) AS rownum
FROM EMPLOYEES
) AS t
WHERE rownum > 1
ORDER BY SALARY;

You have to use LIMIT in addition to OFFSET
SELECT * FROM EMPLOYEES ORDER BY SALARY LIMIT 9999 OFFSET 1;

Related

How to use multiple order by in a sql query with limit keyword?

I want to get nth highest salary in a table by first limiting result set to n top salaries first and then to reverse the result set and limiting it to 1.
I have used this query which is resulting into an error:-
select *
from salaries
where emp_no=(select * from salaries order by salary desc limit 2) order by salary asc limit 1;
The error states that subquery return more than one row.
If you want second highest value you could use:
SELECT *
FROM salaries
ORDER BY salary DESC
LIMIT 1,1;
db<>fiddle demo
Here's one way:
SELECT s.* FROM
(SELECT * FROM `salaries` ORDER BY salary DESC LIMIT 2) s
ORDER BY s.salary ASC LIMIT 1;
Never use SELECT *, see http://www.parseerror.com/blog/select-*-is-evil
select * from salaries
order by salary
desc limit 1 offset 4;
https://www.db-fiddle.com/f/2itHSFs2enyNpJ3MK6Nxcz/0

How to find second and third highest salary in mysql without using sub query

I know this query but it is sub query can anyone explain me to confused i asked this quation in interview but i can't explain proper
select * from dept e where 2 =(select count(distinct salary) from dept where e.salary<=salary)
Try this its very simple:
SELECT id, dept_name, DISTINCT salary FROM dept_table DESC salary LIMIT 2 OFFSET 2
You can use SQL below to get data.
select *
from dept
where salary < max(salary)
order by salary desc
limit 2;
You can do it without limit using a temporary count column.
SELECT
(#cnt := #cnt + 1) AS rowNumber, *
FROM dept AS t
CROSS JOIN (SELECT #cnt := 0) AS dummy
ORDER BY t.salary DESC ;
you can refer to this example
You can order by salary and get only top 3 items which would represent highest, second highest and third highest salary like
select *
from dept
order by salary desc
limit 3;
You can use sub query as well as using Top keyword.
Using Top
SELECT TOP 2 salary
FROM
(SELECT TOP 3 salary
FROM Table_Name
ORDER BY salary DESC) AS a
ORDER BY salary ASC

querying in MYSQL according to row number

If there is a way to query according to row number ? Like:
select rownumber from table;
what i am trying to do:
I have computed the total number of rows in a table using sub queries and now I want to display only the last one.
rookie answer:
I wanted to get the 2nd highest salary. so this is what i did:
select * from gg where salary< (select max(salary) from gg) order by salary desc limit 1
This doesnot work for Nth highest salary its for 2nd highest only. AND I AM NEW TO THIS SO I JUST WANTED TO GET THE 2ND HIGHEST BY MY OWN METHOD.
Although I'm not sure why you would want to get a specific rownumer, you could do it like this:
select * from table LIMIT rownumer, 1;
Read more on this on http://dev.mysql.com/doc/refman/5.0/en/select.html
So if you want the 3rd row you'll do:
SELECT * FROM table LIMIT 3,1;
If you want the 6th-16th row you'll use:
SELECT * FROM table LIMIT 6,10
Do understand that this is only usefull to get a random row or when you add SORT BY. Otherwise you will never be sure what row will be returned...
If you want to select records with row number see this:
SELECT (#rank := #rank + 1) AS row_number
,contract_id, price1, price2, price3
FROM t1, (SELECT #rank := 0) tmp;
If you want only row number see this:
SELECT (#rank := #rank + 1) AS row_number
FROM t1, (SELECT #rank := 0) tmp
See this SQLFiddle
If you want to just limit your query to return only a specific number of rows you can use the Mysql built in LIMIT row_count OFFSET offset clause. But, If you want to select a ranking number like a row number use:
SET #rownum = 0;
Select *
FROM
(
Select *, (#rownum := #rownum+1) as rank
FROM t
) sub
Update: If you want to
I want to display only the last one.
Then you can do it this way:
SELECT *
FROM Yourquery
ORDER BY SomeField DESC --This how you determine the last one based on the order
LIMIT 1;
select * from table limit [offset,limit]
offset is the row number which you want to start,
limit is the count of rows you want to get starting from offset
so you can use it like
select * from table limit rownumber,1
There's no row number without ordering, but if you do have a sort order in mind then
SELECT * FROM thing ORDER BY ordering LIMIT 1 OFFSET your_row;

Select last N rows from MySQL

I want to select last 50 rows from MySQL database within column named id which is primary key. Goal is that the rows should be sorted by id in ASC order, that’s why this query isn’t working
SELECT
*
FROM
`table`
ORDER BY id DESC
LIMIT 50;
Also it’s remarkable that rows could be manipulated (deleted) and that’s why following query isn’t working either
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
Question: How is it possible to retrieve last N rows from MySQL database that can be manipulated and be in ASC order ?
You can do it with a sub-query:
SELECT * FROM
(
SELECT * FROM table ORDER BY id DESC LIMIT 50
) AS sub
ORDER BY id ASC;
This will select the last 50 rows from table, and then order them in ascending order.
SELECT * FROM table ORDER BY id DESC LIMIT 50
save resources make one query, there is no need to make nested queries
SELECT * FROM table ORDER BY id DESC, datechat DESC LIMIT 50
If you have a date field that is storing the date (and time) on which the chat was sent or any field that is filled with incrementally (order by DESC) or de-incrementally (order by ASC) data per row put it as second column on which the data should be ordered.
That's what worked for me!!!! Hope it will help!!!!
Use it to retrieve last n rows from mysql
Select * from tbl order by id desc limit 10;
use limit according to N value.
if anyone need this
you can change this into
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
into
SELECT
*
FROM
`table`
WHERE
id > (SELECT MAX(id)- 50 FROM chat)
ORDER BY id ASC;
select * from Table ORDER BY id LIMIT 30
Notes:
* id should be unique.
* You can control the numbers of rows returned by replacing the 30 in the query

Fetching top 5 records of maximum salary

I have 3 columns with id,usrnameand salary. I want to find maximum salary of 5 records.
How will i write query in mysql ?
In MySQL you can use ORDER BY to sort the rows in descending order and use LIMIT to return only the top 5 rows:
SELECT id, usrname, salary
FROM yourtable
ORDER BY salary DESC
LIMIT 5
You have to use LIMIT, like so:
SELECT * FROM mytable ORDER BY salary DESC LIMIT 5