Using CASE Value expression

Slides:



Advertisements
Similar presentations
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
Advertisements

Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
Ceng 356-Lab1. Objectives After completing this lesson, you should be able to do the following: Get Familiar with the development environment List the.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions.
SINGLE-ROW FUNCTIONS Lecture 9. SQL Functions Functions are very powerful feature of SQL and can be used to do the following:  Perform a calculation.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
27 Oktober 2015BASIS DATA I/2009-GENAP1 SQL SELECT STATEMENT BASIS DATA I/2009-GENAP Oleh Satrio Agung Wicaksono, S.Kom., M.Kom.
Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
Oracle 11g DATABASE DEVELOPMENT LAB2. Chapter- 2  These commands, which could be issued from SQL*Plus or SQL Developer,  will make it possible to log.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Chapter 13 Triggers. Trigger Overview A trigger is a program unit that is executed (fired) due to an event Event such as updating tables, deleting data.
Rules of Precedence The rules of precedence determine the order in which expressions are evaluated and calculated. The next table lists the default order.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Copyright © 2004, Oracle. All rights reserved. M ANIPULATING D ATA.
Chapter 13 Triggers. Trigger Overview A trigger is a program unit that is executed (fired) due to an event Event such as updating tables, deleting data.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
Using Subqueries to Solve Queries
Restricting and Sorting Data
Retrieving Data Using the SQL SELECT Statement
Writing Basic SQL SELECT Statements
Basic select statement
Using Subqueries to Solve Queries
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Restricting and Sorting Data
Chapter 2 Views.
“Manipulating Data” Lecture 6.
“Manipulating Data” Lecture 6.
Manipulating Data.
Using Subqueries to Solve Queries
Writing Basic SQL SELECT Statements
Chapter 2 Views.
Restricting and Sorting Data
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Chapter 1 Introduction.
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Manipulating Data Lesson 3.
Including Constraints
Restricting and Sorting Data
Presentation transcript:

Using CASE Value expression Chapter 7 Using CASE Value expression

CASE Value expression A CASE value expression allows you to set up a series of conditions that modify specified values returned by your SQL statement You can change the way a value is represented or calculate a new value Each value is modified according to the condition specified within the CASE expression

CASE Value expression Syntax Using CASE with select statement SELECT column name1, …, CASE [ expression] WHEN condition1 THEN result1 WHEN condition2 THEN result2 ..[ELSE result] END [as expression] FROM table name;

Example 1 Represent the names for all employees and determine if its low, high or medium according to the following table Salary Level < 3000 Low Between 3000 and 7000 Medium 7000 high

Example 2 SELECT first_name, Last_name, CASE WHEN salary < 3000 THEN 'Low' WHEN salary BETWEEN 3000 AND 7000 THEN 'Medium' WHEN salary > 7000 THEN 'High' ELSE 'N/A‘ END as “salary Level” FROM employees;

Example 2 SELECT last_name, employee_id, CASE department_id WHEN 10 THEN 'Accounting' WHEN 20 THEN 'Research' WHEN 30 THEN 'Sales' WHEN 40 THEN 'Operations' ELSE 'Unknown' END as "department name" FROM employees ORDER BY last_name;

Using CASE with UPDATE statements Another handy use for the CASE value expression is in the SET clause of an UPDATE statement. For example, suppose you want to increase the salary for employees based on their job id

Using CASE with UPDATE statements Update employees Set salary = Case job_id When ‘AD_PRES’ then salary+100 When ‘AD_VP’ then salary+200 Else salary End;