Sunteți pe pagina 1din 23

CHAPTER 3: QUESTIONS AND ANSWERS

1 Why do some people pronounce SQL as sequel? Ans: Because of its naming history, SQL is developed from SEQUEL language, so some people pronounce SQL as sequel. 2 Why are the manipulation statements of SQL more widely used than the definition and control statements? Ans: Because only the database administrator uses the definition and control statements, while power users and analysts, who are the majority, use the manipulation statements. 3 Is the SQL standard supported in whole or in part? Briefly explain. Ans: The SQL standard is supported in part. Because of the size of the previous standard SQL-92 (contains more than 600 pages) and the current SQL: 1999 standard (contains more than 2,100 pages), there are multiple levels that can be supported. No vendor supports the entire SQL-92 standard now. Most vendors support a super/subset of the standard. With the new SQL: 1999 standard, vendor implementation will likely be more fragmented because of the size and scope of the standard. 4 How many levels of conformance does the SQL-92 standard have? Ans: The SQL-92 specification contained three levels: Entry SQL, Intermediate SQL, and Full SQL. Unfortunately, no vendor claimed conformance beyond Entry SQL although most vendors implemented some features in the higher SQL levels as well as providing proprietary SQL extensions. 5 How many levels of conformance do the SQL: 1999 standard have? Ans: The SQL: 1999 standard contains a single level of conformance called Core SQL. At the time of writing of this chapter, no vendor claims conformance to Core SQL although some vendors seem close to conformance. SQL: 1999 contains many features outside of Core SQL grouped into packages. 6 What is standalone SQL? Ans: In standalone SQL, the user submits SQL statements with the use of a specialized editor. The editor alerts the user to syntax errors and sends the statements to the DBMS. 7 What is embedded SQL? Ans: In the embedded context, an executing program submits SQL statements, and the DBMS sends results back to the program. The program includes SQL statements along with statements of the host programming language such as COBOL or Visual Basic. There are additional statements that allow other SQL statements (such as SELECT) to be used inside a computer program. 8 What is an expression in the context of database languages? Ans: An expression is a combination of constants, column names, functions, and operators that produces a value. In conditions and result columns, expressions can be used in any place that column names can appear.

9 From examples and discussion in Chapter 3, what parts of SELECT are not supported by all DBMSs? Ans: The different parts are pattern- matching characters, case sensitivity in string matching, date constants, the inequality symbol, join operator style, and difference operations. 10 Recite the rule about GROUP BY and HAVING. Ans: The GROUP BY clause must contain every column in the Select clause except for aggregate expressions. The HAVING clause must be preceded by the GROUP BY clause. 11 Recite the rule about columns in SELECT when a GROUP BY clause is used. Ans: The columns in the SELECT clause must either be in the GROUP BY clause or be part of a summary calculation with an aggregate function. 12 How does a row condition differ from a group condition? Ans: A condition in a row (WHERE clause) cannot involve aggregate functions whereas a condition in a group (HAVING clause) involves aggregate functions. 13 Why should row conditions be placed in the WHERE clause rather than the HAVING clause? Ans: Often, execution speed will improve if row conditions are tested in the WHERE clause rather than the HAVING clause. In the conceptual evaluation process, testing of conditions in the WHERE clause precedes testing of group conditions in the HAVING clause. The HAVING clause should be used for conditions that can be tested only on groups. 14 Why are most DBMS not case sensitive when matching on string conditions? Ans: Because case non-sensitive promotes user-friendliness. 15 Explain how working with sample tables can provide insight about difficult problems. Ans: Working with sample tables helps users understand the conceptual evaluation process of SQL statements and visualize the result of a query, so that users can imitate the statement in samples table and apply it to the real problem. 16 When working with date columns, why is it necessary to refer to documentation of your DBMS? Ans: Because the date constants are different among DBMS. For example, in Access SQL, pound symbols enclose date constants; while in Oracle SQL, single quotation marks enclose date constants. 17 How do exact and inexact matching differs in SQL? Ans: Exact matching is used to find an identical string, but inexact matching supports conditions that match just some part of the string. Along with wildcard characters, inexact matching could be used to find values having a common prefix (or suffix) or

match strings containing a substring. Whereas inexact matching uses operators such as LIKE, exact matching uses the equality comparison operator (=). 18 How do you know when the output of a query relates to groups of rows as opposed to individual rows? Ans: In a problem statement, you should look for computations involving aggregate functions. For example, the problem list the name and average grade of students contains an aggregate computation. Problems referencing an aggregate function indicate that the output relates to groups of rows. In a SELECT statement, the GROUP BY and HAVING keywords indicate that the output relates to groups of rows rather than individual rows. 19 What tables belong in the FROM statement? Ans: The FROM clause should contain all tables that were mentioned in the problem statement. You should match the columns listed in the problem statement with columns from various tables. Include columns that are needed for output as well as for conditions. You also should include tables that are needed only to connect other tables. 20 Explain the cross product style for join operations. Ans: The cross product style lists tables in the FROM clause and join conditions in the WHERE clause. 21 Explain the join operator style for join operations. Ans: The join operator style lists join operations in the FROM clause us ing the INNER JOIN and ON keywords. 22 Discuss the pros and cons of the cross product versus the join operator styles. Do you need to know both the cross product and the join operator styles? Ans: The cross product style is easy to read but does not support outer join operations. The join operator style can be difficult to read but supports outer join operations. We need to know both, since sometimes we need the query results of outer join operations. 23 What is a self-join? When is a self- join useful? Ans: A self-join is a join between a table and itself (two copies of the same table). Selfjoin is useful for finding relationships among rows of the same table. 24 Provide a SELECT statement example in which a table is needed even though the table does not provide conditions to test or columns to show in the result. Ans: SELECT Student.* FROM Student, Faculty WHERE StdSSN = FacSSN (This SELECT statement lists students who are the faculty.) 25 What is the requirement when using the traditional set operators in a SELECT statement?

