JPQL Query using Distinct condition

Hey guys in this article, you will learn how to write a JPQL for the Distinct condition in the Spring Data JPA repository.

JPQL stands for Java persistence query language defined in JPA specification. It is used to create queries against entities to store in a relational database. JPQL is developed based on SQL syntax. Behind the scenes Data JPA convert these JPQL queries into native SQL queries.

Scenario: Consider the Employee entity class and if we want to retrieve distinct employee names then here is the Spring data JPA JPQL query:

@Query("SELECT DISTINCT FROM Employee e")
List getDistinctEmployeesByName();
  • @Query annotation to write the JPQL query. We use field names instead of column names and Entity name instead of table name.

Complete Example

We will create this example step by step, follow this tutorial till the end

Read More:

Create database and insert sample data

Open MySQL workbench and execute the following commands


USE demodb;

SELECT * FROM demodb.tbl_employees;

INSERT INTO tbl_employees
(age, department, location, name, salary) 
(28, "IT", "India", "Bushan", 20000.00);

INSERT INTO tbl_employees
(age, department, location, name, salary) 
(28, "Sales", "India", "Bushan", 20000.00);

INSERT INTO tbl_employees
(age, department, location, name, salary) 
(28, "Marketing", "India", "Bushan", 20000.00);

INSERT INTO tbl_employees
(age, department, location, name, salary) 
(28, "IT", "India", "Bharath", 20000.00);

INSERT INTO tbl_employees
(age, department, location, name, salary) 
(28, "IT", "India", "Chethan", 20000.00);

We have created the table tbl_employees. We have inserted the sample data as well.

Create spring boot project

There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add maven dependencies

Open pom.xml and add the following dependencies –

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring boot demo</description>



Configure the datasource

Open file and add the following contents



Create entity class

Create class in.bushansirgur.springsecurityjwt.entity package and add the following content

package in.bushansirgur.springbootdemo.entity;

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Table(name = "tbl_employees")
public class Employee {
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private String name;
	private String location;
	private String department;
	private Long age;
	private BigDecimal salary;

Create Repository class

Create class in.bushansirgur.springsecurityjwt.repository package and add the following content

package in.bushansirgur.springbootdemo.repository;

import java.util.List;

import org.springframework.stereotype.Repository;

import in.bushansirgur.springbootdemo.entity.Employee;

public interface EmployeeRepository extends JpaRepository<Employee, Long>{
	@Query("SELECT DISTINCT FROM Employee e")
	List<String> getDistinctEmployeesByName();

Call repository method from main() method

In order to call the repository method that we have created, we gonna use method to call the repository method. Once the application starts, spring boot will execute the following code –

package in.bushansirgur.springbootdemo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import in.bushansirgur.springbootdemo.repository.EmployeeRepository;

public class SpringbootdemoApplication implements CommandLineRunner{

	private EmployeeRepository employeeRepository;
	public static void main(String[] args) {, args);

	public void run(String... args) throws Exception {
		List<String> list = employeeRepository.getDistinctEmployeesByName();
		System.out.println("----------------List of Employees---------------------");
		list.forEach(e -> {


Run the app

Run the application using the below maven command –

mvn spring-boot:run

Once Spring boot application execution completes, you can able to see generated SQL statements in a console and the distinct values

        distinct as col_0_0_ 
        tbl_employees employee0_
----------------List of Employees---------------------


That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.

About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *