title: Correlated subquery using multiple tables
body: <p>I have some problem with some tasks.</p>
  <p>I've got many tasks like this: Create a query that shows up for every client, ID of his last invoice (name, surname, ID of last invoice, invoice date)</p>
  <p>Here are the tables:</p>
  <p>Table &quot;Clients&quot;:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ClientID</th>
  <th>Surname</th>
  <th>Name</th>
  <th>Pesel</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>ABC</td>
  <td>XYZ</td>
  <td>01234</td>
  </tr>
  <tr>
  <td>etc</td>
  <td>etc</td>
  <td>etc</td>
  <td>etc</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Pesel is a polish person id</p>
  <p>Table &quot;Invoices&quot;:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>InvoiceID</th>
  <th>InvoiceDate</th>
  <th>ClientID</th>
  <th>PaymentMethod</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>F1/2020</td>
  <td>01.01.2020</td>
  <td>1</td>
  <td>Cash</td>
  </tr>
  <tr>
  <td>etc</td>
  <td>etc</td>
  <td>etc</td>
  <td>etc</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I must do it using correlated subquery with aliases. I know how it works with comparing records from single table, but how to do it with two of them?</p>
  <p>The example I've got from a teacher is like:</p>
  <pre class="lang-sql prettyprint-override"><code>SELECT a.subject, a.grade, a.data
  FROM students_grade AS a
  WHERE data = ( 
                 SELECT MAX( data )
                 FROM students_grade AS b
                 WHERE a.subject = b.subject );
  </code></pre>
  <p>And I need to do it simillar</p>
  <p>Thanks for any kind of help :)</p>