Ans: Tables must be union compatible (table must have the same number of columns and each corresponding column must have the same data type). 26 When combining joins and grouping, what conceptually occurs first, joins or grouping? Ans: Joins always occur before grouping. 27 How many times can grouping occur in a SELECT statement? Ans: Grouping can occur only one time. 28 Why is the SELECT statement more widely used than the modification statements INSERT, UPDATE, and DELETE? Ans: Because end users can use data entry forms, which are easier than using the modification statements. 29 Provide an example of an INSERT statement that can insert multiple rows. Ans: INSERT INTO ISFaculty SELECT * FROM Faculty WHERE FacDept = 'IS' (This INSERT statement adds rows into the ISFaculty table.) 30 What is the relationship between the DELETE statement and the rules about deleting referenced rows? Ans: By the referential integrity constraint, the DELETE statement is subject to the actions on referenced rows. A row cannot be deleted if related rows exist and the deletion action is restrict. 31 What is the relationship between the UPDATE statement and the rules about updating the primary key of referenced rows? Ans: The UPDATE statement is subject to the actions on updating the primary key of referenced rows. Update rules on referenced rows may not allow the operation when changing the primary key. 32 How does COUNT(*) differ from COUNT(ColumnName)? Ans: COUNT(*) computes the number of rows. COUNT(ColumnName) computes the number of non null column values. If a column does not have null values, COUNT(*) and COUNT(ColumnName) are identical. 33 How does COUNT(DISTINCT ColumnName) differ from COUNT(ColumnName)? Ans: COUNT(DISTINCT ColumnName) computes the number of unique column values. COUNT(ColumnName) computes the number of non null column values. If a column has unique values such as a primary key or candidate key, COUNT(DISTINCT ColumnName) and COUNT(ColumnName) are identical.

PROBLEM SOLUTIONS
The problems use the tables of the order entry database, an extension of the order entry tables used in the problems of Chapter 2. In the problem description in the textbook, Table 3P1 lists the meaning of each table and Figure 3P.1 shows the Access Relationship window. After the relationship diagram, row listings and Oracle CREATE TABLE statements are shown for each table. Note that the primary key of the OrdLine table is a combination of OrdNo and ProdNo. The Employee table has a self-referencing (unary) relationship to itself through the foreign key, SupEmpNo, the employee number of the supervising employee. In the relationship diagram, the table Employee_1 is a representation of the self-referencing relationship, no t a real table. The relationship from OrderTbl to OrdLine cascades deletions and primary key updates of referenced rows. All other relationships restrict deletions and primary key updates of referenced rows if related rows exist (NO ACTION). Part 1: SELECT 1. List the customer number, name (first and last), and balance of customers. Ans: SELECT CustNo, CustFirstName, CustLastName, CustBal FROM Customer 2. List the customer number, name (first and last), and balance of customers who reside in Colorado (CustState is CO). Ans: SELECT CustNo, CustFirstName, CustLastName, CustBal FROM Customer WHERE CustState = 'CO' 3. List all columns of the Product table for products costing more than $50. Order the result by product manufacturer (ProdMfg) and product name.

Ans: SELECT * FROM Product WHERE ProdPrice > 50 ORDER BY ProdMfg, ProdName 4. List the order number, order date, and shipping name (OrdName) of orders sent to addresses in Denver or Englewood.

Ans: The first solution uses the IN comparison operator and the second solution uses the Boolean OR connector. SELECT OrdNo, OrdDate, OrdName FROM OrderTbl WHERE OrdCity IN ('Denver', 'Englewood')

SELECT OrdNo, OrdDate, OrdName FROM OrderTbl WHERE OrdCity = 'Denver' OR OrdCity = 'Englewood' 5. List the customer number, name (first and last), city, and balance of customers who reside in Denver with a balance greater than $150 or who reside in Seattle with a balance greater than $300.

Ans: The parentheses are necessary when mixing the logical AND and OR connectors. SELECT CustNo, CustFirstName, CustLastName, CustCity, CustBal FROM Customer WHERE (CustCity = 'Denver' AND CustBal > 150) OR (CustCity = 'Seattle' AND CustBal > 200) 6. List the cities and states where orders have been placed. Remove duplicates from the result.

Ans: The DISTINCT keyword eliminates duplicate rows. SELECT DISTINCT OrdCity, OrdState FROM OrderTbl 7. List all columns of the OrderTbl table for Internet orders placed in January 2004. An Internet order does not have an associated employee.

Ans: Two solutions are provided because Access and Oracle have different formats for date constants. Access SQL solution: SELECT * FROM OrderTbl WHERE OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND EmpNo IS NULL Oracle SQL solution: SELECT * FROM OrderTbl WHERE OrdDate BETWEEN '1-Jan-2004' AND '31-Jan-2004' AND EmpNo IS NULL 8. List all columns of the OrderTbl table for phone orders placed in February 2004. A phone order has an associated employee.

