MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Similar documents
Using the Set Operators. Copyright 2004, Oracle. All rights reserved.

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Intermediate SQL: Aggregated Data, Joins and Set Operators

RETRIEVING DATA USING THE SQL SELECT STATEMENT

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Lab # 6. Data Manipulation Language (DML)

Join, Sub queries and set operators

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Retrieving Data from Multiple Tables

Introduction to Oracle9i: SQL

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

Bsc (Hons) Software Engineering. Examinations for / Semester 1. Resit Examinations for BSE/15A/FT & BSE/16A/FT

Aggregate Functions. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Dept. Database Lab (ECOM 4113)

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

EXISTS NOT EXISTS WITH

What Are Group Functions? Reporting Aggregated Data Using the Group Functions. Objectives. Types of Group Functions

Database Programming with SQL

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Oracle Syllabus Course code-r10605 SQL

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Oracle Database: SQL Fundamentals I. Oracle Internal & Oracle Academy Use Only. Volume II Student Guide. D64258GC10 Edition 1.0 January 2010 D65028

KORA. RDBMS Concepts II

ÇALIŞMA TEST SORULARI

Data Manipulation Language

Course Overview. Copyright 2010, Oracle and/or its affiliates. All rights reserved.

Database Programming - Section 18. Instructor Guide

Database Programming with PL/SQL

Using DbVisualizer Variables

RDBMS- Day 4. Grouped results Relational algebra Joins Sub queries. In today s session we will discuss about the concept of sub queries.

Querying Data with Transact-SQL

Conversion Functions

Querying Data with Transact SQL

Objectives. After completing this lesson, you should be able to do the following:

Querying Data with Transact-SQL

Oracle Database: Introduction to SQL Ed 2

Oracle Database: SQL and PL/SQL Fundamentals NEW

School of Computing, Engineering and Information Sciences University of Northumbria. Set Operations

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Advance Database Systems. Joining Concepts in Advanced SQL Lecture# 4

HR Database. Sample Output from TechWriter 2007 for Databases

5. Single-row function

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

Oracle Database 11g: SQL and PL/SQL Fundamentals

CS 275 Winter 2011 Problem Set 3

Full file at

normalization are being violated o Apply the rule of Third Normal Form to resolve a violation in the model

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Creating Other Schema Objects

Introduction to Computer Science and Business

Chapter 3B Objectives. Relational Set Operators. Relational Set Operators. Relational Algebra Operations

School of Computing and Information Technology. Examination Paper Autumn Session 2017

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

Database Programming with SQL

Oracle Database 11g: SQL Fundamentals I

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Oracle Database SQL Basics

Table of Contents. Oracle SQL PL/SQL Training Courses

Join (SQL) - Wikipedia, the free encyclopedia

SQL Structured Query Language Introduction

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

Oracle 1Z Oracle Database 11g : SQL Fundamentals I. Download Full Version :

GIFT Department of Computing Science. CS-217/224: Database Systems. Lab-5 Manual. Displaying Data from Multiple Tables - SQL Joins

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Reporting Aggregated Data Using the Group Functions. Copyright 2004, Oracle. All rights reserved.

SELF TEST. List the Capabilities of SQL SELECT Statements

Database Programming with SQL

Oracle Database 11g: SQL Fundamentals I

Introductory SQL SQL Joins: Viewing Relationships Pg 1

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

Assignment Grading Rubric

ORACLE CERTIFIED ASSOCIATE ORACLE DATABASE 11g ADMINISTRATOR

Querying Microsoft SQL Server 2014

SQL functions fit into two broad categories: Data definition language Data manipulation language

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

SQL Data Query Language

Introduction to SQL Server 2005/2008 and Transact SQL

Database Design. 2-3 Entity Relationship Modeling and ERDs. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

Testing Masters Technologies

[AVNICF-MCSASQL2012]: NICF - Microsoft Certified Solutions Associate (MCSA): SQL Server 2012

Oracle Database 10g: Introduction to SQL

Database Programming with PL/SQL

Transcription:

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