w3resource

SQL creating view with JOIN

View with JOIN

In this page, we are going to discuss, how two or more tables can be involved and join themselves to make a view in CREATE VIEW statement.

Example:

Sample table: orders


Sample table: customer


Sample table: agents


To create a view 'ordersview' by three tables 'orders', 'customer' and ' agents' with following conditions -

1. 'a' and 'b' and 'c' are the aliases of 'orders' and 'customer' and 'agents' table,

2. 'cust_code' of 'orders' and 'customer' table must be same,

3. 'agent_code' of 'orders' and 'agents' table must be same,

the following SQL statement can be used:

CREATE VIEW ordersview
AS SELECT ord_num, ord_amount, a.agent_code,
agent_name, cust_name
FROM orders a, customer b, agents c
WHERE a.cust_code=b.cust_code
AND a.agent_code=c.agent_code;

Output:

Sql creating view with having

To execute query on this view

SELECT * FROM ordersview;

See our Model Database

Practice SQL Exercises

Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.

Previous: Create view with aggregate functions count(), sum() and avg()
Next: Update View