Ans: Two solutions are provided because Access and Oracle have different formats for date constants. Access SQL solution:

SELECT * FROM OrderTbl WHERE OrdDate BETWEEN #2/1/2004# AND #2/29/2004# AND EmpNo IS NOT NULL Oracle SQL solution: SELECT * FROM OrderTbl WHERE OrdDate BETWEEN '1-Feb-2004' AND '29-Feb-2004' AND EmpNo IS NOT NULL 9. List all columns of the Product table that contain the words Ink Jet in the product name.

Ans: The only difference in the Access and Oracle solutions is the wildcard character (* versus %, respectively). In Oracle SQL, because string matching is case sensitive, the constant must match the case of the ProdName value. Access SQL solution: SELECT * FROM Product WHERE ProdName LIKE '*Ink Jet*' Oracle SQL solution: SELEC T * FROM Product WHERE ProdName LIKE '%Ink Jet%'

10. List the order number, order date, and customer number of orders placed after January 23, 2004, that are shipped to Washington recipients. Ans: The only difference in the Access and Oracle solutions is representation of date constants. Access SQL solution: SELECT OrdNo, OrdDate, CustNo FROM OrderTbl WHERE OrdState = 'WA' AND OrdDate > #1/23/2004# Oracle SQL solution: SELECT OrdNo, OrdDate, CustNo FROM OrderTbl WHERE OrdState = 'WA' AND OrdDate > '23-Jan-2004' 11. List the order number, order date, customer number, and customer name (first and last) of orders placed in January 2004 sent to Colorado recipients.

Ans: The only difference in the Access and Oracle solutions is the format of date constants. Access SQL solution: SELECT OrdNo, OrdDate, Customer.CustNo, CustFirstName, CustLastName FROM OrderTbl, Customer WHERE OrdState = 'CO' AND OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND OrderTbl.CustNo = Customer.CustNo Oracle solution: SELECT OrdNo, OrdDate, Customer.CustNo, CustFirstName, CustLastName FROM OrderTbl, Customer WHERE OrdState = 'CO' AND OrdDate BETWEEN '1-Jan-2004' AND '31-Jan2004' AND OrderTbl.CustNo = Customer.CustNo Access solution with an INNER JOIN operation: SELECT OrdNo, OrdDate, Customer.CustNo, CustFirstName, CustLastName FROM OrderTbl INNER JOIN Customer ON OrderTbl.CustNo = Customer.CustNo WHERE OrdState = 'CO' AND OrdDate BETWEEN #1/1/2004# AND #1/31/2004# Oracle 9i solution with an INNER JOIN operation: SELECT OrdNo, OrdDate, Customer.CustNo, CustFirstName, CustLastName FROM OrderTbl INNER JOIN Customer ON OrderTbl.CustNo = Customer.CustNo WHERE OrdState = 'CO' AND OrdDate BETWEEN '1-Jan-2004' AND '31-Jan2004' 12. List the order number, order date, customer number, and customer name (first and last) of orders placed in January 2004 placed by Colorado customers (CustState) but sent to Washington recipients (OrdState). Ans: The only difference in the Access and Oracle solutions is the format of date constants. Access SQL solution: SELECT OrdNo, OrdDate, Customer.CustNo, CustFirstName, CustLastName FROM OrderTbl, Customer WHERE OrdState = 'WA' AND OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND OrderTbl.CustNo = Customer.CustNo AND CustState = 'CO' Oracle SQL solution: SELECT OrdNo, OrdDate, Customer.CustNo, CustFirstName, CustLastName FROM OrderTbl, Customer

WHERE OrdState = 'WA' AND OrdDate BETWEEN '1-Jan-2004' AND '31-Jan2004' AND OrderTbl.CustNo = Customer.CustNo AND CustState = 'CO' 13. List the customer number, name (first and last), and balance of Washington customers who have placed one or more orders in February 2004. Remove duplicate rows from the result. Ans: The only difference in the Access and Oracle solutions is the format of date constants. The DISTINCT keyword is necessary because a customer can place multiple orders in February 2004. Access SQL solution: SELECT DISTINCT Customer.CustNo, CustFirstName, CustLastName, CustBal FROM OrderTbl, Customer WHERE CustState = 'WA' AND OrdDate BETWEEN #2/1/2004# AND #2/29/2004# AND OrderTbl.CustNo = Customer.CustNo Oracle SQL solution: SELECT DISTINCT Customer.CustNo, CustFirstName, CustLastName, CustBal FROM OrderTbl, Customer WHERE CustState = 'WA' AND OrdDate BETWEEN '1-Feb-2004' AND '29-Feb2004' AND OrderTbl.CustNo = Customer.CustNo 14. List the order number, order date, customer number, customer name (first and last), employee number, and employee name (first and last) of January 2004 orders placed by Colorado customers. Ans: The only difference in the Access and Oracle solutions is the format of date constants. Access SQL solution: SELECT OrdNo, OrdDate, Customer.CustNo, CustFirstName, CustLastName, Employee.EmpNo, EmpFirstName, EmpLastName FROM OrderTbl, Customer, Employee WHERE CustState = 'CO' AND OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND OrderTbl.CustNo = Customer.CustNo AND OrderTbl.EmpNo = Employee.EmpNo Oracle SQL solution: SELECT OrdNo, OrdDate, Customer.CustNo, CustFirstName, CustLastName, Employee.EmpNo, EmpFirstName, EmpLastName FROM OrderTbl, Customer, Employee WHERE CustState = 'CO' AND OrdDate BETWEEN '1-Jan-2004' AND '31-Jan2004'

