Using Relational Databases and SQL 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 7: Subqueries and Set Operations.
Advertisements

Northwind2003 database 11 Aug Sakila database 11 Aug
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Chapter 6 Set Functions.
Chapter 11 Group Functions
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
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 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 5: 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.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Functions.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 7:
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 Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
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.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 6: Midterm Review.
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.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
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.
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.
Access Queries Agenda 6/16/14 Review Access Project Part 1, answer questions Discuss queries: Turning data stored in a database into information for decision.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries.
CS 122: Lecture 3 Joins (Part 1) Tarik Booker CS 122 California State University, Los Angeles October 7, 2014.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
Lecture 7: Subqueries Tarik Booker California State University, Los Angeles.
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.
CS122 Using Relational Databases and SQL
In this session, you will learn to:
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
MYSQL DEFINITION MySQL, pronounced either "My S-Q-L" or "My Sequel," is an open source relational database management system. It is based on the structure.
Aggregating Data Using Group Functions
Tarik Booker California State University, Los Angeles October 21, 2014
Using Relational Databases and SQL
Group Functions Lab 6.
CS122 Using Relational Databases and SQL
SQL FUNDAMENTALS CDSE Days 2018.
(SQL) Aggregating Data Using Group Functions
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Chapter 11 Functions and Groups
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Aggregating Data Using Group Functions
SQL – Entire Select.
CS122 Using Relational Databases and SQL
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Aggregating Data Using Group Functions
Creating Noninput Items
CS122 Using Relational Databases and SQL
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Presentation transcript:

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

Topics for Today Next week's lab will be an ungraded practice midterm MW Section will take the practice MT on Monday of Week 6, get Lecture 6 on Weds, and take the actual MT on Weds of Week 7 TTH section will take the practice exam on Tuesday of week 6 and take the actual exam on Thursday of week 6 If you miss the midterm, you may take a makeup exam without explanation, but the makeup exam will be much harder than the regular exam.

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)‏ Aggregate Functions

What is a Function? In math: A function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output. The output of a function f corresponding to an input x is denoted by f(x) (read "f of x"). If f(x) = x 2, then if the input is −3, the output is 9, and we may write f(−3) = 9. The input variable(s) are sometimes referred to as the argument(s) of the function.

What is a SQL Function? Portion of built-in code in MySQL used to perform a 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,...] (at least in mysql for Windows) function_name is case insensitive Examples: SELECT LENGTH('STEVEN'); -- output is 6 SELECT NOW(); -- output is, eg, :59:02

Function Tips Don't Try to Memorize Everything Too many functions Every DBMS vendor uses different functions; vendor lock-in 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, not from 0! '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,...)‏ select concat_WS(' ', 'x', 'y') 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)‏ TRIM(str), LTRIM(str), RTRIM(str)‏ More string functions can be found in the ebook or MySQL documentation at dev.mysql.com

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) select date_format(' ', '%m %d %Y') DAY(date)‏, WEEK(date) MONTH(date), YEAR(date)

More Date and Time Functions DAYOFMONTH(date) DAYOFWEEK(date)‏ DAYOFYEAR(date)‏ NOW()‏ TO_DAYS(date)‏ SELECT to_days(' ') Not what you think! More date and time functions can be found at dev.mysql.com

About Dates You can do math on date values, using the INTERVAL keyword select entrydate, adddate(entrydate, interval 10 year) from artists select entrydate, subdate(entrydate, interval 10 year) from artists Valid intervals are DAY, MONTH, YEAR SELECT artistname, DATEDIFF(' :59:59', entrydate) from artists;

Control Flow Functions IF(condition, true_expr, false_expr)‏ select if(M.LastName = 'sanders', 'yes', 'no') from Members M IFNULL(expr1, expr2) If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. select IFNULL(webaddress, "NONE") from artists;

Comparison Functions GREATEST(comma separated list of values)‏ LEAST(comma separated list of values)‏ What happens if any of the values are null?

Numerical Functions ABS(value)‏ CEIL(value), CEILING(value)‏, FLOOR(value) COS(radian), SIN(radian), TAN(radian)‏ PI()‏ 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

Datatype Conversions MYSQL is not strict about data types SELECT T.LengthSeconds/60 FROM Tracks T;

Nested Functions Nested functions are very common! String functions SELECT CONCAT(LEFT(M.FirstName, 1), LEFT(M.LastName, 1)) from members M Date functions SELECT DAY(CURDATE()) Control flow functions IF(grade > 90, 'A', IF(grade > 80, 'B', IF(grade > 70, 'C', 'NC'))) SELECT Lastname, Birthday, IF(YEAR(Birthday) < 1965, "old", IF(YEAR(Birthday) < 1975, "medium", "young")) FROM Members;

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 ArtistName, YEAR(EntryDate) AS Y FROM Artists WHERE Y > 2000; -- OK SELECT ArtistName, YEAR(EntryDate) AS Y FROM Artists WHERE YEAR(entryDate) > 2000;

Aggregate Functions “Aggregate” means “combined” or “composed of many separate parts added together.” Similarities between aggregate and non-aggregate functions: Both take some kind of input Both perform operations on the input Both have a single output. Aggregate/Non-aggregate differences Input to an aggregate function is a set of data

