I've been requested within an interview to create a SQL query which brings the very first three records with greatest value on some column from the table. I'd written a question which fetched all of the records with greatest value, but did not get exactly how i'm able to get only first three records of individuals.

Would you assist me to within this.

Thanks.

SELECT TOP 3 * FROM Table ORDER BY FieldName DESC

From here, but may well be a little outdated:

Postgresql:

SELECT * FROM Table ORDER BY FieldName DESC LIMIT 3

MS SQL Server:

SELECT TOP 3 * FROM Table ORDER BY FieldName DESC

mySQL:

SELECT * FROM Table ORDER BY FieldName DESC LIMIT 3

With respect to the database engine, either

select top 3 * from table order by column desc

or

select * from table order by column desc limit 3

Choose Best Three....

The syntax to find the best 3 varies broadly from database to database.

Regrettably, you should utilize individuals constructs for top performance. Libraries like Hibernate help here, simply because they can translate a typical API in to the various SQL dialects.

As you are asking about Java, you'll be able to just Choose from the database (by having an ORDER BY), but simply fetch just the first three rows. For the way the query must be performed this can be adequate (particularly if no sorting around the database needs to happen because of appropriate indexes, for instance whenever you sort by primary key fields).

However in general, you need to opt for an SQL solution.

In oracle you may also use where rownum < 4...

Also on mysql there's a restriction keyword (i believe)