AND OrderTbl.CustNo = Customer.CustNo AND OrderTbl.EmpNo = Employee.EmpNo Access SQL solution with INNER JOIN operations: SELECT OrdNo, OrdDate, Customer.CustNo, CustFirstName, CustLastName, Employee.EmpNo, EmpFirstName, EmpLastName FROM ( OrderTbl INNER JOIN Customer ON OrderTbl.CustNo = Customer.CustNo ) INNER JOIN Employee ON OrderTbl.EmpNo = Employee.EmpNo WHERE CustState = 'CO' AND OrdDate BETWEEN #1/1/2004# AND #1/31/2004# Oracle 9i SQL solution with INNER JOIN operations: SELECT OrdNo, OrdDate, Customer.CustNo, CustFirstName, CustLastName, Employee.EmpNo, EmpFirstName, EmpLastName FROM ( OrderTbl INNER JOIN Customer ON OrderTbl.CustNo = Customer.CustNo ) INNER JOIN Employee ON OrderTbl.EmpNo = Employee.EmpNo WHERE CustState = 'CO' AND OrdDate BETWEEN '1-Jan-2004' AND '31-Jan2004' 15. List the employee number, name (first and last), and phone of employees who have taken orders in January 2004 from customers with balances greater than $300. Remove duplicate rows in the result. Ans: The only difference in the Access and Oracle solutions is the format of date constants. The DISTINCT keyword is necessary because an employee can take multiple orders. Access SQL solution: SELECT DISTINCT Employee.EmpNo, EmpFirstName, EmpLastName, EmpPhone FROM OrderTbl, Customer, Employee WHERE CustBal > 300 AND OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND OrderTbl.CustNo = Customer.CustNo AND OrderTbl.EmpNo = Employee.EmpNo Oracle SQL solution: SELECT DISTINCT Employe e.EmpNo, EmpFirstName, EmpLastName, EmpPhone FROM OrderTbl, Customer, Employee WHERE CustBal > 300 AND OrdDate BETWEEN '1-Jan-2004' AND '31-Jan2004' AND OrderTbl.CustNo = Customer.CustNo AND OrderTbl.EmpNo = Employee.EmpNo

16. List the product number, name, and price of products ordered by customer number C0954327 in January 2004. Remove duplicate products in the result. Ans: The only difference in the Access and Oracle solutions is the format of date constants. The DISTINCT keyword is required because a customer may order the same product across orders. Access SQL solution: SELECT DISTINCT Product.ProdNo, ProdName, ProdPrice FROM OrderTbl, OrdLine, Product WHERE CustNo = 'C0954327' AND OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo Oracle SQL solution: SELECT DISTINCT Product.ProdNo, ProdName, ProdPrice FROM OrderTbl, OrdLine, Product WHERE CustNo = 'C0954327' AND OrdDate BETWEEN '1-Jan-2004' AND '31-Jan-2004' AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo 17. List the customer number, name (first and last), order number, order date, employee number, employee name (first and last), product number, product name, and order cost (OrdLine.Qty * ProdPrice) for products ordered on January 23, 2004, in which the order cost exceeds $150. Ans: The only difference in the Access and Oracle solutions is the format of date constants. The join operator style can also be used with Access SQL. Access SQL solution: SELECT Customer.CustNo, CustFirstName, CustLastName, OrderTbl.OrdNo, OrdDate, Employee.EmpNo, EmpFirstName, EmpLastName, Product.ProdNo, ProdName, ProdPrice*Qty AS OrderCost FROM OrderTbl, OrdLine, Product, Customer, Employee WHERE OrdDate = #1/23/2004# AND ProdPrice*Qty > 150 AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo AND OrderTbl.CustNo = Customer.CustNo AND Employe e.EmpNo = OrderTbl.EmpNo Oracle SQL solution: SELECT Customer.CustNo, CustFirstName, CustLastName, OrderTbl.OrdNo, OrdDate, Employee.EmpNo, EmpFirstName, EmpLastName, Product.ProdNo, ProdName, ProdPrice*Qty AS OrderCost FROM OrderTbl, OrdLine, Product, Customer, Employee WHERE OrdDate = '23-Jan-2004' AND ProdPrice*Qty > 150 AND OrderTbl.OrdNo = OrdLine.OrdNo

