Database Programming with SQL

Similar documents
Database Programming with SQL

Database Programming with SQL

Database Programming with SQL

Database Programming with SQL

Database Programming with SQL

Database Programming with SQL

Cartesian Product and the Join Operations. Copyright 2008, Oracle. All rights reserved.

Introduction to Relational Database Concepts. Copyright 2011, Oracle. All rights reserved.

Conversion Functions

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

Database Programming with SQL

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.

DEFAULT Values, MERGE, and Multi-Table Inserts. Copyright 2009, Oracle. All rights reserved.

Database Programming with SQL

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

Recursive Common Table Expressions in Oracle Database 11g Release 2. Iggy Fernandez Database Specialists Session #303

Introduction to Oracle

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

Architecture. Architecture. Introduction to Oracle 10g Express Edition. Help

Database Programming with PL/SQL

RETRIEVING DATA USING THE SQL SELECT STATEMENT

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

Database Programming with SQL 5-1 Conversion Functions. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Introduction to Oracle9i: SQL

EXISTS NOT EXISTS WITH

Join, Sub queries and set operators

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

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

Database Programming with PL/SQL

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

KORA. RDBMS Concepts II

Database Programming with PL/SQL

Database Programming with PL/SQL

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

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

Oracle Database SQL Basics

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

DEC Computer Technology LESSON 6: DATABASES AND WEB SEARCH ENGINES

Course Outline and Objectives: Database Programming with SQL

Database Programming with PL/SQL

Database Foundations. 5-1 Mapping Entities and Attributes. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Introduction to Explicit Cursors. Copyright 2008, Oracle. All rights reserved.

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

Database Programming with PL/SQL

Database Programming with SQL

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

Exam Questions 1Z0-051

Data Manipulation Language

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

Oracle. Exam Questions 1Z Oracle Database 12c SQL Fundamentals. Version:Demo

Database Programming - Section 3. Instructor Guide

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

Oracle MOOC: SQL Fundamentals

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

Introduction to Computer Science and Business

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

Creating Other Schema Objects

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

Database Programming - Section 10. Instructor Guide

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

RMOUG Training Days 2018

20761 Querying Data with Transact SQL

Three types of sub queries are supported in SQL are Scalar, Row and Table sub queries.

Intermediate SQL: Aggregated Data, Joins and Set Operators

Database Programming with PL/SQL

SYSTEM CODE COURSE NAME DESCRIPTION SEM

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

Oracle Database 11g: SQL Fundamentals I COPYRIGHTED MATERIAL. PArt

Database Foundations. 6-1 Introduction to Oracle Application Express. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Assignment Grading Rubric

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

Database Programming with PL/SQL

5. Single-row function

Getting Information from a Table

ITDUMPS QUESTION & ANSWER. Accurate study guides, High passing rate! IT dumps provides update free of charge in one year!

Database Programming with PL/SQL

Graphical Joins in More Detail

Oracle 1Z0-071 Exam Questions and Answers (PDF) Oracle 1Z0-071 Exam Questions 1Z0-071 BrainDumps

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

Real Application Security Administration

Database Programming - Section 18. Instructor Guide

Database Design. 9-2 Basic Mapping: The Transformation Process. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

A. It executes successfully and displays rows in the descending order of PROMO_CATEGORY.

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

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Basic Mapping: The Transformation Process. Copyright 2011, Oracle. All rights reserved.

You can use Dreamweaver to build master and detail Web pages, which

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

CSE 530A. Inheritance and Partitioning. Washington University Fall 2013

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

What do you mean the Oracle Optimizer won't use my Index? John Mullins

COMM 391. Objectives. Introduction to Microsoft Access. What is in an Access database file? Introduction to Microsoft Access 2010

Database Management Systems,

Database Programming with SQL

Monitoring and Resolving Lock Conflicts. Copyright 2004, Oracle. All rights reserved.

Database Programming with SQL

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

Lab Assignment 9 CIS 208A PL/SQL Programming and SQL

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

