SQL INNER JOIN syntax

Both queries are an inner joins and equivalent. The first is the older method of doing things, whereas the use of the JOIN syntax only became common after the introduction of the SQL-92 standard (I believe it’s in the older definitions, just wasn’t particularly widely used before then). The use of the JOIN syntax is … Read more

How to make an Inner Join in django?

You are probably looking for select_related, which is the natural way to achieve this: pubs = publication.objects.select_related(‘country’, ‘country_state’, ‘city’) You can check the resulting SQL via str(pubs.query), which should result in output along the following lines (the example is from a postgres backend): SELECT “publication”.”id”, “publication”.”title”, …, “country”.”country_name”, … FROM “publication” INNER JOIN “country” ON … Read more

Is it true that using INNER JOIN after any OUTER JOIN will essentially invalidate the effects of OUTER JOIN?

A subsequent inner join will only “essentially invalidate” an outer join if the inner join’s ON clause requires should-be-optional rows to be present. In such a case, reordering the join either won’t work or won’t help; rather, the only fix is to change the inner join to an appropriate outer join. So, for example, this … Read more

MySQL difference between two rows of a SELECT Statement

SELECT mt1.ID, mt1.Kilometers, mt1.date, mt1.Kilometers – IFNULL(mt2.Kilometers, 0) AS number_km_since_last_date FROM myTable mt1 LEFT JOIN myTable mt2 ON mt2.Date = ( SELECT MAX(Date) FROM myTable mt3 WHERE mt3.Date < mt1.Date ) ORDER BY mt1.date Sql Fiddle Or, by emulating a lag() function through MySql hackiness… SET @kilo=0; SELECT mt1.ID, mt1.Kilometers – @kilo AS number_km_since_last_date, @kilo … Read more