AND OrdLine.ProdNo = Product.ProdNo AND OrderTbl.CustNo = Customer.CustNo AND Employee.EmpNo = OrderTbl.EmpNo 18. List the average balance of customers by city. Only include customers residing in Washington state (WA). Ans: SELECT CustCity, AVG(CustBal) AS AvgBal FROM Customer WHERE CustState = 'WA' GROUP BY CustCity 19. List the average balance of customers by city and short zip code (the first five digits of the zip code). Only include customers residing in Washington state (WA). In Microsoft Access, the expression left (CustZip, 5) returns the first five digits of the zip code. In Oracle, the expression substr (CustZip, 1, 5) returns the first five digits. Ans: The Access and Oracle solutions use different functions to extract the first five digits of the zip code. Access SQL solution: SELECT CustCity, left(CustZip, 5) AS ShortZip, AVG(CustBal) AS AvgBal FROM Customer WHERE CustState = 'WA' GROUP BY CustCity, Left(CustZip, 5) Oracle SQL solution: SELECT CustCity, substr(CustZip, 1, 5) AS ShortZip, AVG(CustBal) AS AvgBal FROM Customer WHERE CustState = 'WA' GROUP BY CustCity, substr(CustZip, 1, 5) 20. List the average balance and number of customers by city. Only include customers residing in Washington state (WA). Eliminate cities in the result with less than two customers. Ans: The HAVING clause is necessary to eliminate cities with only one customer. SELECT CustCity, AVG(CustBal) AS AvgBal, COUNT(*) AS NumCustomers FROM Customer WHERE CustState = 'WA' GROUP BY CustCity HAVING COUNT(*) > 1

21. List the number of unique short zip codes and average customer balance by city. Only include customers residing in Washington state (WA). Eliminate cities in the result in which the average balance is less than $100. In Microsoft Access, the expression left (CustZip, 5) returns the first five digits of the zip code. In Oracle, the expression substr (CustZip, 1, 5) returns the first five digits. (Note: this problem requires two SELECT statements in Access SQL or a nested query in the FROM clause see Chapter 9.) Ans: The solution cannot be directly performed in Microsoft Access because DISTINCT cannot be used inside of aggregate functions. However, the same result can be produced in Access by using two SELECT statements as shown below. Oracle SQL solution: SELECT CustCity, COUNT(DISTINCT substr(CustZip, 1, 5)) AS NumShortZips, AVG(CustBal) AS AvgBal FROM Customer WHERE CustState = 'WA' GROUP BY CustCity HAVING AVG(CustBal) > 100 Access SQL solution: the second SELECT statement should be saved as a query with the name Temp3_21. SELECT CustCity, COUNT(*) AS NumShortZips, AVG(CustBal) AS AvgCustBal FROM Temp3_21 GROUP BY CustCity HAVING AVG(CustBal) >= 100; Temp3_21: SELECT DISTINCT CustCity, left(CustZip, 5) AS CustShortZip, CustBal FROM Customer WHERE CustState = 'WA'; 22. List the order number and the total amount of the order for orders on January 23, 2004. The total amount of an order is the sum of the quantity times the product price for each product on the order. Ans: The Product table is necessary because the ProdPrice column is needed in the aggregate expression. The solutions differ by the format of date constants. Access SQL Solution SELECT OrderTbl.OrdNo, SUM(Qty*ProdPrice) AS TotOrdAmt FROM OrderTbl, OrdLine, Product WHERE OrdDate = #1/23/2004# AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo GROUP BY OrderTbl.OrdNo

Oracle SQL Solution SELECT OrderTbl.OrdNo, SUM(Qty*ProdPrice) AS TotOrdAmt FROM OrderTbl, OrdLine, Product WHERE OrdDate = '23-Jan-2004' AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo GROUP BY OrderTbl.OrdNo

23. List the order number, order date, customer name (first and last), and total amount of the order for orders on January 23, 2004. The total amount of an order is the sum of the quantity times the product price for each product on the order. Ans: The OrdDate, CustFirstName, and CustLastName columns must be included in the GROUP BY clause because every column in SELECT that is not an aggregate expression must appear in the GROUP BY clause. The solutions differ by the format of date constants. Access SQL Solution SELECT OrderTbl.OrdNo, OrdDate, CustFirstName, CustLastName, SUM(Qty*ProdPrice) AS TotOrdAmt FROM OrderTbl, OrdLine, Product, Customer WHERE OrdDate = #1/23/2004# AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo AND Customer.CustNo = OrderTbl.CustNo GROUP BY OrderTbl.OrdNo, OrdDate, CustFirstName, CustLastName Oracle SQL Solution SELECT OrderTbl.OrdNo, OrdDate, CustFirstName, CustLastName, SUM(Qty*ProdPrice) AS TotOrdAmt FROM OrderTbl, OrdLine, Product, Customer WHERE OrdDate = '23-Jan-2004' AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo AND Customer.CustNo = OrderTbl.CustNo GROUP BY OrderTbl.OrdNo, OrdDate, CustFirstName, CustLastName 24. List the customer number, customer name (first and last), the sum of the quantity of products ordered, and the total amount of products ordered in January 2004. Only include products in which the product name contains the string Ink Jet or Laser. Include only customers who have ordered more than two Ink Jet or Laser products in January 2004. Ans: The HAVING clause is necessary to eliminate customers with two or less products ordered. Access SQL Solution

