title: How to convert complex SQL query to JPQL?
body: <p>I have the following query in SQL.</p>
  <p>Here sender document table contains the sender and document entity.</p>
  <pre><code>@Entity
  @Table(name = &quot;sender_document&quot;)
  public class SenderDocument  {
  
      @Id
      @Column(columnDefinition = &quot;uuid&quot;)
      protected UUID id;
  
      @Column(name = &quot;created_at&quot;)
      private LocalDateTime createdAt;
  
      @ManyToOne
      @JoinColumn(name = &quot;sender_id&quot;)
      private Sender sender;
  
      @ManyToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = &quot;document_id&quot;)
      private Document document;
      
      //other columns
  
  }
  </code></pre>
  <p>I have to find the latest document of all sender whose expiry date is between start and end.</p>
  <pre><code>select * from sender_document join
  (select sender_id, max(created_at) as maxDate
  from sender_document
  group by sender_id ) as t2
  on created_at = t2.maxDate
  and t2.sender_id = sender_document.sender_id
  where sender_document.document.expiry_date between 'start_date' and 'end_date'
  </code></pre>
  <p>I have to convert this to JPQL or using criteria API. I am using the Postgres database.</p>
  <p>How to convert this to single JPQL?</p>