Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

What SQL would Spring Data JPA generate for List<User> findTop10ByStatusAndCreatedAtAfterOrderByCreatedAtDesc(Status s, Instant t), and what happens if you misspell a property name?

Spring Data parses the method name into a query equivalent to select ... from users where status = ? and created_at > ? order by created_at desc, limited to 10 rows (the exact limit syntax depends on the database dialect). Keywords such as And, Or, After, Between, In, Like, IgnoreCase, OrderBy and Top or First are read from the name, and a misspelled property makes the application fail at startup with a PropertyReferenceException, so the mistake is caught early. Beyond two or three conditions the name becomes hard to read, and @Query is the better choice.

Why this question is asked

Derived query methods are the first piece of Spring Data magic most of us use: we type a method name and a working query appears. Interviewers ask about one specific name to check that we can read it the way Spring Data does and predict the SQL, which is what we need when a query is slow or returns the wrong rows. The misspelling half of the question checks whether we know when the mistake is caught. In this answer, we will recap derived queries, take the method name apart piece by piece, look at the SQL Hibernate generates, see what a misspelling does, and discuss when to stop using long names.

1A Quick Recap of Derived Query Methods

A derived query method is a repository method whose name describes the query. Spring Data reads the name when the application starts, using a parser that splits it into a subject (what to return, such as find, count, exists or delete, plus options like Top10 or Distinct) and a predicate (the conditions after By). Each condition names a property of the entity, optionally followed by a keyword such as After or Like, and the method parameters are bound to the conditions in order.

Because the name refers to entity properties, not column names, we write createdAt, the Java field, and Hibernate maps it to the created_at column.

2Taking the Name Apart

Let us split findTop10ByStatusAndCreatedAtAfterOrderByCreatedAtDesc the way the parser does. find is the subject: return entities. Top10 limits the result to the first ten rows; First10 would mean the same. By starts the predicate. StatusAndCreatedAtAfter is two conditions joined by And: Status with no keyword means equals, and CreatedAtAfter means createdAt greater than the parameter. OrderByCreatedAtDesc adds the sort. The two parameters, Status s and Instant t, bind to the two conditions in that order.

How the parser reads the method name
Part of the nameMeaningBecomes
findReturn entitiesselect u from User u
Top10Limit the resultfetch first 10 rows (dialect specific)
ByStart of conditionswhere
StatusEquality on status, first parameteru.status = ?1
AndJoin conditionsand
CreatedAtAfterGreater than, second parameteru.createdAt > ?2
OrderByCreatedAtDescSortorder by u.createdAt desc
Property names are entity fields; Hibernate maps them to columns.

3The Query and the SQL It Produces

Here is the entity, the repository method and what we see in the SQL log on PostgreSQL. Note that User is a reserved word in many databases, so the entity is mapped to a table called users.

Java
@Entity @Table(name = "users")
class User {
@Id @GeneratedValue Long id;
@Enumerated(EnumType.STRING) Status status;
Instant createdAt;
String email;
}
interface UserRepository extends JpaRepository<User, Long> {
List<User> findTop10ByStatusAndCreatedAtAfterOrderByCreatedAtDesc(Status s, Instant t);
}
userRepository.findTop10ByStatusAndCreatedAtAfterOrderByCreatedAtDesc(Status.ACTIVE, weekAgo);
// Equivalent JPQL:
// select u from User u where u.status = ?1 and u.createdAt > ?2 order by u.createdAt desc
// SQL on PostgreSQL (Hibernate 6):
// select u.id, u.created_at, u.email, u.status from users u
// where u.status = ? and u.created_at > ? order by u.created_at desc fetch first ? rows only
// -> at most 10 users, newest first

Notice that the limit is not part of the JPQL. Spring Data applies it by setting the maximum number of results on the query, and Hibernate renders that in the dialect's syntax: fetch first ? rows only or limit ? on PostgreSQL depending on version, limit ? on MySQL, and fetch first or a rownum filter on Oracle. That is why we say limited to 10 rows rather than quoting one syntax.

For this query to be fast on a large table, an index on (status, created_at) lets the database find the matching rows already in order and stop after ten.

4Other Keywords Worth Knowing

The same parser understands many more keywords, and they combine freely. Or joins conditions with or. Between takes two parameters, findByCreatedAtBetween(from, to). In takes a collection, findByStatusIn(List<Status>). Like takes a pattern we supply, while Containing, StartingWith and EndingWith add the % signs for us. IgnoreCase wraps both sides in upper(), as in findByEmailIgnoreCase. IsNull and IsNotNull take no parameter. LessThan, GreaterThanEqual and Before cover the remaining comparisons, and Distinct goes in the subject, findDistinctByStatus.

