w3resource

SQL Exercise: Display each item producer company

SQL JOINS: Exercise-21 with Solution

From the following tables write a SQL query to select all rows from both participating tables as long as there is a match between pro_com and com_id.

Sample table: company_mast

COM_ID COM_NAME
------ -------------
    11 Samsung
    12 iBall
    13 Epsion
    14 Zebronics
    15 Asus
    16 Frontech

Sample table: item_mast

 PRO_ID PRO_NAME                       PRO_PRICE    PRO_COM
------- ------------------------- -------------- ----------
    101 Mother Board                    3200.00         15
    102 Key Board                        450.00         16
    103 ZIP drive                        250.00         14
    104 Speaker                          550.00         16
    105 Monitor                         5000.00         11
    106 DVD drive                        900.00         12
    107 CD drive                         800.00         12
    108 Printer                         2600.00         13
    109 Refill cartridge                 350.00         13
    110 Mouse                            250.00         12

Sample Solution:

-- Selecting all columns from the result of an inner join between 'item_mast' and 'company_mast' tables
SELECT *
-- Specifying the tables to retrieve data from ('item_mast' as the left table and 'company_mast' as the right table)
FROM item_mast 
-- Performing an inner join based on the equality of 'pro_com' in 'item_mast' and 'com_id' in 'company_mast'
INNER JOIN company_mast
ON item_mast.pro_com = company_mast.com_id;

Output of the Query:

pro_id	pro_name		pro_price	pro_com	com_id	com_name
101	Mother Board		3200.00		15	15	Asus
102	Key Board		450.00		16	16	Frontech
103	ZIP drive		250.00		14	14	Zebronics
104	Speaker			550.00		16	16	Frontech
105	Monitor			5000.00		11	11	Samsung
106	DVD drive		900.00		12	12	iBall
107	CD drive		800.00		12	12	iBall
108	Printer			2600.00		13	13	Epsion
109	Refill cartridge	350.00		13	13	Epsion
110	Mouse			250.00		12	12	iBall

Explanation:

The said SQL query that retrieves all columns from the table item_mast and company_mast and combines the data based on the join condition specified in the ON clause.
The join condition is that the 'pro_com' column in the item_mast table matches the 'com_id' column in the company_mast table. The result of the query is a new table that contains all the columns from both item_mast and company_mast, with rows that have matching values in the specified columns.

Relational Algebra Expression:

Relational Algebra Expression: Display all the data from the item_mast, including all the data for each item's producer company.

Relational Algebra Tree:

Relational Algebra Tree: Display all the data from the item_mast, including all the data for each item's producer company.

Practice Online


Query Visualization:

Duration:

Query visualization of Display all the data from the item_mast, including all the data for each item's producer company - Duration

Rows:

Query visualization of Display all the data from the item_mast, including all the data for each item's producer company - Rows

Cost:

Query visualization of Display all the data from the item_mast, including all the data for each item's producer company - Cost

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous SQL Exercise: Customers from a different city than the salesmen.
Next SQL Exercise: Display the item name, price, and company name.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/sql-exercises/sql-joins-exercise-21.php