0

I have two tables in database one is Orders and another one is customers

Orders Table Contains : OrderID , CustomerID and OrderDate

Customers Table Contains: CustomerID , CustomerName , ContactName and country

Actually the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.

Query with Join

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
    FROM Orders
    INNER JOIN Customers
    ON Orders.CustomerID=Customers.CustomerID;

Simple Query

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders,Customers
WHERE Orders.CustomerID=Customers.CustomerID;

Result of Both Queries is : OrderID , CustomerName and OrderDate

On both the above query i get the same result . I want to know the difference between these queries and if we can do it via SIMPLE query why use JOIN?

Iftikhar uddin
  • 135
  • 1
  • 7

1 Answers1

5

There is no difference: your two examples are completely equivalent but using different versions of SQL syntax. The database engine will handle them in exactly the same way.

Your first example is using an explicit join and is the preferred syntax these days. It was introduced in the SQL-92 standard and is supported by pretty much every SQL-style query engine.

Your second example is usually referred to as an implicit join. It is an older syntax but still supported by most SQL based query engines and is still in the standard (so is considered to be correct even though the newer syntax is preferred). It quickly fell out of favour with the introduction and wide support of explicit joins as these are usually clearer and easier to read & maintain (though the older syntax could be said to have the advantage of being more concise).

See https://en.wikipedia.org/wiki/Join_(SQL) for further examples of this and other join types, including some engine specific variations.

David Spillett
  • 32,103
  • 3
  • 49
  • 89
  • 4
    That "still supported by many SQL based query engines" should probably be "by all" or "almost all". It's still in the SQL standard and I can't see why a DBMS that added JOIN support would remove support for the implicit join syntax. – ypercubeᵀᴹ May 19 '16 at 10:53
  • Good points, I've edited accordingly. I can't name a database engine that supports SQL92 syntax but not the other, but I've seen several libraries that provide sql-like interfaces for objects or services which seek to be compatible with explicit join syntax but offer no support for the other. – David Spillett May 19 '16 at 11:07
  • I guess the other syntax you are referring to would be the SQL 89 or ANSI 89 standard? – John K. N. May 19 '16 at 11:13