Java
List<User> findByStatusInAndEmailContainingIgnoreCase(List<Status> s, String part);
// where u.status in (?, ?) and upper(u.email) like upper(?) escape '\'
// with part = "acme" bound as "%acme%"
Optional<User> findFirstByEmailIgnoreCaseOrderByCreatedAtDesc(String email);
// single newest match, or Optional.empty()

5What a Misspelled Property Does

Suppose we type CreatedAfter instead of CreatedAtAfter. The parser reads Created as the property and After as the keyword, looks for a property called created on User and does not find one. Because Spring Data builds every derived query when it creates the repository bean, this happens during startup, not when the method is first called.

Java
List<User> findTop10ByStatusAndCreatedAfterOrderByCreatedAtDesc(Status s, Instant t);
// Application fails to start:
// ... Failed to create query for method public abstract java.util.List
// UserRepository.findTop10ByStatusAndCreatedAfterOrderByCreatedAtDesc(...)
// Caused by: org.springframework.data.mapping.PropertyReferenceException:
// No property 'created' found for type 'User'

This is one of the real strengths of derived queries. The mistake cannot reach production unnoticed, because any test that starts the Spring context, or the application itself, fails immediately with a message that names the missing property. Spring Data even suggests a close match in some versions; check your version for the exact wording.

6When to Stop Using Long Names

The name we were given is already long, and it only has two conditions. Add a third condition and an IgnoreCase, and the name is past eighty characters and hard to review: is that And or Or, and which parameter goes where? The parameters are bound by position, so swapping two parameters of the same type compiles fine and silently returns wrong data.

A good rule is to use derived names for one to three simple conditions and switch to @Query beyond that, where the query is written out and parameters are named. A default method with a short name can also wrap a long derived one, but that hides the query instead of making it readable.

Java
@Query("""
select u from User u
where u.status = :status and u.createdAt > :since
order by u.createdAt desc
limit 10""")
List<User> recentByStatus(@Param("status") Status status, @Param("since") Instant since);
// same result; JPQL limit is supported in Hibernate 6.x - check your version

Both forms are checked at startup, so switching to @Query with JPQL does not lose the early error detection.

7Follow-up questions

Is Top10 the same as passing a Pageable of size 10?

The result is the same rows, but Top10 is fixed in the method, while a Pageable lets the caller choose the page and size. If the limit is always ten, Top10 is simpler; if callers need different pages, take a Pageable or a Limit parameter, which newer Spring Data versions support.

Does the order of parameters matter?

Yes. Derived queries bind parameters by position, in the order the conditions appear in the name. Status s must come before Instant t here. Swapping two parameters of the same type would compile and return the wrong rows, which is another reason to prefer named parameters in @Query for longer queries.

What does IgnoreCase cost?

It wraps the column in upper() or lower(), so a normal index on the column cannot be used. For frequent case-insensitive lookups, create a functional index on lower(email) or upper(email), matching what the dialect generates, or store a normalised copy of the value.

8Conclusion

In this answer, we saw that Spring Data splits a derived method name into a subject and conditions, so findTop10ByStatusAndCreatedAtAfterOrderByCreatedAtDesc becomes a query on status equals and createdAt greater than, sorted newest first and limited to ten rows in the dialect's syntax. A misspelled property fails at startup with a PropertyReferenceException, which keeps errors out of production, and once a name grows past two or three conditions, @Query with named parameters is easier to read and review.

What to say in the interviewAbout 90 seconds aloud

Open with

Spring Data reads the name at startup. find is the subject, Top10 means limit to ten rows, and everything after By is the condition: Status means status equals the first parameter, And joins, CreatedAtAfter means createdAt greater than the second parameter, and OrderByCreatedAtDesc sorts newest first.

Then, if they want more

So the JPQL is select u from User u where u.status = ?1 and u.createdAt > ?2 order by u.createdAt desc, and Hibernate turns it into SQL like select the user columns from users where status = ? and created_at > ? order by created_at desc, with the limit rendered in the dialect's syntax, fetch first ten rows or limit ten.

The parser knows many keywords: And, Or, After, Before, Between, In, Like, Containing, IgnoreCase, IsNull, OrderBy, Top and First, Distinct. To keep it fast I would want an index on status and created_at.

If I misspell a property, say CreatedAfter, the parser looks for a property called created, does not find it, and the application fails at startup with a PropertyReferenceException saying no property created found for type User. That is a nice property of derived queries: the error shows up in any test that starts the context, not in production.

The downside is readability. With more than two or three conditions the name becomes hard to read, and parameters are bound by position, so I would switch to @Query with named parameters at that point.

About this answer

Last updated
12 November 2026
Versions it names
Hibernate 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What SQL would Spring Data JPA generate for List<User>...": https://scrivio-interview-library.netlify.app/databases/spring-data-jpa/01e8dd1587/