Get first record by latest date - sql

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

Related

how to remove subquery from sql involving max()

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

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

SELECT TOP 1 result from table ordered by field

I have a table like this :
id package start_date option
1 342323 2012-02-02 1
2 3423333 2012-02-06 1
3 552234 2012-02-14 2
I want to select only the (top 1) row that has the minimum start_date.
I tried where start_date = MIN(start_date) but it's not working.
I'm using SQL Server 2008.
select top 1 * from table
order by start_date
try:
where start_date = (select min(start_date) from yourtable)
SELECT * FROM myTable
ORDER BY start_date ASC
LIMIT 1
SELECT * from Yourtable
WHERE start_date = (SELECT (Min(yt1.Start_Date) FROM YourTable as yt1)
For MSSQL
select top 1 * from your_table order by start_date asc
For Sql server
select top 1 * from tbl_name order by start_date
For Mysql
select * from tbl_name order by start_date asc limit 1
You could also use:
;With top_record as
(
SELECt *
,row_number() OVER (order by start_date ASC) as row
FROM yourtable
)
SELECT * from top_record WHERE row = 1
This would then give you the flexibility to determine what to do with ties by using DENSE_RANK / RANK instead of ROW_NUMBER()
This SQL Server-specific query should get you the data you want. Try using this:
select top 1 * from tbl_name order by start_date asc

How to return second newest record in SQL?

If this:
SELECT *
FROM Table
WHERE Date=( SELECT MAX(Date)
FROM Table
)
returns newest record from the table, how to get second newest record?
SELECT *
FROM Table
WHERE Date = ( SELECT MAX(Date)
FROM Table
WHERE Date < ( SELECT MAX(Date)
FROM Table
)
) ;
or:
SELECT TOP (1) *
FROM Table
WHERE Date < ( SELECT MAX(Date)
FROM Table
)
ORDER BY Date DESC ;
or:
SELECT *
FROM
( SELECT t.*
, ROW_NUMBER() OVER(ORDER BY Date DESC) AS RowNumber
FROM Table t
) AS tmp
WHERE RowNumber = 2 ;
If the Date column has unique values, all three queries will give the same result. If the column can have duplicate dates, then they may give different results (when there are ties in 1st or 2nd place). The first query will even give multiple rows in the result if there are ties in 2nd place.
"select TOP (1) *
from Table
WHERE Date<(SELECT MAX(Date) FROM Table)
ORDER BY Date DESC"
should do the trick.
Please check this code.
SELECT * FROM category
WHERE Created_Time <(
SELECT MAX(Created_Time)
FROM category
)
ORDER BY Created_Time DESC
LIMIT 1
Prasad.
select second last date in sql:
SELECT MAX(YourDateColumn) FROM YourTable where ColumnId=2 and YourDateColumn <
(SELECT MAX(YourDateColumn) FROM YourTable where ColumnId=2)
hope helps someone.
Try this
SELECT *
FROM Table
WHERE Date = ( SELECT MAX(Date) FROM Table
WHERE Date < ( SELECT MAX(Date) FROM Table)
) ;

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