SQL Oracle PL/SQL. Select SELECT column1, column2,...columnN FROM table_name WHERE condition; SELECT column1, column2,...columnN FROM table_name WHERE.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Advertisements

Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
Using Single-Row Functions to Customize Output
Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Ch. 3 Single-Row Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Single-Row Functions. SQL Functions Functions are a very powerful feature of SQL and can be used to do the following: Perform calculations on data Modify.
3-1 Copyright  Oracle Corporation, All rights reserved. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
Oracle FUNCTIONS. Comment ScreenShot (in 10g) General Example of null Foreign Key: create table deptcs( deptno NUMBER(4) primary key, hiredate DATE,
SQL Training ORACLE SQL Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Lesson Objectives Write SQL Statements using:
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
2-1 Copyright © Oracle Corporation, All rights reserved. Character Strings and Dates Character strings and date values are enclosed in single quotation.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value.
MIT5314: Database ApplicationsSlide # 1 More SQL Dr. Peeter KirsFall, 2003 More SQL (With a little more on Database Design)
3 Copyright © 2004, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
Single – Row Functions. Objectives After completing this lesson, you should be able to do the following:  Describe various types of functions available.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
Single Row Functions Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number, date, general and conversion.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
6 Copyright © 2006, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
2-1 Copyright © Oracle Corporation, All rights reserved. Using the NULL Conditions Test for nulls with the IS NULL operator. SELECT last_name, manager_id.
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
Single Row Functions. Objectives –Use character, number, and date functions –Use conversion functions –Describe types of single row functions in SQL.
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.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
1 Creating and Maintaining Database Objects Part 1 Database Systems.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
INTRODUCTION TO SQL Chapter SELECT * FROM teacher WHERE INSTR (subject_id, ‘&1’)= 4 AND LOWER (subject_id) LIKE ‘HST%’ ; When prompted for the.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
Copyright س Oracle Corporation, All rights reserved. 3 Single-Row Functions.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
3 第三讲 Single-Row Functions. Objectives After completing this lesson, you should be able to do the following: Describe various types of functions available.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
SQL Functions. SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full.
Built-in SQL Functions. 2 Type of Functions Character Functions returning character values returning numeric values Numeric Functions Date Functions Conversion.
Using Single-Row Functions to Customize Output
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Oracle & SQL. Oracle Data Types Character Data Types: Char(2) Varchar (20) Clob: large character string as long as 4GB Bolb and bfile: large amount of.
3 Copyright © 2007, Oracle. All rights reserved. Substitution Variables ra Oly l&On nase lce Int erU.
4/2/16. Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running.
+, -, *, / Mathematical operators ABS(value) Returns the absolute value SIGN(value) Returns the sign of a value, such as 1, -1, 0 MOD(value,divisor) Returns.
3 Copyright © 2004, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
Defining a Column Alias
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
Single Row Functions Part I Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number and date SQL functions.
Open Source Server Side Scripting MySQL Functions
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Restricting and Sorting Data
Chapter Nine Data Manipulation Language (DML) Functions
Computations Done on table data
Using Single-Row Functions to Customize Output
Index Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23,
Restricting and Sorting Data
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

SQL Oracle PL/SQL

Select SELECT column1, column2,...columnN FROM table_name WHERE condition; SELECT column1, column2,...columnN FROM table_name WHERE condition;

WHERE Clause SELECT phone FROM customer WHERE customer_id = 300; = Is equal to <>, != Is not equal to < Less than > Greater than <= Is less than or equal to >= Is greater than or equal to SELECT phone FROM customer WHERE customer_id = 300; = Is equal to <>, != Is not equal to < Less than > Greater than <= Is less than or equal to >= Is greater than or equal to

Logical Operators AND OR NOT SELECT first_name, last_name, salary FROM sales_person WHERE salary >= AND salary <= 40000; AND OR NOT SELECT first_name, last_name, salary FROM sales_person WHERE salary >= AND salary <= 40000;

Nesting Logical Operators 1. NOT 2. AND 3. OR SELECT first_name, last_name, state, salary FROM sales_person WHERE salary >= AND salary <= OR NOT (state = 'VA‘) 1. NOT 2. AND 3. OR SELECT first_name, last_name, state, salary FROM sales_person WHERE salary >= AND salary <= OR NOT (state = 'VA‘)

Other Comparison Keywords LIKE IN BETWEEN…AND IS NULL LIKE IN BETWEEN…AND IS NULL

SELECT product_name FROM product WHERE product_name LIKE 'C%'; SELECT product_name FROM product WHERE product_name LIKE '_e%'; SELECT product_name FROM product WHERE product_name LIKE 'C%'; SELECT product_name FROM product WHERE product_name LIKE '_e%'; LIKE

BETWEEN...AND SELECT first_name, last_name, salary FROM sales_person WHERE salary BETWEEN AND 40000; SELECT first_name, last_name, salary FROM sales_person WHERE salary BETWEEN AND 40000;

IN SELECT first_name, last_name FROM sales_person WHERE state IN ('VA', 'MD'); SELECT first_name, last_name FROM sales_person WHERE state IN ('VA', 'MD');

IS NULL SELECT first_name, last_name FROM sales_person WHERE commission IS NULL; SELECT first_name, last_name FROM sales_person WHERE commission IS NULL;

Order by SELECT salary, first_name FROM sales_person ORDER BY salary, first_name; SELECT salary, first_name FROM sales_person ORDER BY 1, 2 DESC; SELECT salary, first_name FROM sales_person ORDER BY salary, first_name; SELECT salary, first_name FROM sales_person ORDER BY 1, 2 DESC;

Expressions in a SELECT Statement || Concatenate or append - Subtract + Add / Divide * Multiply ( ) Parentheses are used to group expressions. || Concatenate or append - Subtract + Add / Divide * Multiply ( ) Parentheses are used to group expressions.

|| SELECT first_name || ' ' || last_name FROM sales_person; SELECT first_name || ' ' || last_name fulln FROM sales_person; SELECT first_name || ' ' || last_name FROM sales_person; SELECT first_name || ' ' || last_name fulln FROM sales_person;

Expressions in the WHERE Clause SELECT product_name, unit_price, unit_price*1.1 FROM product WHERE unit_price *1.1>45; SELECT product_name, unit_price, unit_price*1.1 AS new_salary FROM product WHERE new_salary > 45; SELECT product_name, unit_price, unit_price*1.1 FROM product WHERE unit_price *1.1>45; SELECT product_name, unit_price, unit_price*1.1 AS new_salary FROM product WHERE new_salary > 45;

Expressions in the ORDER BY Clause SELECT product_name, unit_price, unit_price*1.1 FROM product WHERE unit_price*1.1 > 45 ORDER BY unit_price * 1.1; SELECT product_name, unit_price, unit_price*1.1 new_price FROM product WHERE unit_price*1.1 > 45 ORDER BY new_price DESC; SELECT product_name, unit_price, unit_price*1.1 FROM product WHERE unit_price*1.1 > 45 ORDER BY unit_price * 1.1; SELECT product_name, unit_price, unit_price*1.1 new_price FROM product WHERE unit_price*1.1 > 45 ORDER BY new_price DESC;

Single Row Functions Dual is a dummy table Oracle provides to help us perform ad-hoc queries or mathematical calculations. Select 224*12 from dual Dual is a dummy table Oracle provides to help us perform ad-hoc queries or mathematical calculations. Select 224*12 from dual

Single Row Functions There are four types of single row functions. –Numeric Functions –Character Functions –Date Functions –Conversion Functions: NVL, TO_CHAR, TO_NUMBER, TO_DATE. There are four types of single row functions. –Numeric Functions –Character Functions –Date Functions –Conversion Functions: NVL, TO_CHAR, TO_NUMBER, TO_DATE.

Numeric Functions ABS(n) - Absolute value of n CEIL(n) - Integer value that is Greater than or Equal to n FLOOR(n) - Integer value that is Less than or Equal to n ROUND(n, m) - Rounded off value of nup to mdecimal places TRUNC(n, m) - Truncated value of nup to mdecimal places ABS(n) - Absolute value of n CEIL(n) - Integer value that is Greater than or Equal to n FLOOR(n) - Integer value that is Less than or Equal to n ROUND(n, m) - Rounded off value of nup to mdecimal places TRUNC(n, m) - Truncated value of nup to mdecimal places

Character Functions LOWER (text) Returns text with all letters in lowercase UPPER (text) Returns text with all letters in uppercase INITCAP (text) Returns text with all letters in mixed case LTRIM (text,trim_text) Returns text after removing all occurrences of trim_text from the left of text RTRIM (text,trim_text)Returns text after removing all occurrences of trim_text from the right of text SUBSTR (text, m, n) Returns n characters from text starting from the mth position LENGTH (text) Returns the number ofcharacters in text LPAD (text, n,pad_text) Returns text left-padded with pad_text for a length of n characters RPAD (text, n,pad_text) Returns text right-padded with pad_text for a length of n characters LOWER (text) Returns text with all letters in lowercase UPPER (text) Returns text with all letters in uppercase INITCAP (text) Returns text with all letters in mixed case LTRIM (text,trim_text) Returns text after removing all occurrences of trim_text from the left of text RTRIM (text,trim_text)Returns text after removing all occurrences of trim_text from the right of text SUBSTR (text, m, n) Returns n characters from text starting from the mth position LENGTH (text) Returns the number ofcharacters in text LPAD (text, n,pad_text) Returns text left-padded with pad_text for a length of n characters RPAD (text, n,pad_text) Returns text right-padded with pad_text for a length of n characters

Date Functions ADD_MONTHS (d, n) - Returns the date after adding the number, n to the date d MONTHS_BETWEEN(d1,d2) - Returns the number after adding the dates, d1 and d2 ROUND(d, format) - Returns the date rounded off to the nearest century, year,month, date, hour, minute, or second as specifed by theformat. TRUNC(d, format) - Returns the date lesser than or equal to the nearest century,year, month, date, hour, minute, or second as speci.ed by the format. NEXT_DAY(d,week_day) - Returns the nearest date on or after d, that is the same as the week_day specifed. week_day must be a valid format of day of the week. LAST_DAY(d) - Returns the date of the last day in the month with respect to d. Used to determine the number of days remaining in a month. SYSDATE - Returns the current date and time. ADD_MONTHS (d, n) - Returns the date after adding the number, n to the date d MONTHS_BETWEEN(d1,d2) - Returns the number after adding the dates, d1 and d2 ROUND(d, format) - Returns the date rounded off to the nearest century, year,month, date, hour, minute, or second as specifed by theformat. TRUNC(d, format) - Returns the date lesser than or equal to the nearest century,year, month, date, hour, minute, or second as speci.ed by the format. NEXT_DAY(d,week_day) - Returns the nearest date on or after d, that is the same as the week_day specifed. week_day must be a valid format of day of the week. LAST_DAY(d) - Returns the date of the last day in the month with respect to d. Used to determine the number of days remaining in a month. SYSDATE - Returns the current date and time.

Examples ADD_MONTHS('12-JUN-99', 2) 12-AUG-99 MONTHS_BETWEEN('12-AUG-99','12-JUN-99') 2 NEXT_DAY('01-JAN-00','MONDAY') 03-JAN-00 LAST_DAY('01-JAN-00') 31-JAN-00. SELECT SYSDATE FROM dual SELECT ROUND(SYSDATE, 'YEAR') FROM dual; ADD_MONTHS('12-JUN-99', 2) 12-AUG-99 MONTHS_BETWEEN('12-AUG-99','12-JUN-99') 2 NEXT_DAY('01-JAN-00','MONDAY') 03-JAN-00 LAST_DAY('01-JAN-00') 31-JAN-00. SELECT SYSDATE FROM dual SELECT ROUND(SYSDATE, 'YEAR') FROM dual;

Conversion Functions SELECT TO_CHAR(22) FROM DUAL; SELECT TO_DATE('11-JAN-02') FROM DUAL; NVL(val1,val2) – if val1=null replace it with val2 DECODE(val1, val2, val3, val4, val5, val6, default val) SELECT grade, DECODE(grade, 1, 'A', 2, 'B', 3, 'C', 4, 'D', 'E') FROM salary_grade; SELECT TO_CHAR(22) FROM DUAL; SELECT TO_DATE('11-JAN-02') FROM DUAL; NVL(val1,val2) – if val1=null replace it with val2 DECODE(val1, val2, val3, val4, val5, val6, default val) SELECT grade, DECODE(grade, 1, 'A', 2, 'B', 3, 'C', 4, 'D', 'E') FROM salary_grade;

SELECT TO_CHAR(SYSDATE,'Month dd, yyyy') FROM dual; SELECT TO_DATE('01/01/2000', 'mm/dd/yyyy') FROM dual; SELECT salary, commission, salary+NVL(commission, 0) income FROM sales_person; SELECT TO_CHAR(SYSDATE,'Month dd, yyyy') FROM dual; SELECT TO_DATE('01/01/2000', 'mm/dd/yyyy') FROM dual; SELECT salary, commission, salary+NVL(commission, 0) income FROM sales_person;

Group Functions AVG COUNT MAX MIN SUM AVG COUNT MAX MIN SUM

DISTINCT AND ALL SELECT COUNT (DISTINCT product_id) FROM order_items; SELECT SUM(DISTINCT MANAGER_ID) FROM SALES_PERSON SELECT SUM(ALL MANAGER_ID) FROM SALES_PERSON SELECT COUNT (DISTINCT product_id) FROM order_items; SELECT SUM(DISTINCT MANAGER_ID) FROM SALES_PERSON SELECT SUM(ALL MANAGER_ID) FROM SALES_PERSON

Using GROUP BY and HAVING SELECT state, MAX(salary) FROM sales_person GROUP BY state; SELECT state, gender, MAX (salary) FROM sales_person GROUP BY gender, state; SELECT state, MAX(salary) FROM sales_person GROUP BY state; SELECT state, gender, MAX (salary) FROM sales_person GROUP BY gender, state;

SELECT state, gender, MAX (salary) FROM sales_person WHERE MAX (salary) > GROUP BY gender, state; SELECT state, gender, MAX (salary) FROM sales_person GROUP BY gender, state HAVING MAX (salary) > 40000; SELECT state, gender, MAX (salary) FROM sales_person WHERE MAX (salary) > GROUP BY gender, state; SELECT state, gender, MAX (salary) FROM sales_person GROUP BY gender, state HAVING MAX (salary) > 40000;