Linking of table is a very common requirement in SQL. Different types of data
can be stored in different tables and based on the requirement the tables can be
linked to each other and the records can be displayed in a very interactive way.
Let us take one example of linking of tables by considering product and customer
relationship. We have a product table where all the records of products are
stored. Same way we will have customer table where records of customers are
stored. The daily sales keep the record of all the sales. This sales table will
keep record of which product who has purchased. So linking is to be done from
Sales table to product table and customer table.
From these three tables let us find out the information on sales by linking
these tables. We will look into sales table and link it to the customer
table by the customer id field and in same way we will link product table by
product ID field. We will use WHERE sql command to link different tables. Here
is the command to do link three tables.
SELECT product_name, customer.name, date_of_sale FROM sales, product, customer WHERE product.product_id = sales.product_id and customer.customer_id >= sales.customer_id LIMIT 0, 30
The above SQL command links three tables and display the required result. The
tables are linked by their ID fields. The output is here
This way we can link three tables and get a meaningful report.
We may be interested to know which are the products not sold or who are the customers who have not purchased today.
We can prepare such reports by using Left Join.