Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Functions.

Slides:



Advertisements
Similar presentations
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Advertisements

Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
Northwind2003 database 11 Aug Sakila database 11 Aug
Database Systems: Design, Implementation, and Management Tenth Edition
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Chapter 6 Set Functions.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
Introduction to Structured Query Language (SQL)
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lab 2: Single-Table Selections.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Midterm Review.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Subqueries and Set Operations.
Introduction to Python
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Functions.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Strings PART II STRING$ AND SPACE$. Create strings of specified number String$ creates string of specified character Space$ creates string of spaces Example:
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 5: Functions.
U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay.
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.
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.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 6: Midterm Review.
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.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Chapter 11 Functions and Groups Part C. SQL Copyright 2005 Radian Publishing Co.
Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
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 [ ],...]
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.
String and General Procedures. Answer to the last question of Exam 1 Start Get a number Divide the Number By 2 Is the quotient Equal to 1? Print The Remain.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
 CONACT UC:  Magnific training   
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
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.
In this session, you will learn to:
Open Source Server Side Scripting MySQL Functions
Writing Basic SQL SELECT Statements
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Tarik Booker California State University, Los Angeles October 21, 2014
Chapter 9 SQL and RDBMS Part C. SQL
Built-in Functions. Usage of Wildcards
SQL DATE/TIME FUNCTIONS
CS122 Using Relational Databases and SQL
Writing Basic SQL SELECT Statements
Chapter 11 Functions and Groups
Single-Row Functions Lecture 9.
Writing Basic SQL SELECT Statements
Reporting Aggregated Data Using the Group Functions
Contents Preface I Introduction Lesson Objectives I-2
Lecture 5 SQL FUNCTIONS.
Presentation transcript:

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Functions

Topics for Today Functions String Functions (Pages 117 – 120)‏ Date and Time Functions (Pages 120 – 123)‏ Control Flow Functions (Page 125)‏ Comparison Functions (Pages 125 – 126)‏ Numerical Functions (Not in book)‏

Next Tuesday UNION [ALL | DISTINCT] Review topics for midterm Slightly modified movie archive database Talk about final exam database a little bit

What is a Function? Portion of prewritten code in MySQL used to perform a very specific task A function in SQL consists of: Zero or more input values A named function identifier One output value Functions in SQL are useful for: Formatting and extracting data Mathematical computing, converting data

Function Syntax Syntax: function_name(argument_list)‏ argument_list = [arg1, arg2, arg3,...] function_name is case insensitive Examples: SELECT LENGTH('STEVEN'); -- output is 6 SELECT SIN( ); -- output is 2.6e-6 SELECT CURDATE(); -- output is

Function Tips Don't Try to Memorize Everything Too many functions Every DBMS vendor uses different functions Look them up in the book, on the internet, etc. Get Parentheses Right Type in parentheses first, then fill in the arguments Nested Functions Type each function in separately in notepad When done, substitute nested functions back in

Function Usage You can use functions in most SQL clauses SELECT clause (yes, and very common) FROM clause (no) ON clause (yes) WHERE clause (yes, and very common) GROUP BY clause (yes) HAVING clause (yes) ORDER BY clause (yes)

Strings and String Functions Unlike strings in Java, C, C++, and C#, SQL strings use indexes that start from 1 's t e v e n' (SQL indexes) (C++ indexes) To extract 'even' from 'steven' we must start at position 3 and extract 4 characters SELECT SUBSTRING('steven', 3, 4)

String Functions CHAR_LENGTH(str), LENGTH(str)‏‏ CONCAT(str1, str2,...)‏ CONCAT_WS(separator, str1, str2,...)‏ FORMAT(number, decimal_places)‏ LEFT(str, len), RIGHT(str, len)‏ LOCATE(substr, str), LOCATE(substr, str, pos)‏ LOWER(str), UPPER(str)‏

More String Functions SUBSTRING(str, pos, len)‏ SUBSTRING_INDEX(str, delim, count)‏ TRIM(str), LTRIM(str), RTRIM(str)‏ More string functions can be found here.here.

About Dates You can do math on date values, using the INTERVAL keyword SELECT ' ' + INTERVAL 10 YEAR; SELECT ' ' - INTERVAL 10 YEAR; Valid intervals are: DAY MONTH YEAR

About Date Times Time information can be appended to a date in the following way (place a space between the date and the time): ' :50:15' April 19, 1970 at 6:50:15 am You can do math on date times as well: SELECT ' :50:15' + INTERVAL 10 MINUTE; Extra valid intervals for time include: HOUR, MINUTE and SECOND

More About Interval Math You can chain interval additions and subtractions SELECT ' :50:15' + INTERVAL 10 MINUTE + INTERVAL 1 YEAR;

Date and Time Functions CURDATE()‏ DATEDIFF(date1, date2)‏ DATE_ADD(date, INTERVAL value type)‏ DATE_SUB(date, INTERVAL value type)‏ DATE_FORMAT(date, format_string) DAY(date)‏, WEEK(date) MONTH(date), YEAR(date)

More Date and Time Functions DAYOFMONTH(date) DAYOFWEEK(date)‏ DAYOFYEAR(date)‏ LAST_DAY(date)‏ NOW()‏ TO_DAYS(date)‏ More date and time functions can be found here.here

Control Flow Functions IF(condition, true_expr, false_expr)‏ IFNULL(expr1, expr2)‏ CASE WHEN expr1 IS NULL THEN expr2 ELSE expr1 END NULLIF(expr1, expr2)‏ CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END More information on control flow functions can be found here.here

Comparison Functions GREATEST(comma separated list of values)‏ LEAST(comma separated list of values)‏

Numerical Functions ABS(value)‏ CEIL(value), CEILING(value)‏ COS(radian), SIN(radian), TAN(radian)‏ DEGREES(radian), RADIANS(degree), PI()‏ FLOOR(value)‏ POW(X, Y), SQRT(X)‏ ROUND(value), ROUND(value, decimal_places)‏ TRUNCATE(value, decimal_places)‏

Format Versus Truncate FORMAT rounds, TRUNCATE does not SELECT FORMAT( , 4) Returns SELECT TRUNCATE( , 4) Returns FORMAT is a string function, and returns a string; TRUNCATE is a mathematical function, and returns a number

Numerical Functions More numerical functions can be found here.here

Nested Functions Nested functions are very common! String functions CONCAT(LEFT(FirstName, 1), LEFT(LastName, 1)) Date functions DAY(CURDATE()) Control flow functions IF(grade > 90, 'A', IF(grade > 80, 'B', IF(grade > 70, 'C', 'NC')))

Functions and Column Aliases Column aliases can be used in any of the following clauses GROUP BY HAVING (MySQL yes, PostgreSQL no) ORDER BY You cannot refer to column aliases in the following clauses SELECT FROM WHERE

Functions and Column Aliases Therefore, if you have a long function expression and want to use it in the WHERE clause too, using a column alias won’t work You must manually duplicate the expression (by copying and pasting it) into the WHERE clause

Functions and Column Aliases Example: -- ERROR!!! SELECT Title, YEAR(ReleaseDate) AS Y FROM Movies WHERE Y > 2000; -- OK SELECT Title, YEAR(ReleaseDate) AS Y FROM Movies WHERE YEAR(ReleaseDate) > 2000;