﻿ SQL: Display the item name, price, and company name

# SQL Exercise: Display the item name, price, and company name

## SQL JOINS: Exercise-22 with Solution

Write a SQL query to display the item name, price, and company name of all the products.

Sample table: company_mast

Sample table: item_mast

Sample Solution:

``````SELECT item_mast.pro_name, pro_price, company_mast.com_name
FROM item_mast
INNER JOIN company_mast
ON item_mast.pro_com = company_mast.com_id;
``````

Output of the Query:

```pro_name		pro_price	com_name
Mother Board		3200.00		Asus
Key Board		450.00		Frontech
ZIP drive		250.00		Zebronics
Speaker			550.00		Frontech
Monitor			5000.00		Samsung
DVD drive		900.00		iBall
CD drive		800.00		iBall
Printer			2600.00		Epsion
Refill cartridge	350.00		Epsion
Mouse			250.00		iBall
```

Explanation:

The said SQL query that retrieves specific columns from the table item_mast and company_mast and the INNER JOIN clause to combine the data based on the join condition specified in the ON clause.
The selected columns are 'pro_name' from item_mast, 'pro_price' from item_mast and 'com_name' from company_mast. 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 the specified columns from both item_mast and company_mast, with rows that have matching values in the specified columns.

Relational Algebra Expression:

Relational Algebra Tree:

## Query Visualization:

Duration:

Rows:

Cost:

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

Previous SQL Exercise: Display each item producer company.
Next SQL Exercise: Display the average price of items of each company.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## SQL: Tips of the Day

Grouped LIMIT in PostgreSQL: Show the first N rows for each group?

```db=# SELECT * FROM xxx;
id | section_id | name
----+------------+------
1 |          1 | A
2 |          1 | B
3 |          1 | C
4 |          1 | D
5 |          2 | E
6 |          2 | F
7 |          3 | G
8 |          2 | H
(8 rows)
```

I need the first 2 rows (ordered by name) for each section_id, i.e. a result similar to:

```id | section_id | name
----+------------+------
1 |          1 | A
2 |          1 | B
5 |          2 | E
6 |          2 | F
7 |          3 | G
(5 rows)
```

PostgreSQL v9.3 you can do a lateral join

```select distinct t_outer.section_id, t_top.id, t_top.name from t t_outer
join lateral (
select * from t t_inner
where t_inner.section_id = t_outer.section_id
order by t_inner.name
limit 2
) t_top on true
order by t_outer.section_id;
```

Database: PostgreSQL

Ref: https://bit.ly/3AfYwZI

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook