Anda di halaman 1dari 10

BTE 351Chapter 7 Multi-Table Queries Teradata SQL Assistant

Getting Started:

Go to the following Website:


http://tunweb.teradata.ws/tunstudent/
Use your UserName and Password to Login

For each of the following examples:


1) Try to write the query yourself
2) Check your answer against the example query
3) If your query does not provide any results, chances are that there is a TYPO some part of the
syntax incorrect. (extra ;, missing , etc)

Tables for Reference:

Customer_T (TOTAL 15 ROWS)

CustomerID CustomerName CustomerAddress CustomerCity CustomerState CustomerPostalCode


2 Value Furniture 15145 S.W. 17th St. Plano TX 75094-7743
3 Home Furnishings 1900 Allard Ave. Albany NY 12209-1125
15 Mountain Scenes 4132 Main Street Ogden UT 84403-4432
13 Heritage Furnishings 66789 College Ave. Carlisle PA 17013-8834
1 Contemporary Casuals 1355 S Hines Blvd Gainesville FL 32601-2871
14 Kaneohe Homes 112 Kiowai St. Kaneohe HI 96744-2537
M and H Casual
9 3709 First Street Clearwater FL 34620-2314
Furniture
American Euro 2424 Missouri Ave
11 Prospect Park NJ 07508-5621
Lifestyles N.
2400 Rocky Point
10 Seminole Interiors Seminole FL 34646-4423
Dr.
7 Period Furniture 394 Rainbow Dr. Seattle WA 97954-5589
5 Impressions 5585 Westcott Ct. Sacramento CA 94206-4056
4 Eastern Furniture 1925 Beltline Rd. Carteret NJ 07008-3188
345 Capitol Ave.
12 Battle Creek Furniture Battle Creek MI 49015-3401
SW
6 Furniture Gallery 325 Flatiron Dr. Boulder CO 80514-4432
8 California Classics 816 Peach Rd. Santa Clara CA 96915-7754

Order_T Total 10 Rows

OrderID OrderDate CustomerID


1008 2010-10-30 12
1006 2010-10-24 2
1010 2010-11-05 1
10
BTE 351Chapter 7 Multi-Table Queries Teradata SQL Assistant

1004 2010-10-22 5
1009 2010-11-05 4
1003 2010-10-22 15
1005 2010-10-24 3
1002 2010-10-21 8
1007 2010-10-27 11
1001 2010-10-21 1

OrderLine_T Total 18 rows


OrderID ProductID OrderedQuantity
1008 3 3
1006 4 1
1010 8 10
1004 6 2
1009 4 2
1003 3 3
1005 4 4
1008 8 3
1006 5 2
1007 1 3
1004 8 2
1009 7 3
1001 1 2
1006 7 2
1007 2 2
1001 2 2
1002 3 5
1001 4 1

Product_T (Total 8 Rows)


ProductID ProductDescription ProductFinish ProductStandardPrice ProductLineID
2 Coffee Table Natural Ash 200.00 2
3 Computer Desk Natural Ash 375.00 2
1 End Table Cherry 175.00 1
8 Computer Desk Walnut 250.00 3
7 Dining Table Natural Ash 800.00 2
5 Writers Desk Cherry 325.00 1
4 Entertainment Center Natural Maple 650.00 3
10
BTE 351Chapter 7 Multi-Table Queries Teradata SQL Assistant

6 8-Drawer Desk White Ash 750.00 2

Employee_T

Employe EmployeeN EmployeeAd Employee EmployeeS EmployeeZip EmployeeDate EmployeeSuper


eID ame dress City tate code Hired visor
454-56- Robert 17834
Nashville TN 1999-01-01
768 Lewis Deerfield Ln
123-44- 2134 Hilltop
Jim Jason TN 1999-06-12 454-56-768
345 Rd

Query Result
What are the Customer IDs and Names of ALL
Customers, along with the order IDs for all the Orders
they have placed?
SELECT CustomerID CustomerName OrderID
Customer_T.CustomerID,Customer_T.CustomerName,
Order_T.OrderID 1 Contemporary Casuals 1001
8 California Classics 1002
FROM Customer_T, Order_T 15 Mountain Scenes 1003

WHERE Customer_T.CustomerID = 5 Impressions 1004


Order_T.CustomerID 3 Home Furnishings 1005
2 Value Furniture 1006
ORDER BY OrderID
11 American Euro Lifestyles 1007
12 Battle Creek Furniture 1008
4 Eastern Furniture 1009
1 Contemporary Casuals 1010
[USING AS TO RENAME TABLES:
Shorthand for the above)

SELECT C_T.CustomerID,
C_T.CustomerName,
O_T.OrderID
FROM Customer_T AS C_T, Order_T AS O_T
WHERE C_T.CustomerID=O_T.CustomerID;

What are the Customer IDs and names of all


customers, along with the order IDs for all the orders
they have placed?
SELECT CustomerID CustomerName OrderID
Customer_T.CustomerID,Customer_T.CustomerName,
10
BTE 351Chapter 7 Multi-Table Queries Teradata SQL Assistant

Order_T.OrderID 1 Contemporary Casuals 1001


FROM Customer_T JOIN Order_T ON
Customer_T.CustomerID=Order_T.CustomerID 8 California Classics 1002
ORDER BY OrderID 15 Mountain Scenes 1003
5 Impressions 1004
3 Home Furnishings 1005
2 Value Furniture 1006
11 American Euro Lifestyles 1007
12 Battle Creek Furniture 1008
4 Eastern Furniture 1009
1 Contemporary Casuals 1010
SELECT CustomerID CustomerName OrderID
Customer_T.CustomerID,Customer_T.CustomerName,
Order_T.OrderID 1 Contemporary Casuals 1001
FROM Customer_T INNER JOIN Order_T ON 8 California Classics 1002
Customer_T.CustomerID=Order_T.CustomerID 15 Mountain Scenes 1003
ORDER BY OrderID
5 Impressions 1004
3 Home Furnishings 1005
2 Value Furniture 1006
11 American Euro Lifestyles 1007
12 Battle Creek Furniture 1008
4 Eastern Furniture 1009
1 Contemporary Casuals 1010
SELECT C_T.CustomerID,
C_T.CustomerName,
O_T.OrderID
FROM Customer_T AS C_T JOIN Order_T AS O_T
ON
C_T.CustomerID=O_T.CustomerID;
List Customer Name, ID, Order_Number for all
customers listed in the Customer Table. Include the
Customer ID and Name, even if the customer has not
placed any orders.
SELECT Customer_T.CustomerID, CustomerID CustomerName OrderID
Customer_T.CustomerName,
Order_T.OrderID 13 Heritage Furnishings ?
FROM Customer_T LEFT OUTER JOIN Order_T ON 6 Furniture Gallery ?
Customer_T.CustomerID=Order_T.CustomerID 14 Kaneohe Homes ?
ORDER BY OrderID;
9 M and H Casual Furniture ?
10 Seminole Interiors ?
10
BTE 351Chapter 7 Multi-Table Queries Teradata SQL Assistant

7 Period Furniture ?
1 Contemporary Casuals 1001
8 California Classics 1002
15 Mountain Scenes 1003
5 Impressions 1004
3 Home Furnishings 1005
2 Value Furniture 1006
11 American Euro Lifestyles 1007
12 Battle Creek Furniture 1008
4 Eastern Furniture 1009
1 Contemporary Casuals 1010
SELECT Customer_T.CustomerID, CustomerID CustomerName OrderID
Customer_T.CustomerName,
Order_T.OrderID 1 Contemporary Casuals 1010
FROM Customer_T LEFT OUTER JOIN Order_T ON 1 Contemporary Casuals 1001
Customer_T.CustomerID=Order_T.CustomerID 2 Value Furniture 1006
ORDER BY Customer_T.CustomerID;
3 Home Furnishings 1005
4 Eastern Furniture 1009
5 Impressions 1004
6 Furniture Gallery ?
7 Period Furniture ?
8 California Classics 1002
9 M and H Casual Furniture ?
10 Seminole Interiors ?
11 American Euro Lifestyles 1007
12 Battle Creek Furniture 1008
13 Heritage Furnishings ?
14 Kaneohe Homes ?
15 Mountain Scenes 1003
SELECT CustomerID CustomerName OrderID
Customer_T.CustomerID,Customer_T.CustomerName,
Order_T.OrderID 1 Contemporary Casuals 1001
FROM Customer_T RIGHT OUTER JOIN Order_T ON 8 California Classics 1002
Customer_T.CustomerID=Order_T.CustomerID 15 Mountain Scenes 1003
ORDER BY OrderID;
5 Impressions 1004
3 Home Furnishings 1005
2 Value Furniture 1006
10
BTE 351Chapter 7 Multi-Table Queries Teradata SQL Assistant

11 American Euro Lifestyles 1007


12 Battle Creek Furniture 1008
4 Eastern Furniture 1009
1 Contemporary Casuals 1010
Assemble all the information necessary to create an
invoice for ORDER number 1006
SELECT Customer_T.CustomerID, (See Table 1)
Customer_T.CustomerName, Order_T.OrderID,
OrderDate, OrderedQuantity, ProductDescription,
ProductStandardPrice, (OrderedQuantity *
ProductStandardPrice)
FROM Customer_T , Order_T, OrderLine_T ,
Product_T
WHERE
CUSTOMER_T.CustomerID=ORDER_T.CustomerID AND
ORDER_T.OrderID = OrderLine_T.OrderID AND
ORDERLINE_T.ProductID=PRODUCT_T.ProductID AND
Order_T.OrderID= 1006;
What are the EmployeeID and name of Each
Employee, and the name his/her supervisor. Label the
SUPERVISOR column Manager)
SELECT E.EmployeeID, E.EmployeeName, (See Table 2)
M.EmployeeName AS MANAGER
FROM Employee_T AS E, Employee_T AS M
WHERE E.EmployeeSupervisor = M.EmployeeID;
Provide the name and address of the customer who
placed order number 1008.
SELECT Customer_T.CustomerName, CustomerName CustomerAddress
Customer_T.CustomerAddress
FROM Customer_T, Order_T Battle Creek Furniture 345 Capitol Ave. SW
WHERE
Customer_T.CustomerID = Order_T.CustomerID AND
OrderID=1008;
Provide the name and address of the customer who
placed order number 1008 Sub query Approach
SELECT Customer_T.CustomerName, CustomerName CustomerAddress
Customer_T.CustomerAddress
FROM Customer_T Battle Creek Furniture 345 Capitol Ave. SW
WHERE Customer_T.CustomerID IN
(
SELECT Order_T.CustomerID
FROM Order_T
WHERE OrderID=1008
);
Result of just the SubQuery: CustomerID
10
BTE 351Chapter 7 Multi-Table Queries Teradata SQL Assistant

SELECT Order_T.CustomerID 12
FROM Order_T
WHERE OrderID=1008;
What are the names of customers who have placed
orders?
SELECT CustomerName FROM Customer_T WHERE CustomerName
CustomerID IN (SELECT CustomerID FROM Order_T);
Value Furniture
Home Furnishings
Mountain Scenes
Contemporary Casuals
American Euro Lifestyles
California Classics
Impressions
Eastern Furniture
Battle Creek Furniture
SELECT CustomerID FROM Order_T; CustomerID
12
2
1
5
4
15
3
8
11
1
Which Customers have NOT placed ANY orders for
computer desks?

First ask Which Customers HAVE placed orders for


Computer Desks
Select CustomerID CustomerID
from Order_T AS OT, OrderLine_T AS OLT, Product_T
AS PT 12
WHERE OT.OrderID=OLT.OrderID
AND OLT.ProductID = PT.ProductID and
8
PT.ProductDescription = 'Computer Desk';
1
10
BTE 351Chapter 7 Multi-Table Queries Teradata SQL Assistant

15

12

Which Customers have NOT placed orders for the


above?
Select CustomerName CustomerName
FROM Customer_T
WHERE CustomerID NOT IN Value Furniture
(Select CustomerID
from Order_T AS OT, OrderLine_T AS OLT, Product_T
Home Furnishings
AS PT
WHERE OT.OrderID=OLT.OrderID
Heritage Furnishings
AND OLT.ProductID = PT.ProductID and
PT.ProductDescription = 'Computer Desk');
Furniture Gallery

Kaneohe Homes

M and H Casual Furniture

American Euro Lifestyles

Seminole Interiors

Period Furniture

Eastern Furniture

Which Product IDs have a Natural Ash Finish


Select ProductID ProductID
From Product_T
Where ProductFinish = 'Natural Ash'; 2

What are the Order IDs for all orders that have
included furniture finished in Natural Ash?
Select OrderID FROM OrderLine_T WHERE OrderID
ProductID IN (Select ProductID
10
BTE 351Chapter 7 Multi-Table Queries Teradata SQL Assistant

From Product_T 1007


Where ProductFinish = 'Natural Ash');
1003

1006

1001

1008

1009

1002

ALTERNATE SOLUTION: OrderID

SELECT Distinct OrderID FROM OrderLine_T Where 1001


EXISTS
(Select * From Product_T
1002
Where Product_T.ProductFinish = 'Natural Ash' and
Product_T.ProductID=OrderLine_T.ProductID);
1003

1006
The sub query is executed for each order line in the
outer query. The inner query usually shows all (*). We
use the outer query to do the actual selection process 1007

1008

1009

List the details about the product with the highest


standard price
SELECT ProductDescription, ProductFinish, ProductDescription ProductFinish ProductStandardPrice
ProductStandardPrice
FROM Product_T AS PA Dining Table Natural Ash 800.00
WHERE PA.ProductStandardPrice > ALL
(Select ProductStandardPrice FROM Product_T AS TB
WHERE TB.ProductID <> PA.ProductID);
10
BTE 351Chapter 7 Multi-Table Queries Teradata SQL Assistant

Table 1
(OrderedQuantity
Custom Customer Orde OrderedQu ProductStanda
OrderDate ProductDescription *ProductStandard
erID Name rID antity rdPrice
Price)
Value
2 1006 2010-10-24 2 Dining Table 800.00 1600.00
Furniture
Value
2 1006 2010-10-24 2 Writers Desk 325.00 650.00
Furniture
Value Entertainment
2 1006 2010-10-24 1 650.00 650.00
Furniture Center

Table 2
EmployeeID EmployeeName MANAGER
123-44-345 Jim Jason Robert Lewis

10

Anda mungkin juga menyukai