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?

6
  • 1
    #2 is not a "simple" query, but an older syntax for joins meaning "cartesian product" which is then limited by the WHERE clause. The queries are 100% the same, just a different syntax used to write them.
    – jkavalik
    Commented May 19, 2016 at 10:31
  • @jkavalik and what's the difference between JOINs and these old query? Commented May 19, 2016 at 10:32
  • 1
    None, only a different version of standard, the JOIN keyword possibly was defined only in the SQL-92 ANSI standard so it was not available before and the older syntax was used. But the meaning is the same in both cases.
    – jkavalik
    Commented May 19, 2016 at 10:35
  • 2
    Possible duplicate of What does it mean there is multiple sources in a SELECT's "FROM" clause? Commented Jan 16, 2018 at 18:31
  • I'm sorry. but I'm having difficulties understanding how a question that was asked 1 year and 8 months ago can be a duplicate of a question that was asked yesterday? I will vote to keep this question open.
    – John K. N.
    Commented Jan 17, 2018 at 9:22

1 Answer 1

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.

3
  • 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. Commented May 19, 2016 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. Commented May 19, 2016 at 11:07
  • I guess the other syntax you are referring to would be the SQL 89 or ANSI 89 standard?
    – John K. N.
    Commented May 19, 2016 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.