I need to create the below SQL join condition using JPA criteria builder,

```
SELECT * FROM student s1
INNER JOIN
(SELECT subject,teacher,MIN(marks) AS marks FROM student GROUP BY subject, teacher) s2
ON s1.subject = s2.subject
AND s1.teacher = s2.teacher
AND s1.marks = s2.marks

```

Below is the entity class and JPA query builder.

```
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Student implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="id")
    Long id;

    @Column(name="name")
    String name;

    @Column(name="subject")
    public
    String subject;

    @Column(name="teacher")
    String teacher;

    @Column(name="marks")
    String marks;

    @JsonIgnore
    @ManyToOne
    @JoinColumns({
    @JoinColumn(insertable=false, updatable=false, name="subject",referencedColumnName="subject"),
    @JoinColumn(insertable=false, updatable=false, name="teacher",referencedColumnName="teacher"),
    @JoinColumn(insertable=false, updatable=false, name="marks",referencedColumnName="marks")
    })
    Student studentSelf;

    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL, mappedBy="studentSelf")
    Set<Student> studentref;

```

```
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Student> query = cb.createQuery(Student.class);
Root<Student> mainStudent = query.from(Student.class);
List<Predicate> predicates = new ArrayList<>();

Join<Student, Student> studentJoin = mainStudent.join("studentSelf", JoinType.INNER);

List<Student> list = entityManager.createQuery(query).getResultList();

```

I am able to build the query with join condition but cannot create the inner SELECT query. How can i create a inner select query for join clause?

**Requirement description**: The below is the input table and output required.

[![Student Table](https://i.stack.imgur.com/YabFa.png)](https://i.stack.imgur.com/YabFa.png)

[![Output](https://i.stack.imgur.com/4kgio.png)](https://i.stack.imgur.com/4kgio.png)