how to remove subquery from sql involving max() - sql

I have a query like this
select * from tablename where ID in(select max(ID) from tablename )
I want to remove subquery to improve performace. Please suggest me,How to do it
Thank you in advanced

You don't mention your RDBMS, but in MS-SQL:
SELECT TOP 1 *
FROM tablename
ORDER BY ID DESC

If you are using MS SQL Server; you can achieve it like:
SELECT TOP 1 *
FROM tablename
ORDER BY ID DESC
In MYSQL You can use :
SELECT * from tablename
ORDER BY ID DESC LIMIT 1;

for Oracle :
SELECT *FROM
(
SELECT * FROM tablename ORDER BY ID desc
)
WHERE rownum <= 1 ORDER BY ID ;
For MS-SQL :
SELECT TOP 1 * FROM tablename ORDER BY ID DESC
For MY SQL :
select * from tablename order by ID desc limit 1

Related

Get first record by latest date

Please help complete this sql string? I want to get 1st record from table 1 where createddate = most recent/latest.
Select TOP 1 * From Table1 Where CreateDate = "latest date??"
the other way to do it:
select * From Table1 order by CreateDate DESC Limit 1
Or without TOP, using subquery:
Select *
From Table1
Where CreateDate = (select max(CreateDate) from Table1)
;
use order by
Select TOP 1 * From Table1 order by createddate desc

How can I identify which record has the MAX for a particular field?

The title is pretty much self explanatory. I want to find out a way to identify which record the MAX of a field is in.
Try this with MySQL or Postgres:
SELECT * FROM MyTable ORDER BY MyField DESC LIMIT 1
Or this with MSSQL:
SELECT TOP 1 * FROM MyTable ORDER BY MyField DESC
Oracle requires a nested SELECT with ROWNUM:
SELECT * FROM (SELECT * FROM MyTable ORDER BY MyField DESC) WHERE ROWNUM = 1
The answer will depend on the DBMS that you're using. #StilesCrisis covered MySQL and MSSQL; in Oracle you might do the following:
SELECT * FROM (
SELECT * FROM mytable ORDER BY myfield DESC
) WHERE rownum = 1;
Postgres uses the same syntax as MySQL:
SELECT * FROM mytable ORDER BY myfield DESC LIMIT 1;

get id value when max is on other column

is this the best way to get the id value of the most recent date?
table1
id,entrydate
1,8/23/2012
2,8/24/2012
3,8/23/2012
select id from table1 where entrydate = ( select MAX(entrydate) from table1 )
Assuming you're using SQL-Server, you can use ORDER BY and then take one row:
SELECT TOP 1 id
FROM table
ORDER BY entrydate DESC
In MySql it is LIMIT:
SELECT id
FROM table
ORDER BY entrydate DESC
LIMIT 1
In Oracle:
SELECT id
FROM (SELECT id FROM table ORDER BY entrydate DESC)
WHERE ROWNUM = 1
You already have a good way there. I'd watch out for ties:
select top id from table1 where entrydate = ( select MAX(entrydate) from table1 )
This, of course, assuming you are using SQL Server.
SELECT id FROM table1 ORDER BY entrydate DESC LIMIT 1
You should be able to do SELECT id FROM table1 ORDER BY entrydate DESC LIMIT 1
Not exactly, you want to do this:
For SQL Server:
SELECT TOP 1 id, MAX(entrydate) FROM table1 GROUP BY id
For MySQL:
SELECT id, MAX(entrydate) FROM table1 GROUP BY id LIMIT 1

Simple SQL query

I have a table, with these columns:
ID | Data
How to find out which record has highest ID?
To get the largest ID:
select max(ID) from myTable
To get a record that has the largest ID:
select *
from MyTable
where ID = (Select max(ID) from myTable)
select *
from YourTable
where ID = (select max(ID) from YourTable)
select max(ID) from tablename
As well as max, you can use TOP on SQL Server
select TOP 1 * from myTable order by id desc
For joint top
select TOP 1 WITH TIES * from myTable order by id desc
Other engines have LIMIT not TOP. This can give the whol record without a separate MAX sub-query too

Select top and bottom rows

I'm using SQL Server 2005 and I'm trying to achieve something like this:
I want to get the first x rows and the last x rows in the same select statement.
SELECT TOP(5) BOTTOM(5)
Of course BOTTOM does not exist, so I need another solution. I believe there is an easy and elegant solution that I'm not getting. Doing the select again with GROUP BY DESC is not an option.
Using a union is the only thing I can think of to accomplish this
select * from (select top(5) * from logins order by USERNAME ASC) a
union
select * from (select top(5) * from logins order by USERNAME DESC) b
Check the link
SQL SERVER – How to Retrieve TOP and BOTTOM Rows Together using T-SQL
Did you try to using rownumber?
SELECT *
FROM
(SELECT *, ROW_NUMBER() OVER (Order BY columnName) as TopFive
,ROW_NUMBER() OVER (Order BY columnName Desc) as BottomFive
FROM Table
)
WHERE TopFive <=5 or BottomFive <=5
http://www.sqlservercurry.com/2009/02/select-top-n-and-bottom-n-rows-using.html
I think you've two main options:
SELECT TOP 5 ...
FROM ...
ORDER BY ... ASC
UNION
SELECT TOP 5 ...
FROM ...
ORDER BY ... DESC
Or, if you know how many items there are in the table:
SELECT ...
FROM (
SELECT ..., ROW_NUMBER() OVER (ORDER BY ... ASC) AS intRow
FROM ...
) AS T
WHERE intRow BETWEEN 1 AND 5 OR intRow BETWEEN #Number - 5 AND #Number
Is it an option for you to use a union?
E.g.
select top 5 ... order by {specify columns asc}
union
select top 5 ... order by {specify columns desc}
i guess you have to do it using subquery only
select * from table where id in (
(SELECT id ORDER BY columnName LIMIT 5) OR
(SELECT id ORDER BY columnName DESC LIMIT 5)
)
select * from table where id in (
(SELECT TOP(5) id ORDER BY columnName) OR
(SELECT TOP(5) id ORDER BY columnName DESC)
)
EDITED
select * from table where id in (
(SELECT TOP 5 id ORDER BY columnName) OR
(SELECT TOP 5 id ORDER BY columnName DESC)
)
No real difference between this and the union that I'm aware of, but technically it is a single query.
select t.*
from table t
where t.id in (select top 5 t2.id from table t2 order by MyColumn)
or
t.id in (select top 5 t2.id from table t2 order by MyColumn desc);
SELECT *
FROM (
SELECT x, rank() over (order by x asc) as rown
FROM table
) temp
where temp.rown = 1
or temp.rown = (select count(x) from table)
Then you are out - doing the select again IS the only option, unless you want to pull in the complete result set and then throwing away everything in between.
ANY sql I cna think of is the same way - for the bottom you need to know first either how many items you have (materialize everything or use count(*)) or a reverse sort order.
Sorry if that does not suit you, but at the end.... reality does not care, and I do not see any other way to do that.
I had to do this recently for a very large stored procedure; if your query is quite large, and you want to minimize the amount of queries you could declare a #tempTable, insert into that #tempTable then query from that #tempTable,
DECLARE #tempTable TABLE ( columns.. )
INSERT INTO #tempTable
VALUES ( SELECT.. your query here ..)
SELECT TOP(5) columns FROM #tempTable ORDER BY column ASC -- returns first to last
SELECT TOP(5) columns FROM #tempTable ORDER BY column DESC -- returns last to first