Technology & Information Management Instructor: Michael Kremer, Ph.D. Class 4 Professional Program: Data Administration and Management MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)
AGENDA 6. Complex Filtering, SQL Expressions 6.1 Complex Filtering of Data 6.2 SQL Expressions 7. Intersect, Difference, and Union of Sets 7.1 Overview of Set Operations 7.2 Intersection 7.3 Difference 7.4 Union
Complex Filtering, SQL Expressions 6.
6.1 COMPLEX FILTERING OF DATA 109 Up to now single-valued criteria against single field (except Between And) Multiple conditions: Against one field Against multiple fields
6.1 COMPLEX FILTERING OF DATA 110 Previous query returns rows where the salary column is 8000 or more but at the same time less than or equal to 10000
6.1 COMPLEX FILTERING OF DATA 111 AND operator requires all search conditions to be true entire search condition is true row is returned OR operator requires only one of the multiple conditions to be true entire search condition is true row is returned Careful when analyzing user requests and translating into SQL code Example: Show all customers living in California AND Arizona Requirement AND actually translates into SQL OR IN operator allows you to list multiple values in a comma separated list without repeating OR operator and column references
6.1 COMPLEX FILTERING OF DATA 112
6.1 COMPLEX FILTERING OF DATA 113
6.1 COMPLEX FILTERING OF DATA 114
6.1 COMPLEX FILTERING OF DATA 115 Now multiple search conditions across multiple columns Connect search conditions with Boolean operators AND, OR, NOT
6.1 COMPLEX FILTERING OF DATA 116 Using AND yields only one record Using OR yields 19 records only one search condition must be true! HR Manager needs to find all employees that were hired after 1/1/1998 and earn more than $8000 or belong to department 100: What exactly does the HR manager want? Hire_date > 1/1/1998 AND at the same time salary > $8000 OR department_id = 100 Hire_date > 1/1/1998 AND either salary > $8000 OR department_id =100
6.1 COMPLEX FILTERING OF DATA 117 To avoid this ambiguity, use parentheses to combine and prioritize search conditions 1 st Condition: Hire_date > 1/1/1998 AND at the same time salary > $8000 OR department_id = 100 2 nd Condition: Hire_date > 1/1/1998 AND either salary > $8000 OR department_id =100
6.1 COMPLEX FILTERING OF DATA 118 Results may be different or not when changing order of precedence Define any number of parenthetical conditions, even nest them Rules for handling parenthetical conditions: But even before SQL processes the order of parentheses, a single search condition is processed in a specific way Order of precedence in WHERE clause
6.1 COMPLEX FILTERING OF DATA 119 For performance reasons, simply ask for less include only columns needed in SELECT and streamline WHERE clause List condition that excludes most rows in WHERE clause first Example: Assume 100, 000 orders in orders table Customer 1001 has placed 105 orders Find all orders of customer 1001 shipped on the same day they were ordered Company ships 92.4% of all orders on the same day!
6.1 COMPLEX FILTERING OF DATA 120
6.2 SQL EXPRESSIONS 121 Complex expression in SQL are common the better the db design the simpler are the queries Every db platform contains procedural environment to supplement non-procedural SQL If logic construct, use DECODE or CASE WHEN Anatomy of a condition: DECODE function:
6.2 SQL EXPRESSIONS 122 1 st arg: Column or expression 2 nd arg: Search value 3 rd arg: If col/expr matches search value Last arg: If none of the search values match
6.2 SQL EXPRESSIONS 123 Comparison uses implicit equal operator Classify salary column: Salary < 2500 Low Salary < 5000 Medium etc.
6.2 SQL EXPRESSIONS 124 Case is an inline comparison similar to Decode Much more flexible not restricted to equal relational operator only Simple CASE statement breaks condition into two pieces: Left side at the top of the CASE statement Right side in each individual WHEN clause Equal relational operator only Searched CASE statement uses entire condition in each WHEN clause allowing for more complex logic including any relational operator.
6.2 SQL EXPRESSIONS 125 Simple CASE statement works exactly like DECODE function Searched CASE statement allows more complex comparison logic
6.2 SQL EXPRESSIONS 126 Searched CASE statement examples
6.2 SQL EXPRESSIONS 127 NULL propagation in expressions
6.2 SQL EXPRESSIONS 128 Full_Salary column is null for those employees having no commission_pct One part of the expression is null entire expression is null Use NVL function to replace null with a specified value, in most cases with a zero
Intersect, Difference, and Union of Sets 7.
7.1 OVERVIEW OF SET OPERATIONS 129 Set subset of data or entire relation or even an empty set Having sets of data, you may want to perform operations Three main operations:
7.2 INTERSECT 130 Intersection Simple sets of numbers Perform set operations: Specific column or columns Entire table Above example shows values that may be stored in: One column (for different records) Different columns (for same record)
7.2 INTERSECT 131 Data from the same datasource Show employees that are sales reps (one set) and intersect them with employees who earn $10000 30 employees who are sales reps and 4 employees who earn $10000 Intersection yields 3 employees who are sales reps and make $10000 Intersection done in WHERE clause using Boolean AND operator
7.2 INTERSECT 132 Data from different datasources Intersect operator returns all rows that are common to multiple queries. Following rules apply: Optional ORDER BY clause after intersect operation Use column names or aliases from the first SELECT statement
7.2 INTERSECT 133 Intersecting employee records with job_history records
7.3 DIFFERENCE 134 Order of sets is important, result will differ if order is changed Set operator for difference is called EXCEPT (standard SQL), in Oracle MINUS Similar rules:
7.3 DIFFERENCE 135 Data from the same datasource Subtract domain of sales reps from employees who earn $10000
7.3 DIFFERENCE 136 Data from different datasources Subtract job_history records from employee records Select employee_id and job_id from employees and subtract corresponding columns of job_history Results displays employees who have never switch jobs
7.4 UNION 137 Most commonly used set operator Combines data from multiple queries and automatically eliminates duplicate rows Rules of Union:
7.4 UNION 138 Data from the same datasource Combine sales reps with employees who earn $10000
7.4 UNION 139 Data from different datasources