0% found this document useful (0 votes)
36 views12 pages

Data Management: Structured Query Language - Part 2

The document discusses various SQL join operators like cross join, inner join, outer join and how to write queries using these operators to combine data from multiple tables. It also covers relational set operators like union, union all, intersect, minus and examples of writing queries using these operators. The document concludes with an exercise asking to write queries to combine and filter customer data from sample customer and invoice tables.

Uploaded by

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

Data Management: Structured Query Language - Part 2

The document discusses various SQL join operators like cross join, inner join, outer join and how to write queries using these operators to combine data from multiple tables. It also covers relational set operators like union, union all, intersect, minus and examples of writing queries using these operators. The document concludes with an exercise asking to write queries to combine and filter customer data from sample customer and invoice tables.

Uploaded by

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

Data Management

Structured Query Language – part 2


SQL JOIN OPERATORS

• CROSS JOIN
• Perform relational product (Cartesian product) of two tables
• Same with old-style Join Tables, without WHERE clause
• Syntax : SELECT columnlist FROM table1 CROSS JOIN table 2
SQL JOIN OPERATORS
• INNER JOIN
• Returns only rows that meet the join condition (not limited to two tables)
• Can be written in many types of syntax
• NATURAL JOIN
• Returns only rows with matching values in the matching columns (must have same name and
data type)
• SELECT columnlist FROM table1 NATURAL JOIN table2
• JOIN USING clause
• Returns only rows with matching values in the columns indicated in the USING clause
• SELECT columnlist FROM table1 JOIN table2 USING (common-column)
• JOIN ON clause
• Returns only the rows that meet the join condition indicated in the ON clause
• SELECT columnlist FROM table1 JOIN table2 ON join-condition
• ON clause same with WHERE clause in old-style join (comparing PK and FK)
SQL JOIN OPERATORS

• OUTER JOIN
• Returns not only the rows matching the join condition (rows with matching values in
the common column), it also returns the rows with unmatched values
• LEFT JOIN
• Returns rows with matching values and includes all rows from the left table (T1) with
unmatched values
• SELECT columnlist FROM table1 LEFT JOIN table2 ON join-condition
• RIGHT JOIN
• Returns rows with matching values and includes all rows from the right table (T2) with
unmatched values
• SELECT columnlist FROM table1 RIGHT JOIN table2 ON join-condition
SQL examples

• SELECT VENDOR.V_CODE, V_NAME, P_DESCRIPT FROM VENDOR CROSS


JOIN PRODUCT
• SELECT VENDOR.V_CODE, V_NAME, P_DESCRIPT FROM VENDOR
NATURAL JOIN PRODUCT
• SELECT VENDOR.V_CODE, V_NAME, P_DESCRIPT FROM VENDOR JOIN
PRODUCT USING (V_CODE)
• SELECT VENDOR.V_CODE, V_NAME, P_DESCRIPT FROM VENDOR JOIN
PRODUCT ON VENDOR.V_CODE = PRODUCT.V_CODE
• SELECT VENDOR.V_CODE, V_NAME, P_DESCRIPT FROM VENDOR LEFT
JOIN PRODUCT ON VENDOR.V_CODE = PRODUCT.V_CODE
• SELECT VENDOR.V_CODE, V_NAME, P_DESCRIPT FROM VENDOR RIGHT
JOIN PRODUCT ON VENDOR.V_CODE = PRODUCT.V_CODE
RELATIONAL SET OPERATORS

• UNION
• Combines rows from two or more (SELECT) queries without including duplicate rows.
• The SELECT statement must be union-compatible (return the same number of
attributes and similar data types)
• Syntax :
SELECT column list FROM T1 [WHERE clause]
UNION
SELECT column list FROM T2 [WHERE clause]
RELATIONAL SET OPERATORS

• UNION ALL
• Combines rows from two or more (SELECT) queries, including duplicate rows.
• The SELECT statement must be union-compatible (return the same number of
attributes and similar data types)
• Syntax :
SELECT column list FROM T1 [WHERE clause]
UNION ALL
SELECT column list FROM T2 [WHERE clause]
RELATIONAL SET OPERATORS
• INTERSECT
• Combines rows from two or more (SELECT) queries, returning only rows that appear in
both sets.
• The SELECT statement must be union-compatible (return the same number of
attributes and similar data types)
• Syntax :
SELECT column list FROM T1 [WHERE clause]
INTERSECT
SELECT column list FROM T2 [WHERE clause]
• Syntax alternatives: using IN and subquery in WHERE clause
RELATIONAL SET OPERATORS

• MINUS
• Combines rows from two or more (SELECT) queries, returning only rows that appear in
the first set but not in the second.
• The SELECT statement must be union-compatible (return the same number of
attributes and similar data types)
• Syntax :
SELECT column list FROM T1 [WHERE clause]
MINUS
SELECT column list FROM T2 [WHERE clause]
• Syntax alternatives: using NOT IN and subquery in WHERE clause
SQL Example

• SELECT V_CODE FROM VENDOR UNION


SELECT V_CODE FROM PRODUCT
• SELECT V_CODE FROM VENDOR UNION ALL
SELECT V_CODE FROM PRODUCT
• SELECT V_CODE FROM VENDOR WHERE V_CODE IN (SELECT DISTINCT
V_CODE FROM PRODUCT)
• SELECT V_CODE FROM VENDOR WHERE V_CODE NOT IN (SELECT
DISTINCT V_CODE FROM PRODUCT WHERE V_CODE IS NOT NULL)
CUSTOMER
EXERCISE
CUST_NUM CUST_LNAME CUST_FNAME CUST_BALANCE
1000 Smith Jeanne 1050.11
1001 Ortega Juan 840.92

CUSTOMER_2
CUST_NUM CUST_LNAME CUST_FNAME
2000 McPherson Anne
2001 Ortega Juan
2002 Kowalski Jan
2003 Chen George
INVOICE
INV_NUM CUST_NUM INV_DATE INV_AMOUNT
8000 1000 23-mar-12 235.89
8001 1001 23-mar-12 312.82
8002 1001 30-mar-12 528.10
8003 1000 12-apr-12 194.78
8004 1000 23-apr-12 619.44
EXERCISE

1. Write the query that will generate a combined list of customers (LNAME
and FNAME) from the tables CUSTOMER and CUSTOMER_2 that do not
include the duplicate customer records.
2. Write the query that will generate customers (LNAME and FNAME) only
the records that are unique to the CUSTOMER_2 table
3. Write the query to show how the invoice number, customer number,
customer name, invoice date, and invoice amount for all customers in the
CUSTOMER table with a balance of $1,000 or more.

You might also like