We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

SQL select row with highest autonumber

Medium Priority
595 Views
Last Modified: 2012-05-11
experts.

I have a table with an autoincrement field (ID) and other fields (EMAIL).

please tell me how to select an EMAIL with the highest ID (newest record).

Thanks
Comment
Watch Question

Greg AlexanderLead Developer

Commented:
SELECT MAX(ID) FROM your_table
Greg AlexanderLead Developer

Commented:
to get the email you would


SELECT MAX(ID),EMAIL FROM your_table
Walter RitzelSenior Software Engineer
CERTIFIED EXPERT

Commented:
select email from table where id = (select max(id) from table)
Daniel ReynoldsSoftware Applications Developer / Integrator
CERTIFIED EXPERT

Commented:
Select * FROM your_table where ID = (SELECT Max(ID) from your_table)
Greg AlexanderLead Developer

Commented:
Yeah, I was wrong, sorry wpcortes has the answer I believe
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
or this:
select t.*
  from yourtable
 order by ID desc
 LIMIT 1

Open in new window

Travis HydzikVarious
CERTIFIED EXPERT

Author

Commented:
sorry, guys for not being clear, I need to have a where condition for EMAIL, ie `EMAIL`='a@b.com'

so it needs to first select the email, and then the newest (highest ID) row.
Greg AlexanderLead Developer

Commented:

Try

SELECT *,MAX(ID) as max_id FROM your_table WHERE email = 'a@b.com';
Senior Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Travis HydzikVarious
CERTIFIED EXPERT

Author

Commented:
thanks!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.