w3resource

SQL Exercise: Join within the tables salesman, customer and orders

SQL JOINS: Exercise-7 with Solution

Write a SQL statement to join the tables salesman, customer and orders so that the same column of each table appears once and only the relational rows are returned.

Sample table: orders


Sample table: customer


Sample table : salesman


Sample Solution:

SELECT * 
FROM orders 
NATURAL JOIN customer  
NATURAL JOIN salesman;

Output of the Query:

salesman_id	city		customer_id	ord_no	purch_amt	ord_date	cust_name	grade	name		commission
5005		London		3001		70009	270.65		2012-09-10	Brad Guzan		Pit Alex	0.11
5001		New York	3002		70002	65.26		2012-10-05	Nick Rimando	100	James Hoog	0.15
5001		New York	3007		70005	2400.60		2012-07-27	Brad Davis	200	James Hoog	0.15
5001		New York	3002		70008	5760.00		2012-09-10	Nick Rimando	100	James Hoog	0.15
5006		Paris		3004		70010	1983.43		2012-10-10	Fabian Johnson	300	Mc Lyon		0.14
5001		New York	3002		70013	3045.60		2012-04-25	Nick Rimando	100	James Hoog	0.15

Explanation:

The said SQL query that uses the NATURAL JOIN clause to combine rows from three tables: orders, customer, and salesman.
The NATURAL JOIN clause compares all columns of the two joined tables and only returns the rows where the values match.
The query retrieves all columns from the three tables where the customer_id from the orders table matches the customer_id from the customer table, and the salesman_id from the orders table matches the salesman_id from the salesman table.
Note that using NATURAL JOIN can be dangerous if the tables have columns with the same name but different meanings, because it could lead to unexpected results.

Relational Algebra Expression:

Relational Algebra Expression: Make a join on the tables salesman, customer and orders in such a form that the same column of each table will appear once and only the relational rows will come.

Relational Algebra Tree:

Relational Algebra Tree: Make a join on the tables salesman, customer and orders in such a form that the same column of each table will appear once and only the relational rows will come.

Explanation:

Syntax of join within the tables salesman, customer and orders in such a form that the same column of each table will appear once and only the relational rows will come

Visual Explanation:

Result of join within the tables salesman, customer and orders in such a form that the same column of each table will appear once and only the relational rows will come
Result of join within the tables salesman, customer and orders in such a form that the same column of each table will appear once and only the relational rows will come

Practice Online


Query Visualization:

Duration:

Query visualization of Make a join on the tables salesman, customer and orders in such a form that the same column of each table will appear once and only the relational rows will come - Duration

Rows:

Query visualization of Make a join on the tables salesman, customer and orders in such a form that the same column of each table will appear once and only the relational rows will come - Rows

Cost:

Query visualization of Make a join on the tables salesman, customer and orders in such a form that the same column of each table will appear once and only the relational rows will come - Cost

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

Previous SQL Exercise: Display commission of the salesman for an order.
Next SQL Exercise: Customer who works either through a salesman or by own.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

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