CSIS 115 Database Design and Applications for Business Dr. Meg Fryling “Dr. Meg” Fall 2012 @SienaDrMeg #csis115
Agenda Questions? Assignments Review Chapter 2 - SQL Basics “Fun” with iClicker Chapter 2 - SQL Basics Continued
Homework Project Part I Finish Chapter 3 (100-105 only) Was due at the start of class today Finish Chapter 3 (100-105 only) Finish Chapter 5 Reminder: Quiz 1 will be Mon, 9/24 Closed book but you may have a 8.5x11 handwritten “cheat sheet”. Must use your own and turn in with quiz.
Let’s do some review from last week Get those clickers ready!
The industry standard supported by all major DBMS that allows data to be selected, added, updated, and deleted from a relational database is… A) Sequential Query Language (SQL) B) Structured Question Language (SQL) C) Structured Query Language (SQL) D) Relational Question Language (RQL) E) Relational Query Language (RQL)
Which elements are required in an SQL statement SELECT FROM WHERE SELECT and FROM SELECT, FROM, and WHERE Answer: D
In addition to being a data sublanguage, SQL is also a programming language, like Java or C# A) True B) False Answer: FALSE KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall
The SQL keyword SELECT is used to specify the __________ to be listed in the query results. A) Columns B) Rows C) Records D) Tuples E) None of the above Answer: A KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall
A column is also referred to as a field or an attribute A) True B) False Answer: A KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall
Add ORDER BY Update the query below so it sorts the records by OrderNumber and then by Price SELECT * FROM ORDER_ITEM ORDER BY OrderNumber; Last time we ended with this query
Sorting the Results – ORDER BY SELECT * FROM ORDER_ITEM ORDER BY OrderNumber, Price; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall
Which order are the results sorting in? Ascending Descending Sort Order Default Which order are the results sorting in? Ascending Descending Answer: A
Sort Order: Ascending and Descending SELECT * FROM ORDER_ITEM ORDER BY Price DESC, OrderNumber ASC; NOTE: The default sort order is ASC – does not have to be specified. KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall
SQL for Data Retrieval: Logical Operators Multiple matching criteria (conditions) may be specified using… AND Representing an intersection of the data sets OR Representing a union of the data sets
Write a Query Selects all columns from the SKU_DATA table Only include rows for the Water Sports department where the buyer is Nancy Meyers
WHERE Clause Options - AND SELECT * FROM SKU_DATA WHERE Department = 'Water Sports' AND Buyer = 'Nancy Meyers'; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall
Write a Query Selects all columns from the SKU_DATA table Only include rows that are associated with the Camping or the Climbing department
WHERE Clause Options - OR SELECT * FROM SKU_DATA WHERE Department = 'Camping' OR Department = 'Climbing'; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall
How many rows will this query return? SELECT * FROM SKU_DATA WHERE Department = 'Water Sports' AND Buyer = 'Nancy Meyers'; Answer: 2 Just look at data in SKU_DATA table first to answer this question?
How many rows will this query return? SELECT * FROM SKU_DATA WHERE Department = 'Camping' OR Department = 'Climbing'; Answer: 4 Just look at data in SKU_DATA table first to answer this question?
Write a Query That will return all columns from the ORDER_ITEM table Only include records with an extended price greater than or equal to 100 and less than or equal to 200 Only include records for SKU 101100 or SKU 101200 Hint: You will need to use both AND and OR in the WHERE clause
Using both OR and AND SELECT * FROM ORDER_ITEM WHERE ExtendedPrice >= 100 AND ExtendedPrice <= 200 AND SKU = 101100 OR SKU = 101200; Does this return what you would expect?
Remember operator precedence? 3 + 4 × 5 = 23 vs (3 + 4) × 5 = 35 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall
Excel OR() and AND() Functions Recall CSIS114 BI-OLAP Lab =IF(OR(A3<62,C3="snow",C3="rain",B3>70%,AND(D3="Monday",OR(E4="Average",E4="Bad"))),"no","yes")
Using both OR and AND SELECT * FROM ORDER_ITEM WHERE ExtendedPrice >= 100 AND ExtendedPrice <= 200 AND SKU = 101100 OR SKU = 101200; How do you think you can “fix” this query?
Using both OR and AND SELECT * FROM ORDER_ITEM WHERE ExtendedPrice >= 100 AND ExtendedPrice <= 200 AND (SKU = 101100 OR SKU = 101200);
Gotcha When using both OR & AND clauses be sure to use () to clarify what criteria belongs together If not you may get unexpected results! KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall
SQL for Data Retrieval: Inclusion/Exclusion SQL provides a IN/NOT IN statements that allows a user to specify all valid values on one line! SWEET SELECT FieldName FROM TABLE WHERE FieldName IN (X,Y,Z); SELECT FieldName FROM TABLE WHERE FieldName = X OR FieldName = Y OR FieldName = Z; You can use this instead of _______________
SQL for Data Retrieval: The Logical NOT Operator Any criteria statement may be preceded by a NOT operator which is to say that all information will be shown except that information matching the specified criteria SELECT FieldName FROM TABLE WHERE FieldName NOT IN (X, Y, Z);
WHERE Clause Options - IN Update query below so it uses IN clause to return same results SELECT * FROM ORDER_ITEM WHERE ExtendedPrice >= 100 AND ExtendedPrice <= 200 AND (SKU = 101100 OR SKU = 101200);
WHERE Clause Options - IN SELECT * FROM ORDER_ITEM WHERE ExtendedPrice >= 100 AND ExtendedPrice <= 200 AND SKU IN(101100, 101200); Wow, that sure looks cleaner! What if we wanted to return all SKU records that were NOT 101100 or 101200 but had an extended price between 100 and 200?
WHERE Clause Options – NOT IN SELECT * FROM ORDER_ITEM WHERE ExtendedPrice >= 100 AND ExtendedPrice <= 200 AND SKU NOT IN(101100, 101200);
SQL for Data Retrieval: Finding Data in a Range of Values SQL provides a BETWEEN statement that allows a user to specify a minimum and maximum value on one line! SUPER SWEET Note: Will include the minimum and maximum value in the results SELECT FieldName FROM TABLE WHERE FieldName BETWEEN X AND Y;
SQL for Data Retrieval: Finding Data in a Range of Values Update query below so it uses BETWEEN clause to return same results SELECT * FROM ORDER_ITEM WHERE ExtendedPrice >= 100 AND ExtendedPrice <= 200 AND SKU IN(101100, 101200);
WHERE Clause Options – BETWEEN SELECT * FROM ORDER_ITEM WHERE ExtendedPrice BETWEEN 100 AND 200 AND SKU IN(101100, 101200); Huh, that’s awesome!
In-Class Activity Part III SQL Basics In-Class Activity Part III