First page Back Continue Last page Image

Using a Subquery in the SELECT Clause

SELECT vendor_name,

(SELECT MAX(invoice_date) FROM invoices

WHERE vendor_id = vendors.vendor_id) AS latest_inv

FROM vendors

ORDER BY latest_inv DESC

While you can put a subquery in the SELECT clause, they usually end up being difficult to read, so it is a good idea to use another approach like the one on the next slide

The subquery is looking through the invoice table for every vendor to find their latest invoice