Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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



SQL: Tips of the Day

SQL Server SELECT into existing table.

INSERT INTO dbo.TABLETWO
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:

INSERT INTO dbo.TABLETWO
  (col1, col2)
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

Database: SQL Server

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