0% found this document useful (0 votes)
28 views14 pages

SQL - Joins - Views

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views14 pages

SQL - Joins - Views

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

DBS211

STRUCTURED QUERY LANGUAGE (SQL)

1
What is a Join?
• Use a join to query data from more than one table

• Prefix the column name with the table name or table alias when the same
column name appears in more than one table

• To join n tables together, you need a minimum of (n-1) join conditions.

2
Cross Join (Cartesian Product)
• All rows in the first table are joined to all rows in the second table
• To avoid a Cartesian product, always include a valid join condition in a
WHERE clause

SELECT firstname || ' ' || lastname, customername


FROM dbs211_employees, dbs211_customers;

SELECT firstname || ' ' || lastname, customername


FROM dbs211_employees
CROSS JOIN dbs211_customers;

3
Qualifying Ambiguous Column Names
• Use table prefixes or table aliases to qualify column names that are in
multiple tables

• Improve performance by using table prefixes/aliases

• Distinguish columns that have identical names but reside in different


tables by using column aliases

4
Inner Join
• Inner Joins are also called simple joins or natural joins
• This type of join involves primary and foreign key
• The SELECT clause specifies the column names from two or more tables to
be retrieved
• The FROM clause specifies the two tables that the database must access
• The WHERE clause specifies how the tables are to be joined
• If joined columns have different data types, an error is returned

5
INNER Join/Natural Joins (cont’d)
• SQL-89 compliant Syntax - Simple Join
SELECT customernumber, customername, employeenumber, firstname || ' ' || lastname
FROM dbs211_customers c, dbs211_employees e WHERE c.salesrepemployeenumber = e.employeenumber;

• SQL-92 compliant Syntax


• Inner Join
SELECT customernumber, customername, employeenumber, firstname || ' ' || lastname
FROM dbs211_customers c JOIN dbs211_employees e ON c.salesrepemployeenumber = e.employeenumber;
• USING Clause (Join based on the columns specified in Using clause)
SELECT customernumber, customername, checknumber, paymentdate, amount
FROM dbs211_customers JOIN dbs211_payments USING (customernumber);
• Natural (Join based on all the columns that have the same name)
SELECT customernumber, customername, employeenumber, firstname || ' ' || lastname
FROM dbs211_customers c NATURAL JOIN dbs211_employees e;
6
LEFT OUTER JOIN (LEFT JOIN)
• A join between two tables that returns the results of the inner join as well as
unmatched rows left tables is a left outer join.
SELECT customernumber, customername, employeenumber, firstname || ' ' || lastname
FROM dbs211_customers c
LEFT OUTER JOIN dbs211_employees e ON c.salesrepemployeenumber = e.employeenumber;

• Inverse Left Outer Join


• Display Customer Numbers and names for those customers that have no Reps.
SELECT customernumber, customername, employeenumber, firstname || ' ' || lastname
FROM dbs211_customers c
LEFT OUTER JOIN dbs211_employees e ON c.salesrepemployeenumber = e.employeenumber
WHERE c.salesrepemployeenumber IS NULL;

7
RIGHT OUTER JOIN (RIGHT JOIN)
• A join between two tables that returns the results of the inner join as well as
unmatched rows right tables is a right outer join.
SELECT customernumber, customername, employeenumber, firstname || ' ' || lastname
FROM dbs211_customers c
RIGHT OUTER JOIN dbs211_employees e ON c.salesrepemployeenumber = e.employeenumber;

• Inverse Right Outer Join


• Display Customer Numbers and names for those customers that have no Reps.
SELECT customernumber, customername, employeenumber, firstname || ' ' || lastname
FROM dbs211_customers c
RIGHT OUTER JOIN dbs211_employees e ON c.salesrepemployeenumber = e.employeenumber
WHERE c.salesrepemployeenumber IS NULL;

8
FULL OUTER JOIN (FULL JOIN)
• A join between two tables that returns the results of an inner join as well as the
results of a left and right join.
SELECT customernumber, customername, employeenumber, firstname || ' ' || lastname
FROM dbs211_customers c
FULL OUTER JOIN dbs211_employees e ON c.salesrepemployeenumber = e.employeenumber;

• Inverse FULL Outer Join


• Display Customer Numbers and names, Employee Numbers and names for those customers that have no
Reps and employees that represent no customers.
SELECT customernumber, customername, employeenumber, firstname || ' ' || lastname
FROM dbs211_customers c
FULL OUTER JOIN dbs211_employees e ON c.salesrepemployeenumber = e.employeenumber
WHERE c.salesrepemployeenumber IS NULL;
9
View
• A view is a logical table based on a table or another view
• A view contains no data of its own
• The tables on which a view is based are called base tables
• A view is stored as a SELECT statement in the data dictionary

• Why use Views?


• To restrict data access
• To make complex queries easy
• To present different view of the same data

10
Creating a View
• You embed a subquery within the CREATE VIEW statement
• You can use the OR REPLACE option to change the definition of the view
without dropping and re-creating it.
• You can retrieve data from a view as you would from any table.
• The subquery can contain an ORDER BY clause

CREATE OR REPLACE VIEW customerVW AS


SELECT customernumber, customername, city
FROM dbs211_customers c
WHERE LOWER(city) IN ('nyc','las vegas')
ORDER BY customername;

11
Querying a View
• When you query a view, Oracle:
• Retrieves the view definition from the data dictionary
• Checks access privileges for the view base table
• Retrieves the data, or updates the base table

SELECT *
FROM customervw;

• You can query the data dictionary table called USER_VIEWS to see the
name of the view and definition
SELECT *
FROM user_views;

12
Removing a View
• DROP VIEW statement removes the view definition
• Dropping views has no effect on the tables

DROP VIEW customervw;

13
Inline Views
• An inline view is a subquery with an alias name that you can use within a SQL
statement
SELECT *
FROM (SELECT DISTINCT productvendor
FROM dbs211_products)
WHERE ROWNUM <= 10
ORDER BY productvendor;

SELECT customername, b.employeename, b.managername


FROM dbs211_customers a
JOIN (SELECT emp.employeenumber, emp.firstname || ' ' || emp.lastname AS employeename,
mgr.employeenumber AS mgrnumber , mgr.firstname || ' ' || mgr.lastname AS managername
FROM dbs211_employees emp
JOIN dbs211_employees mgr ON mgr.employeenumber = emp.reportsto) b
ON a.salesrepemployeenumber = b.employeenumber; 14

You might also like