0
0
Spring Bootframework~20 mins

Native SQL queries in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Native SQL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this native SQL query execution in Spring Boot?
Consider a Spring Boot repository method using a native query to fetch user names from a table named users. What will be the output list if the table contains three users: Alice, Bob, and Carol?
Spring Boot
public interface UserRepository extends JpaRepository<User, Long> {
    @Query(value = "SELECT name FROM users", nativeQuery = true)
    List<String> findAllUserNames();
}

// Assume the users table has names: Alice, Bob, Carol
A[{"name":"Alice"}, {"name":"Bob"}, {"name":"Carol"}]
B[User{name='Alice'}, User{name='Bob'}, User{name='Carol'}]
C["Alice", "Bob", "Carol"]
DEmpty list []
Attempts:
2 left
💡 Hint
The query selects only the 'name' column and returns a list of strings.
📝 Syntax
intermediate
2:00remaining
Which native query syntax is correct for updating a user's email in Spring Boot?
You want to update the email of a user with a specific id using a native SQL query in a Spring Boot repository. Which option is syntactically correct?
Spring Boot
public interface UserRepository extends JpaRepository<User, Long> {
    @Modifying
    @Query(value = "...", nativeQuery = true)
    int updateUserEmailById(String email, Long id);
}
AUPDATE users SET email = ?1 WHERE id = ?2
BUPDATE users SET email = :email WHERE id = :id
Cupdate users set email = :email, id = :id
Dupdate users set email = ?1 where id = ?2
Attempts:
2 left
💡 Hint
Native queries are case-insensitive but usually uppercase keywords are preferred. Positional parameters use ?1, ?2.
🔧 Debug
advanced
2:00remaining
Why does this native query cause a runtime error in Spring Boot?
Given the following repository method, why does executing it cause a runtime exception?
Spring Boot
public interface UserRepository extends JpaRepository<User, Long> {
    @Query(value = "SELECT * FROM users WHERE age > :age", nativeQuery = true)
    List<User> findUsersOlderThan(int age);
}
ANative queries cannot use WHERE clauses
BThe User entity does not map all columns returned by SELECT *
CThe method should return List<String> instead of List<User>
DThe parameter :age is not supported in native queries
Attempts:
2 left
💡 Hint
Check if the entity matches the table columns exactly.
state_output
advanced
2:00remaining
What is the value of 'count' after executing this native query update?
In a Spring Boot repository, you execute this native query to delete users older than 30. What is the value of the returned count?
Spring Boot
public interface UserRepository extends JpaRepository<User, Long> {
    @Modifying
    @Query(value = "DELETE FROM users WHERE age > 30", nativeQuery = true)
    int deleteUsersOlderThan30();
}

// Assume 5 users are older than 30 in the database.
A0
B-1
CThrows exception
D5
Attempts:
2 left
💡 Hint
The return value of a modifying query is the number of rows affected.
🧠 Conceptual
expert
2:00remaining
Which statement about native SQL queries in Spring Boot is TRUE?
Select the correct statement about using native SQL queries with Spring Data JPA in Spring Boot.
ANative queries require manual mapping if the result does not match the entity structure.
BNative queries always bypass the entity manager and cannot return entity objects.
CNative queries automatically support JPQL features like entity relationships and inheritance.
DNative queries cannot use parameters and must concatenate values directly.
Attempts:
2 left
💡 Hint
Think about how native SQL differs from JPQL in mapping results.