JPQL Interview Questions and Answers

Master JPQL (Java Persistence Query Language) with interview questions covering SELECT, JOIN, FETCH JOIN, Named Queries, Parameters, Aggregation, Pagination, Bulk Updates, and JPQL vs SQL.


Introduction

JPQL (Java Persistence Query Language) is JPA's object-oriented query language. Unlike SQL, JPQL works with entities and their fields instead of database tables and columns.

It is portable across databases and is one of the most frequently asked topics in Spring Boot and Hibernate interviews.


JPQL Architecture

flowchart LR

Application --> JPQL

JPQL --> Hibernate

Hibernate --> SQL

SQL --> Database

Q1. What is JPQL?

Answer

JPQL is an object-oriented query language defined by JPA.

Instead of querying database tables, JPQL queries Java entities.

SQL

SELECT * FROM customers;

JPQL

SELECT c FROM Customer c

Here

  • Customer → Entity
  • c → Entity Alias

Example

List<Customer> customers =
entityManager.createQuery(
"SELECT c FROM Customer c",
Customer.class
).getResultList();

Benefits

  • Database independent
  • Uses entity names
  • Supports relationships
  • Type-safe when combined with Criteria API

Q2. JPQL vs SQL?

JPQL SQL
Uses Entities Uses Tables
Uses Entity Fields Uses Columns
Database Independent Database Specific
Object Oriented Relational
Portable Vendor Dependent

Example

JPQL

SELECT c FROM Customer c

SQL

SELECT * FROM CUSTOMER

Q3. What are the different JPQL statements?

JPQL supports

  • SELECT
  • UPDATE
  • DELETE
  • INSERT (Not Supported)

Select

SELECT c FROM Customer c

Update

UPDATE Customer c
SET c.status='ACTIVE'

Delete

DELETE FROM Customer c
WHERE c.active=false

Q4. How do parameters work in JPQL?

Named parameters improve readability.

TypedQuery<Customer> query =
entityManager.createQuery(
"SELECT c FROM Customer c WHERE c.id=:id",
Customer.class);

query.setParameter("id",1L);

Positional parameter

SELECT c FROM Customer c WHERE c.id=?1

Best Practice

Prefer named parameters.


Q5. What is JOIN in JPQL?

JPQL supports entity relationships instead of joining tables manually.

Example

SELECT o
FROM Order o
JOIN o.customer c

Diagram

flowchart LR

Order --> Customer

Generated SQL

SELECT *
FROM orders o
JOIN customers c
ON o.customer_id=c.id

Q6. What is FETCH JOIN?

Normally

SELECT o FROM Order o

loads orders only.

To load customer together

SELECT o
FROM Order o
JOIN FETCH o.customer

Diagram

flowchart TD

Order --> Customer

Customer --> Database

Benefits

  • Avoids LazyInitializationException
  • Reduces extra SQL
  • Solves N+1 problem

Q7. What are Aggregate Functions?

JPQL supports

  • COUNT
  • SUM
  • AVG
  • MAX
  • MIN

Count

SELECT COUNT(c)
FROM Customer c

Sum

SELECT SUM(o.total)
FROM Order o

Average

SELECT AVG(e.salary)
FROM Employee e

Q8. How does Pagination work?

Large datasets should never be loaded completely.

Example

TypedQuery<Customer> query =
entityManager.createQuery(
"SELECT c FROM Customer c",
Customer.class);

query.setFirstResult(0);

query.setMaxResults(20);

Generated SQL

LIMIT 20 OFFSET 0

Benefits

  • Faster
  • Lower Memory
  • Better Performance

Q9. What are Named Queries?

Named queries are precompiled JPQL queries.

Entity

@NamedQuery(
name="Customer.findAll",
query="SELECT c FROM Customer c"
)

Execution

entityManager.createNamedQuery(
"Customer.findAll",
Customer.class)
.getResultList();

Advantages

  • Reusable
  • Cleaner
  • Validated at startup

Q10. What are JPQL Best Practices?

Prefer TypedQuery

TypedQuery<Customer>

Use Named Parameters

Good

:id

Bad

?1

Fetch Only Required Data

Instead of

SELECT c

Use DTO

SELECT NEW
com.demo.CustomerDTO(
c.id,
c.name)

Avoid N+1 Problem

Use

JOIN FETCH

Use Pagination

setMaxResults()

Avoid SELECT *

Fetch only required fields.


Banking Example

flowchart TD

Customer --> Account

Account --> Transaction

Transaction --> JpqlQuery["JPQL Query"]

JpqlQuery["JPQL Query"] --> Database

Common Interview Questions

  • Difference between SQL and JPQL?
  • Difference between JOIN and FETCH JOIN?
  • What is TypedQuery?
  • What are Named Queries?
  • Can JPQL perform INSERT?
  • Difference between JPQL and Native SQL?
  • How is Pagination implemented?
  • What is DTO Projection?
  • How to avoid N+1 problem?
  • Why use JPQL instead of SQL?

Quick Revision

Topic Description
JPQL Object-oriented query language
Entity Used instead of Table
Field Used instead of Column
JOIN Relationship Join
FETCH JOIN Loads relationship immediately
Named Query Predefined JPQL
Pagination setFirstResult + setMaxResults
COUNT Aggregate Function
DTO Projection Fetch required fields
TypedQuery Type-safe query

JPQL Execution Flow

sequenceDiagram
Application->>EntityManager: JPQL
EntityManager->>Hibernate: Parse Query
Hibernate->>Database: SQL
Database-->>Hibernate: Result
Hibernate-->>EntityManager: Entity
EntityManager-->>Application: Objects

Key Takeaways

  • JPQL is an object-oriented query language that works with entities rather than database tables.
  • It is database-independent and automatically translated into SQL by the JPA provider.
  • JPQL supports SELECT, UPDATE, and DELETE, but does not support INSERT statements.
  • Use named parameters (:id) instead of positional parameters (?1) for better readability and maintainability.
  • JOIN is used to navigate relationships, while JOIN FETCH eagerly loads associated entities and helps prevent the N+1 query problem.
  • Aggregate functions such as COUNT, SUM, AVG, MIN, and MAX are supported.
  • Use setFirstResult() and setMaxResults() to implement efficient pagination.
  • @NamedQuery provides reusable and validated JPQL queries.
  • Prefer DTO projections when only a subset of fields is needed instead of loading entire entities.
  • Understanding JPQL is essential for building efficient, database-independent enterprise applications with Spring Boot and Hibernate.