Examine the following scenario (source: http://phpweby.com/tutorials/mysql/32):
mysql> SELECT * FROM products;
+----+--------------+--------------+
| id | product_name | manufacturer |
+----+--------------+--------------+
| 1 | Shoes | Company1 |
| 2 | Laptop | Company2 |
| 3 | Monitor | Company3 |
| 4 | DVD | Company4 |
+----+--------------+--------------+
mysql> SELECT * FROM buyers;
+----+------+------------+----------+
| id | pid | buyer_name | quantity |
+----+------+------------+----------+
| 1 | 1 | Steve | 2 |
| 2 | 2 | John | 1 |
| 3 | 3 | Larry | 1 |
| 4 | 3 | Michael | 5 |
| 5 | NULL | Steven | NULL |
+----+------+------------+----------+
Suppose I'd like to create a single table where each buyer is listed along the products he bought. I can get very similar results using two different ways - one using JOIN
and the other using WHERE
.
mysql> SELECT buyer_name, quantity, product_name
FROM buyers LEFT JOIN products
ON buyers.pid=products.id;
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve | 2 | Shoes |
| John | 1 | Laptop |
| Larry | 1 | Monitor |
| Michael | 5 | Monitor |
| Steven | NULL | NULL |
+------------+----------+--------------+
mysql> SELECT buyers.buyer_name, buyers.quantity, products.product_name
FROM buyers,products
WHERE buyers.pid=products.id;
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve | 2 | Shoes |
| John | 1 | Laptop |
| Larry | 1 | Monitor |
| Michael | 5 | Monitor |
+------------+----------+--------------+
This is a toy example so I guess it doesn't really matter which way you choose (except the difference re. buyers who didn't actually buy anything, e.g. Steven).
But when it comes to large tables, is there a difference in efficiency between the the two queries? From some trials I did, I think there is.
I would be happy to better understand if that's correct and what is the fundamental difference between the implementation of the two schemes, and when should I prefer each of them.