SELECT Customer.CustNo, CustFirstName, CustLastName, SUM(Qty) AS ProdQty, SUM(Qty*ProdPrice) AS TotOrdAmt FROM OrderTbl, OrdLine, Product, Customer WHERE OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo AND Customer.CustNo = OrderTbl.CustNo AND (ProdName LIKE '*Ink Jet*' OR ProdName LIKE '*Laser*') GROUP BY Customer.CustNo, CustFirstName, CustLastName HAVING SUM(Qty) > 2 Oracle SQL Solution SELECT Customer.CustNo, CustFirstName, CustLastName, SUM(Qty) AS ProdQty, SUM(Qty*ProdPrice) AS TotOrdAmt FROM OrderTbl, OrdLine, Product, Customer WHERE OrdDate BETWEEN '1-Jan-2004' AND '31-Jan-2004' AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo AND Customer.CustNo = OrderTbl.CustNo AND (ProdName LIKE '%Ink Jet%' OR ProdName LIKE '%Laser%') GROUP BY Customer.CustNo, CustFirstName, CustLastName HAVING SUM(Qty) > 2 25. List the product number, product name, sum of the quantity of products ordered, and total order amount (sum of the product price times the quantity) for orders placed in January 2004. Only include products that have more than five products ordered in January 2004. Sort the result by descending total amount. Ans: The HAVING clause is necessary to eliminate products with five or less products ordered. In the ORDER BY clause, the column number (3) can be used in place of the aggregate expression. Access SQL Solution SELECT Product.ProdNo, ProdName, SUM(Qty) AS ProdQty, SUM(Qty*ProdPrice) AS TotOrdAmt FROM OrderTbl, OrdLine, Product WHERE OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo GROUP BY Product.ProdNo, ProdName HAVING SUM(Qty) > 5 ORDER BY SUM(Qty*ProdPrice) DESC Oracle SQL Solution SELECT Product.ProdNo, ProdName, SUM(Qty) AS ProdQty, SUM(Qty*ProdPrice) AS TotOrdAmt FROM OrderTbl, OrdLine, Product

WHERE OrdDate BETWEEN '1-Jan-2004' AND '31-Jan-2004' AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo GROUP BY Product.ProdNo, ProdName HAVING SUM(Qty) > 5 ORDER BY SUM(Qty*ProdPrice) DESC 26. List the order number, order date, customer number, customer name (first and last), customer state, and shipping state (OrdState) in which the customer state differs from the shipping state. Ans: The not equality comparison (<>) ensures that the customer and shipping state are different. SELECT OrdNo, OrdDate, OrdState, Customer.CustNo, CustFirstName, CustLastName, CustState FROM OrderTbl, Customer WHERE OrdState <> CustState AND OrderTbl.CustNo = Customer.CustNo

27. List the employee number, employee name (first and last), commission rate, supervising employee name (first and last), and commission rate of the supervisor. Ans: The table alias names are necessary to distinguish the copies of the Employee table. SELECT E.EmpNo, E.EmpFirstName, E.EmpLastName, E.EmpCommRate, Supr.EmpFirstName, Supr.EmpLastName, Supr.EmpCommRate FROM Employee E, Employee Supr WHERE E.SupEmpNo = Supr.EmpNo

28. List the employee number, employee name (first and last), and total amount of commissions on orders taken in January 2004. The amount of a commission is the sum of the dollar amount of products ordered times the commission rate of the employee. Ans: The two solutions differ because of the date formats. Access SQL Solution SELECT Employee.EmpNo, EmpFirstName, EmpLastName, SUM(EmpCommRate*Qty*ProdPrice) AS TotCommAmt FROM Employee, OrderTbl, OrdLine, Product WHERE OrderTbl.EmpNo = Employee.EmpNo AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo AND OrdDate BETWEEN #1/1/2004# AND #1/31/2004#

GROUP BY Employee.EmpNo, EmpFirstName, EmpLastName Oracle SQL Solution SELECT Employee.EmpNo, EmpFirstName, EmpLastName, SUM(EmpCommRate*Qty*ProdPrice) AS TotCommAmt FROM Employee, OrderTbl, OrdLine, Product WHERE OrderTbl.EmpNo = Employee.EmpNo AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo AND OrdDate BETWEEN '1-Jan-2004' AND '31-Jan-2004' GROUP BY Employee.EmpNo, EmpFirstName, EmpLastName 29. List the union of the name, street, city, state, and zip of customers and order recipients. You need to use the concatenation function to combine the first and last names so that they can be compared to the order recipient name. In Access SQL, the & symbol is the concatenation function. In Oracle SQL, the || symbol is the concatenation function. Ans: The two solutions differ because of the concatenation function. The columns have been renamed so that the result table has meaningful column names. Access SQL Solution SELECT CustFirstName & ' ' & CustLastName AS PersonName, CustStreet AS Street, CustCity AS City, CustState AS State, CustZip AS Zip FROM Customer UNION SELECT OrdName AS PersonName, OrdStreet AS Street, OrdCity AS City, OrdState AS State, OrdZip AS Zip FROM OrderTbl Oracle SQL Solution SELECT CustFirstName || ' ' || CustLastName AS PersonName, CustStreet AS Street, CustCity AS City, CustState AS State, CustZip AS Zip FROM Customer UNION SELECT OrdName AS PersonName, OrdStreet AS Street, OrdCity AS City, OrdState AS State, OrdZip AS Zip FROM OrderTbl 30. List the first and last name of customers who have the same name (first and last) as an employee. Ans: This problem can be formulated with a join or intersection operation. Join formulation (Access and Oracle) SELECT CustFirstName, CustLastName

FROM Customer, Employee WHERE CustFirstName = EmpFirstName AND CustLastName = EmpLastName Intersection formulation (Oracle) SELECT CustFirstName AS FirstName, CustLastName AS LastName FROM Customer INTERSECT SELECT EmpFirstName AS FirstName, EmpLastName AS LastName FROM Employee

