SQL - How to find the highest number in a column?

mysql, sql, sql-server

Solution

You can do

SELECT MAX(ID) FROM Customers;

Problem

Let's say I have the following data in the Customers table: (nothing more) ``` ID FirstName LastName ------------------------------- 20 John Mackenzie 21 Ted Green 22 Marcy Nate ``` What sort of `SELECT` statement can get me the number 22, in the ID column? I need to do something like this to generate a unique ID. Sure I can let the system do this via auto-increment, but then how would I get the auto generated ID? I thought of `SELECT ID FROM Customers` and counting the rows returned but this seems horribly inefficient, and in this case, it will incorrectly return "3", though I need a unique ID of 23.

Original source