Question

Is there a difference between these two queries? Like performance issues, etc?

Query 1:

select  i.invoice_id,
        i.total_price
  from ( select invoice_id, 
                sum(price) as total_price 
           from orders
         group by 
                invoice_id
        ) as i 
               inner join invoice 
                   ON i.invoice_id = invoice.invoice_id

Query 2:

select invoice.invoice_id,
       orders.total_price 
  from invoice
       inner join ( select invoice_id, 
                           sum(price) as total_price 
                      from orders
                    group by 
                           invoice_id
                    ) orders 
           ON orders.invoice_id = invoice.invoice_id

Thanks!

Was it helpful?

Solution

Let me rewrite your queries without any sinifical changes:

Query 1

SELECT i.invoice_id,
       i.total_price
  FROM invoice INNER JOIN (
                           SELECT invoice_id, 
                                  sum(price) AS total_price 
                             FROM orders
                           GROUP BY 
                                  invoice_id
                           ) AS i 
                   ON i.invoice_id = invoice.invoice_id;

Query 2:

SELECT invoice.invoice_id,
       i.total_price 
  FROM invoice INNER JOIN (
                           SELECT invoice_id, 
                                  sum(price) AS total_price 
                             FROM orders
                           GROUP BY 
                                  invoice_id
                           ) AS i 
                   ON i.invoice_id = invoice.invoice_id;

things I changed:

  • order of JOIN (which doesn't matter, since it is INNER)
  • table alias (orders to i, and I really don't understand, why you wanted to name it differently)

Now, it is obvious, that the only difference between them - the first argument in the main SELECT. Your question could have made sence (if there was index on one column and wasn't on the other, and, dependant on the query, you would not always have used both orders.invoice_id and invoice.invoice_id), but since you already retrieving the both column for INNER JOIN it doesn't.

Futhermore, these queries are redundant. As already been mentioned by @valex, your query (actually - both of them) could (and must) be simplified to this:

SELECT invoice_id, 
       sum(price) AS total_price 
  FROM orders
GROUP BY 
       invoice_id;

So, no, there is no differnce in perfomance. And, surely, there is no difference in resultset.
Also, I'd like you to know, that you can always use EXPLAIN for perfomance questions.

OTHER TIPS

Your first query

select  i.invoice_id,
        i.total_price
  from ( select invoice_id, 
                sum(price) as total_price 
           from orders
         group by 
                invoice_id
        ) as i 
               inner join invoice 
                   ON i.invoice_id = invoice.invoice_id

is equivalent by its result to:

select invoice_id, 
     sum(price) as total_price 
from orders
group by invoice_id

To get the same result (if all invoice_id from orders exist in the invoice table) you don't need to JOIN the Invoice table just use query:

select invoice_id, 
       sum(price) as total_price 
from orders
      group by invoice_id
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top