31. List the employee number and name (first and last) of second- level subordinates (subordinates of subordinates) of the employee named Thomas Johnson. Ans: This problem requires two self-joins of the Employee table. The table alias E represents the supervising employee. The table alias Subr1 represents the first level subordinates. The table alias Subr2 represents the second level subordinates. SELECT Subr2.EmpNo, Subr2.EmpFirstName, Subr2.EmpLastName FROM Employee E, Employee Subr1, Employee Subr2 WHERE Subr1.EmpNo = Subr2.SupEmpNo AND Subr1.SupEmpNo = E.EmpNo AND E.EmpFirstName = 'Thomas' AND E.EmpLastName = 'Johnson' 32. List the employee number and name (first and last) of the first- and second- level subordinates of the employee named Thomas Johnson. To distinguish the level of subordinates, include a computed column with subordinate level (1 or 2). Ans: This problem requires a union operation to combine the first level and second level subordinates. In each SELECT statement, the SubLevel column indicates the level. SELECT Subr1.EmpNo, Subr1.EmpFirstName, Subr1.EmpLastName, 1 AS SubLevel FROM Employee E, Employee Subr1 WHERE Subr1.SupEmpNo = E.EmpNo AND E.EmpFirstName = 'Thomas' AND E.EmpLastName = 'Johnson' UNION SELECT Subr2.EmpNo, Subr2.EmpFirstName, Subr2.EmpLastName, 2 AS SubLevel FROM Employee E, Employee Subr1, Employee Subr2 WHERE Subr1.EmpNo = Subr2.SupEmpNo AND Subr1.SupEmpNo = E.EmpNo AND E.EmpFirstName = 'Thomas' AND E.EmpLastName = 'Johnson' 33. Using a mix of the join operator and the cross product styles, list the names (first and last) of customers who have placed orders taken by Amy Tang. Remove duplicate rows in the result. Note that Oracle 8i does not support the join operator style, but Oracle 9i does support the style.

Ans: This statement does not execute in Oracle 8i SQL because Oracle 8i does not support the join operator style. SELECT DISTINCT CustFirstName, CustLastName FROM Employee INNER JOIN OrderTbl ON Employee.EmpNo = OrderTbl.EmpNo, Customer WHERE Customer.CustNo = OrderTbl.CustNo AND EmpFirstName = 'Amy' AND EmpLastName = 'Tang' 34. Using the join operator style, list the product name and the price of all products ordered by Beth Taylor in January 2004. Remo ve duplicate rows from the result. Ans: This statement does not execute in Oracle SQL because Oracle SQL does not support the join operator style. Access SQL Solution SELECT DISTINCT ProdName, ProdPrice FROM ( ( OrdLine INNER JOIN OrderTbl ON OrdLine.OrdNo = OrderTbl.OrdNo ) INNER JOIN Customer ON Customer.CustNo = OrderTbl.CustNo ) INNER JOIN Product ON OrdLine.ProdNo = Product.ProdNo WHERE OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND CustFirstName = 'Beth' AND CustLastName = 'Taylor' Oracle 9i SQL Solution SELECT DISTINCT ProdName, ProdPrice FROM ( ( OrdLine INNER JOIN OrderTbl ON OrdLine.OrdNo = OrderTbl.OrdNo ) INNER JOIN Customer ON Customer.CustNo = OrderTbl.CustNo ) INNER JOIN Product ON OrdLine.ProdNo = Product.ProdNo WHERE OrdDate BETWEEN '1-Jan-2004' AND '31-Jan-2004' AND CustFirstName = 'Beth' AND CustLastName = 'Taylor' 35. For Colorado customers, compute the number of orders placed in January 2004. The result should include the customer number, the last name, and the number of orders placed in January 2004. Ans: Access SQL Solution SELECT Customer.CustNo, CustLastName, COUNT(*) AS NumOrders FROM OrderTbl, Customer WHERE OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND Customer.CustNo = OrderTbl.CustNo GROUP BY Customer.CustNo, CustLastName Oracle SQL Solution SELECT Customer.CustNo, CustLastName, COUNT(*) AS NumOrders

FROM OrderTbl, Customer WHERE OrdDate BETWEEN '1-Jan-2004' AND '31-Jan-2004' AND Customer.CustNo = OrderTbl.CustNo GROUP BY Customer.CustNo, CustLastName 36. For Colorado customers, compute the number of orders placed in January 2004 in which the orders contain products made by Connex. The result should include the customer number, the last name, and the number of orders placed in January 2004. Ans: Access SQL Solution SELECT Customer.CustNo, CustLastName, COUNT(*) AS NumOrders FROM OrderTbl, Customer, OrdLine, Product WHERE OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND OrderTb l.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo AND Customer.CustNo = OrderTbl.CustNo AND ProdMfg = 'Connex' GROUP BY Customer.CustNo, CustLastName Oracle SQL Solution SELECT Customer.CustNo, CustLastName, COUNT(*) AS NumOrders FROM OrderTbl, Customer, OrdLine, Product WHERE OrdDate BETWEEN '1-Jan-2004' AND '31-Jan-2004' AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo AND Customer.CustNo = OrderTbl.CustNo AND ProdMfg = 'Connex' GROUP BY Customer.CustNo, CustLastName 37. For each employee with a commission rate of less than 0.04, compute the number of orders taken in January 2004. The result should include the employee number, the employee last name, and the number of orders taken.

Ans: Access SQL Solution SELECT Employee.EmpNo, EmpLastName, COUNT(*) AS NumOrders FROM OrderTbl, Employee WHERE OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND Employee.EmpNo = Employee.EmpNo AND EmpCommRate < 0.04 GROUP BY Employee.EmpNo, EmpLastName