Examples Non-Aggregate Function Example: SELECT UPPER(LastName) FROM Members function runs for each row Aggregate Example: SELECT COUNT(*) FROM Members; all the rows are part of input for one instance of the function

Aggregate Functions Five Main Aggregate Functions COUNT(*), COUNT(expression)‏ AVG(expression)‏ MIN(expression), MAX(expression)‏ SUM(expression)‏

Aggregate Arguments Every aggregate function takes a single argument Argument may be another function, but not another aggregate function Argument may be a CASE statement

Aggregate Arguments Example: SELECT SUM(lengthseconds BETWEEN 60 AND 120) FROM Tracks; Which produces the same result as: SELECT COUNT(TrackNum) FROM Tracks WHERE lengthseconds BETWEEN 60 AND 120; What does the first one do? Why do they produce the same result?

COUNT COUNT(*)‏ Counts the number of rows in a table Excludes NULLs (doesn't count them)‏ -- This query returns 6. SELECT COUNT(*) AS 'Number of Titles' FROM Titles; COUNT(expression)‏ Same as above -- This query also returns 6. SELECT COUNT(titleID) AS 'Number of Titles' FROM Titles;

Examples SELECT count(*) FROM artists SELECT count(artistID) FROM artists SELECT count(WebAddress) FROM artists SELECT count(*) FROM artists where !(WebAddress is null) SELECT count(*) from artists WHERE WebAddress IS NULL -Bad code ahead!-Why does this one return 0? SELECT count(WebAddress) from artists WHERE WebAddress IS NULL

Be careful what you aggregate! Show the number of artists which have recorded in the alternative genre: SELECT count(distinct A.artistID) FROM Artists A JOIN Titles T ON(A.ArtistID = T.ArtistID) WHERE T.Genre = "Alternative"; This one is wrong, because it counts duplicates where an artist has recorded more than one title in the genre: SELECT count(A.artistID) FROM Artists A JOIN Titles T ON(A.ArtistID = T.ArtistID) WHERE T.Genre = "Alternative";

SUM SUM(expression)‏ Sums all the data under expression Excludes NULLs (doesn't count NULL as 0). -- Sums all of the runtimes. SELECT SUM(lengthseconds) AS 'Total Length' FROM Tracks; Select sum(age) from avgdemo;

AVG AVG(expression)‏ Mean Averages all data under expression Excludes NULLs (doesn't count NULL as 0). -- Averages all track lengths that are listed:. SELECT AVG(lengthseconds) FROM tracks

AVG Find the average age in years of all members: SELECT AVG(DATEDIFF(CURDATE(), Birthday))/ FROM Members;

Excluding Data From Aggregation To exclude items from being aggregated, you may use the WHERE clause. Example: Count the number of male members. SELECT COUNT(*) FROM Members WHERE Gender = 'M'; Example: Count the number of female members. SELECT COUNT(*) FROM Members WHERE Gender = 'F';

AVG create table AvgDemo( id int primary key, name varchar(30) not null, sex char, age int); insert into AvgDemo values(1, "Sue", 'f', null); insert into AvgDemo values(2, "Fred", 'm', 20); insert into AvgDemo values(3, "Mary", 'f', 30); insert into AvgDemo values(4, "Marion", null, 40); insert into AvgDemo values(5, "Bob", 'm', 50); insert into AvgDemo values(6, "Jack", 'm', 60); // in a real data base, we wouldn’t use an “age” field // -why not?

AVG What will we get when we run: select avg(age) from avgdemo; Do we need to run this: select avg(age) from avgdemo where age is not null? select avg(age) from avgdemo where sex = ‘f’ select avg(age) from avgdemo where sex is null

MIN and MAX MIN(expression)‏ Returns the minimum value under expression -- Returns the minimum runtime. SELECT MIN(lengthseconds) AS 'Shortest Track' FROM Tracks; MAX(expression)‏ Returns the maximum value under expression -- Returns the maximum runtime. SELECT MAX(lengthseconds) AS 'Longest Track' FROM Tracks;

More Aggregate Functions There are number of statistical aggregate functions: STDDEV_POP, STDDEV_SAMP Example: select stddev_pop(age) from avgdemo; VAR_POP, VAR_SAMP More MySQL specific ones are here.here

Aggregate Function Usage You may use aggregate functions directly in the following clauses: SELECT clause HAVING clause ORDER BY clause

Aggregates and the WHERE Clause The WHERE clause filters data from being aggregated (it filters the table in the FROM clause) In other words, the WHERE clause filters data BEFORE it is aggregated (counted, summed, etc.) Therefore, you MAY NOT use aggregate functions directly in the WHERE clause We will see several ways to deal with this soon

Aggregates and the WHERE Clause -- This causes an ERROR!!! SELECT TrackTitle FROM Tracks WHERE LengthSeconds > AVG(LengthSeconds); Would try to filter out everything that was greater than a value it had not calculated yet

How Aggregate Queries Work Think of any aggregate query as a two-step process Process #1: Filter things you don’t want to aggregate Process #2: Aggregate (COUNT, AVG, SUM, etc.) And because the WHERE clause operates in the first process, you CANNOT use aggregate functions in the WHERE clause