Transcription:

Database Programming with SQL 5-3

Objectives This lesson covers the following objectives: Compare and contrast the DECODE and CASE functions Construct and execute a SQL query that correctly uses the DECODE and CASE functions Construct and execute two methods for implementing IF- THEN-ELSE conditional logic 3

Purpose Being able to make decisions is essential in data modeling. Modelers have to decide which business functions need to be modeled and which do not. The data-modeling process requires that designers analyze information to identify entities, resolve relationships, and select attributes. A typical decision could be: IF a business needs to track data over time, THEN time may need to be an entity or ELSE time should be an attribute. 4

How Functions are Evaluated This decision-making process in programming is not much different from the process that we use in everyday life. Think of the last time you had to make an if-then-else kind of decision. IF I get my homework done before 9:00 p.m., I can watch television, ELSE I can't watch television. In SQL, these kinds of choices involve conditional-processing methods. Knowing how to use conditional processing makes decision making to get the data you want easier. 5

The two conditional expressions are CASE and DECODE. You have already studied NULLIF, which is logically equivalent to the CASE expression in that CASE compares two expressions. NULIF compares two expressions, and if the two expressions are equal, then return null; if they are not equal, then return the first expression. 6

There are two sets of commands or syntax that can be used to write SQL statements: ANSI/ISO SQL 99 compliant standard statements Oracle proprietary statements The two sets of syntax are very similar, but there are a few differences. In this course, you will learn to use both sets of SQL statements, but the use of ANSI/ISO SQL 99 syntax is recommended. 7

CASE and DECODE are examples of one of these differences. CASE is an ANSI/ISO 99 SQL 99 compliant statement. DECODE is an Oracle Proprietary statement. Both statements return the same information using different syntax. 8

CASE Expression The CASE expression basically does the work of an IF-THEN- ELSE statement. Data types of the CASE, WHEN, and ELSE expressions must be the same. The syntax for a CASE expression is: CASE expr WHEN comparison_expr1 THEN return_expr1 [WHEN comparison_expr2 THEN return_expr2 WHEN comparison_exprn THEN return_exprn ELSE else_expr] END 9

CASE Syntax The query checks the department_id. IF it is 90, then return 'Management' IF it is 80, then return 'Sales' IF it is 60, then return 'It' ELSE return '' SELECT last_name, CASE department_id WHEN 90 THEN 'Management' WHEN 80 THEN 'Sales' WHEN 60 THEN 'It' ELSE '' END AS "Department" FROM employees; LAST_NAME King Kochhar De Haan Whalen Higgins Gietz Zlotkey Abel Taylor Grant Mourgos Rajs Davies Matos Vargas Hunold Ernst Lorentz Hartstein Fay Department Management Management Management Sales Sales Sales It It It 10

DECODE Expression The DECODE function evaluates an expression in a similar way to the IF-THEN-ELSE logic. DECODE compares an expression to each of the search values. The syntax for DECODE is: DECODE(columnl expression, search1, result1 [, search2, result2,...,] [, default]) If the default value is omitted, a null value is returned where a search value does not match any of the values 11

DECODE Expression Examine the example: SELECT last_name, DECODE(department_id, 90, 'Management', 80, 'Sales', 60, 'It', '') AS "Department" FROM employees; This query returns exactly the same results as the previous CASE example, but using different syntax. LAST_NAME King Kochhar De Haan Whalen Higgins Gietz Zlotkey Abel Taylor Grant Mourgos Rajs Davies Matos Vargas Hunold Ernst Lorentz Hartstein Fay Department Management Management Management Sales Sales Sales It It It 12

Terminology Key terms used in this lesson included: CASE Conditional expression DECODE 13

Summary In this lesson, you should have learned how to: Compare and contrast the DECODE and CASE functions Construct and execute a SQL query that correctly uses the DECODE and CASE functions Construct and execute two methods for implementing IF- THEN-ELSE conditional logic 14