Oracle SQL Solution SELECT Employee.EmpNo, EmpLastName, COUNT(*) AS NumOrders FROM OrderTbl, Employee WHERE OrdDate BETWEEN '1-Jan-2004' AND '31-Jan-2004' AND Employee.EmpNo = Employee.EmpNo AND EmpCommRate < 0.04 GROUP BY Employee.EmpNo, EmpLastName 38. For each employee with a commission rate of greater than 0.03, compute the total commission earned from orders taken in January 2004. The total commission earned is the total order amount times the commission rate. The result should include the employee number, the employee last name, and the total commission earned. Ans: Access SQL Solution SELECT Employee.EmpNo, EmpLastName, SUM(EmpCommRate * Qty * ProdPrice) AS TotCommEarned FROM OrderTbl, Employee, OrdLine, Product WHERE OrdDate BETWEEN #1/1/2004# AND #1/31/2004# AND Employee.EmpNo = Employee.EmpNo AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo AND EmpCommRate > 0.03 GROUP BY Employee.EmpNo, EmpLastName Oracle SQL Solution SELECT Employee.EmpNo, EmpLastName, SUM(EmpCommRate * Qty * ProdPrice) AS TotCommEarned FROM OrderTbl, Employee, OrdLine, Product WHERE OrdDate BETWEEN '1-Jan-2004' AND '31-Jan-2004' AND Employee.EmpNo = Employee.EmpNo AND OrderTbl.OrdNo = OrdLine.OrdNo AND OrdLine.ProdNo = Product.ProdNo AND EmpCommRate > 0.03 GROUP BY Employee.EmpNo, EmpLastName Part 2: INSERT, UPDATE, and DELETE statements 1. Insert yourself as a new row in the Customer table. Ans: INSERT INTO Customer (CustNo, CustFirstName, CustLastName, CustStreet, CustCity, CustState, CustZip, CustBal) VALUES ('C9999999', 'Michael', 'Mannino', '123 Any Street', 'MyTown', 'CO',

'80217-0211', 500) 2. Insert your roommate, best friend, or significant other as a new row in the Employee table. Ans: INSERT INTO Employee ( EmpNo, EmpFirstName, EmpLastName, EmpPhone, EmpCommRate) VALUES ('E9999999', 'Mary', 'Mannino', '(720)543-1234', 0.04) 3. Insert a new OrderTbl row with you as the customer, the person from Problem 2 (Part 2) as the employee, and your choice of values for the other columns of the OrderTbl table. Ans: Access SQL: INSERT INTO OrderTbl ( OrdNo, OrdDate, CustNo, EmpNo, OrdName, OrdStreet, OrdCity, OrdState, OrdZip) VALUES ('O9999999', #1/27/2000#, 'C9999999', 'E9999999', 'Mary Mannino', '123 Any Street', 'MyTown', 'CO', '80217-1121') Oracle SQL: INSERT INTO OrderTbl ( OrdNo, OrdDate, CustNo, EmpNo, OrdName, OrdStreet, OrdCity, OrdState, OrdZip) VALUES ('O9999999', '27-Jan-2000', 'C9999999', 'E9999999', 'Mary Mannino', '123 Any Street', 'MyTown', 'CO', '80217-1121') 4. Insert two rows in the OrdLine table corresponding to the OrderTbl row inserted in Problem 3 (Part 2). Ans: INSERT INTO OrdLine (OrdNo, ProdNo, Qty) VALUES ('O9999999', 'P6677900', 2) INSERT INTO OrdLine (OrdNo, ProdNo, Qty) VALUES ('O9999999', 'P1556678', 2) 5. Increase the price by 10 percent of products containing the words Ink Jet.

Ans: Access SQL: UPDATE Product SET ProdPrice = ProdPrice * 1.1 WHERE ProdName LIKE '*Ink Jet*'

Oracle SQL: UPDATE Product SET ProdPrice = ProdPrice * 1.1 WHERE ProdName LIKE '%Ink Jet%' 6. Change the address (street, city, and zip) of the new row inserted in Problem 1 (Part 2). Ans: UPDATE Customer SET CustStreet = '123 AnyNew Street', CustCity = 'MyNewTown', CustZip = '80227-1321' WHERE CustNo = 'C9999999' 7. Identify an order that respects the rules about deleting referenced rows to delete the rows inserted in Problems 1 to 4 (Part 2)? Ans: The new OrderTbl row must be deleted first. Because deletes cascade from OrderTbl to OrdLine, separate DELETE statements are not necessary for the new rows of the OrdLine table. After deleting the new OrderTbl row, the order does not matter for deleting the new Customer and Employee rows. 8. Delete the new row(s) of the table listed first in the order for Problem 7 (Part 2). Ans: This DELETE statement deletes the new OrderTbl row along with the related OrdLine rows. DELETE FROM OrderTbl WHERE OrdNo = 'O9999999' 9. Delete the new row(s) of the table listed second in the order for Problem 7 (Part 2). Ans: DELETE FROM Customer WHERE CustNo = 'C9999999' 10. Delete the new row(s) of the remaining tables listed in the order for Problem 7 (Part 2). Ans: DELETE FROM Employee WHERE EmpNo = 'E9999999'

S-ar putea să vă placă și