Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 5: Functions."— Presentation transcript:

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

2 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.

3 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

4 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.

5 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

6 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, 2008-10-12 11:59:02

7 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

8 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)

9 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' 1 2 3 4 5 6 (SQL indexes) 0 1 2 3 4 5 (C++ indexes) To extract 'even' from 'steven' we must start at position 3 and extract 4 characters SELECT SUBSTRING('steven', 3, 4)

10 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)‏

11 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

12 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('2010-01-31', '%m %d %Y') DAY(date)‏, WEEK(date) MONTH(date), YEAR(date)

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

14 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('2007-12-31 23:59:59', entrydate) from artists;

15 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;

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

17 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)‏

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

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

20 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;

21 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

22 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

23 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;

24 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

25 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

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

27 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

28 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?

29 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;

30 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

31 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";

32 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;

33 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

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

35 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';

36 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?

37 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

38 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;

39 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

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

41 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

42 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

43 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


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

Similar presentations


Ads by Google