9780133461909 Appendix B Introduction to SQL

download 9780133461909 Appendix B Introduction to SQL

of 19

Transcript of 9780133461909 Appendix B Introduction to SQL

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    1/19

     

    DB2 Essentials: Understanding DB2 in a Big Data World, ThirdEdition 

    by Raul F. Chong and Clara LiuIBM Press. (c) 2014. Copying Prohibited.

    Reprinted for Balachandar Devaraj, IBM 

    [email protected] 

    Reprinted with permission as a subscription benefit of Books24x7,http://www.books24x7.com/ 

    All rights reserved. Reproduction and/or distribution in whole or in part in electronic,paper or

    other forms without written permission is prohibited. 

    http://www.books24x7.com/

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    2/19

    Appendix B: Introduction to SQL

    Structured Query Language (SQL) was invented by IBM in the 1970s in support of relational databases. SQL allows

    users to retrieve data by expressing what they are looking for without having to indicate how to get to it. The SQL

    database engine is treated as a black box that figures out how to retrieve data the fastest. SQL has become the de-facto

    database language, with thousands of IT professionals using it worldwide. 

    In this Big Data era, surprisingly SQL has not lost its popularity. Recognizing that sharp learning curves to learn new Big

    Data languages and programming paradigms is an inhibitor to technology adoption, several companies have worked to

    develop SQL dialects so that programmers can use their beloved SQL to query Big Data. For example, Hive, donated by

    Facebook to the open source community, has a SQL dialect called HiveQL. HiveQL is a great way to get started with Big

    Data using SQL; however, it lacks in richness and suffers in performance. In 2013 IBM launched “  Big SQL” as part of its

    Big Data platform offering. Big SQL is SQL for Big Data. As it continues to evolve, Big SQL should reach a level of SQL

    support similar to what is offered to standard relational databases. Big SQL is being designed to deliver better

     performance. It gives users the ability to choose whether to run SQL in local mode or in Map-Reduce mode. For example,

    if you are running a simple query that does not need to be run on a cluster, choose local mode; otherwise choose Map-

    Reduce mode. 

    In this appendix, you learn about 

    n The SELECT SQL statement used to query data

    n The INSERT, UPDATE, and DELETE SQL statements used to modify table data

    n Recursive SQL statements

    n How to query data that just got inserted, updated, or deleted in the same SQL statement

    n The MERGE SQL statement used to combine insert, update, and/or delete operations in one statement

    This appendix shows you how to leverage the power of SQL to work with relational data that is stored in DB2 databases;however most of what is covered here also applies to Big Data through Big SQL. The examples provided in this appendix

    use the SAMPLE database.

    Querying DB2 Data

    You use the SELECT statement to query tables or views from a database. At a minimum, the statement contains a SELECT clause and a FROM clause. The following are two examples of SELECT statements. This first example uses the wildcard

    symbol (*) to indicate that all columns from the EMPLOYEE  table are selected:SELECT * FROM employee;

    In this example, the column names empno, firstname, and lastname are specified in the SELECT statement:

    SELECT empno, firstnme, lastname FROM employee;

    Derived Columns

    When data is retrieved from a table using the SELECT clause, you can derive or calculate new columns based on othercolumns. The DESCRIBE command is handy to display table definitions, which for this section, can help you determinehow to derive other columns based on existing ones. Let ’s find out what columns are defined in the EMPLOYEE  table.

    DESCRIBE TABLE employee

    The output of this command would be:

    db2 => describe table employee

    Note If you are following the examples in this appendix using the Command Line Processor (CLP) or the CommandWindow, these tools have autocommit enabled by default, so the changes you make are stored permanently to thetable. Refer to Chapter 4, “Using Database Tools and Utilities,” for more information.

    Note The purpose of this appendix is to provide you a good introduction to SQL. For more advanced SQL topics, referto the DB2 Information Center.

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 2 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    3/19

      Data type Column Column name schema Data type name Length Scale Nulls ---------------- --------- ----------------- ---------- ----- -----EMPNO SYSIBM CHARACTER 6 0 No FIRSTNME SYSIBM VARCHAR 12 0 No MIDINIT SYSIBM CHARACTER 1 0 Yes LASTNAME SYSIBM VARCHAR 15 0 No WORKDEPT SYSIBM CHARACTER 3 0 Yes 

    PHONENO SYSIBM CHARACTER 4 0 Yes HIREDATE SYSIBM DATE 4 0 Yes JOB SYSIBM CHARACTER 8 0 Yes EDLEVEL SYSIBM SMALLINT 2 0 No SEX SYSIBM CHARACTER 1 0 Yes BIRTHDATE SYSIBM DATE 4 0 Yes SALARY SYSIBM DECIMAL 9 2 Yes BONUS SYSIBM DECIMAL 9 2 Yes COMM SYSIBM DECIMAL 9 2 Yes 

    14 record(s) selected. 

    Listing B.1 illustrates how to derive the column totalpay  by adding the salary  and comm columns.

    Listing B.1: Example of a Derived Column 

    SELECT empno, firstnme, lastname, (salary + comm) AS totalpay FROM employee 

    EMPNO FIRSTNME LASTNAME TOTALPAY ------ ------------ --------------- ------------000010 CHRISTINE HAAS 156970.00 000020 MICHAEL THOMPSON 97550.00 000030 SALLY KWAN 101310.00 000050 JOHN GEYER 83389.00 000060 IRVING STERN 74830.00 . . . 

    Note that totalpay  is the name for the derived column specified in the SELECT statement. If it is not specified, DB2 uses thecolumn number as the column name. In the following example, ( salary + comm ) is the fourth column in the SELECT list,hence a number 4 is used as the column name.

    SELECT empno, firstnme, lastname, (salary + comm) FROM employee

    The SELECT Statement with COUNT Aggregate Function

    The COUNT option enables you to get a row count of the result set. For example, the SQL statement in Listing B.2 returnsthe number of rows in the SALES table whose region column has the value Quebec . In this case, there are 12 records thatmatch this criteria.

    Listing B.2: Example of a SELECT Statement with COUNT Aggregate Function 

    SELECT COUNT(*) FROM sales 

    WHERE region = 'Quebec' 

    1-----------

    121 record(s) selected. 

    The SELECT Statement with DISTINCT Clause

    To eliminate duplicate rows in a result set among the columns specified in the SELECT statement, use the DISTINCT 

    clause. The SQL statement in Listing B.3 selects the distinct, or unique values, of the region column of the SALES table.

    Listing B.3: Example of a SELECT Statement with DISTINCT Clause 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 3 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    4/19

    SELECT DISTINCT region FROM sales 

    REGION---------------ManitobaOntario-NorthOntario-South

    Quebec4 record(s) selected. 

    You can also use the DISTINCT clause in the SELECT statement with COUNT function. For example, the SQL statement inListing B.4 returns the number of distinct or unique values in the region column of the SALES table.

    Listing B.4: Example of a SELECT Statement with COUNT Function and DISTINCT Clause 

    SELECT COUNT (DISTINCT region) FROM sales 

    1-----------

    4

    1 record(s) selected. 

    The output shows that there are four distinct values for the region column in the SALES table. This value is the same aswe saw with the SELECT DISTINCT region FROM sales result obtained in Listing B.3.

    DB2 Special Registers

    DB2 special registers are memory values/registers that allow DB2 to provide information to an application about itsenvironment. These registers can be referenced in SQL statements. Table B.1 lists the most commonly used specialregisters. For a complete list of DB2 special registers, refer to the DB2 SQL Reference Guide.

    Table B.1: DB2 Special Registers

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 4 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    5/19

    To display the value of a special register, use the following statement:

    VALUES special_register

    For example, to display the value of the CURRENT TIMESTAMP special register, issue

    VALUES CURRENT TIMESTAMP

    SQL also supports expressions using DB2 special registers. Listing B.5 uses the CURRENT DATE register to derive the

    retiredate column.

    Listing B.5: Example of Using DB2 Special Registers in a SELECT Statement 

    SELECT empno, firstnme, lastname , (salary + comm) AS totalpay , CURRENT DATE AS retiredate 

    FROM employee 

    EMPNO FIRSTNME LASTNAME TOTALPAY RETIREDATE ------ ------------ --------------- ------------ ----------000010 CHRISTINE HAAS 156970.00 10/12/2006 000020 MICHAEL THOMPSON 97550.00 10/12/2006 000030 SALLY KWAN 101310.00 10/12/2006 

    000050 JOHN GEYER 83389.00 10/12/2006 000060 IRVING STERN 74830.00 10/12/2006 . . . 

     As indicated in Table B.1, some of the special registers are updatable. For example, to change the value of the CURRENTISOLATION special register to RR (Repeatable Read), issue:

    SET CURRENT ISOLATION RR

    Scalar and Column Functions

    Invoking a function against the column values can be useful to derive new column values. Consider the following examplewhere you want to obtain the name of the day for each employee’s hire date. You can use the DAYNAME built-in function

    supplied by DB2, as shown in Listing B.6.

    Listing B.6: Example of a Scalar Function 

    SELECT empno, firstnme, lastname , (salary + comm) AS totalpay , DAYNAME(hiredate) AS dayname 

    FROM employee 

    EMPNO FIRSTNME LASTNAME TOTALPAY DAYNAME ------ ------------ --------------- ------------ ----------000010 CHRISTINE HAAS 156970.00 Sunday 000020 MICHAEL THOMPSON 97550.00 Friday 000030 SALLY KWAN 101310.00 Tuesday 

    000050 JOHN GEYER 83389.00 Friday 000060 IRVING STERN 74830.00 Sunday . . . 

    In Listing B.6, the hiredate column is defined with a DATE data type. Invoking the DAYNAME function on the hiredate column retrieves the name of the day for that date. The function DAYNAME is called a scalar function. A scalar function takes input values and returns a single value. Another type of function, called a column function, operates on the values ofan entire column. The example in Listing B.7 shows how to calculate the average value of the salary  column in theEMPLOYEE  table.

    Listing B.7: Example of a Column Function 

    SELECT DECIMAL( AVG(salary), 9, 2 ) AS avgsalary FROM employee 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 5 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    6/19

    AVGSALARY-----------

    58155.351 record(s) selected. 

    The AVG column function, which is a built-in function, calculates the average of a specified column. In this example, itcalculates the average of all the salary values in the EMPLOYEE  table. Notice that the DECIMAL function is also used; this

    casts the average result to a decimal representation with a precision of 9 and scale of 2. Casting is discussed in the nextsection.

    The CAST Expression

    There are many occasions when a value with a given data type needs to be converted to a different data type. Forexample, when manipulating data using the DATE and TIMESTAMP data types, TIMESTAMP might need to be cast toDATE. Listing B.8 illustrates such an example.

    Listing B.8: Example of a CAST Expression 

    SELECT CURRENT TIMESTAMP, CAST(CURRENT TIMESTAMP AS DATE) FROM SYSIBM.SYSDUMMY1 

    1 2 -------------------------- ----------2006-10-12-12.42.16.828000 10/12/2006 

    1 record(s) selected. 

    DB2 provides many built-in functions you can use. In addition, you can create your own user-defined functions (UDFs). Formore information, refer to the DB2 Information Center.

    The FROM Clause

    The FROM clause is used to specify the table or tables from where column information will be retrieved. When specifyingmultiple tables after FROM, you are dealing with JOIN operations explained later in this appendix. In addition, after a FROM clause you can enter another SELECT statement or even a function call as long as the output is another table. Later in theappendix you see examples of this.

    The WHERE Clause

    For better performance, you should always write your SQL statements so that only the required data is returned. One wayto achieve this is to limit the number of columns to be retrieved by explicitly specifying the column names in the SELECT statement (as illustrated in previous examples). The other way is to limit the number of rows to be retrieved using theWHERE clause. Listing B.9 illustrates an example of a SELECT statement that returns employees who are managers with asalary greater than $1,000.00.

    Listing B.9: Example of a WHERE Clause 

    SELECT empno, firstnme, lastname FROM employee 

    WHERE salary > 1000 AND job = 'MANAGER' 

    EMPNO FIRSTNME LASTNAME ------ ------------ ---------------000020 MICHAEL THOMPSON 000030 SALLY KWAN 000050 JOHN GEYER 000060 IRVING STERN 000070 EVA PULASKI 

    000090 EILEEN HENDERSON 000100 THEODORE SPENSER 7 record(s) selected. 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 6 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    7/19

    Using FETCH FIRST n ROWS ONLY

    Sometimes the result set returned contains hundreds or thousands of rows, and you might only need the first few rowsfrom the result set. Use the FETCH FIRST n ROWS ONLY clause of the SELECT statement to accomplish this. Forexample, to only return the first three rows from the example illustrated in Listing B.9, use the statement shown in ListingB.10. Note that the actual result of the query does not change, but you instructed DB2 to return only the first n rows of the

    result set. This is also helpful for performance.

    Listing B.10: Example of FETCH FIRST n ROWS ONLY 

    SELECT empno, firstnme, lastname FROM employee 

    WHERE workdept > 1000 AND job = 'MANAGER' 

    FETCH FIRST 3 ROWS ONLY 

    EMPNO FIRSTNME LASTNAME ------ ------------ ---------------000020 MICHAEL THOMPSON 000030 SALLY KWAN 000050 JOHN GEYER 

    3 record(s) selected. 

    The LIKE Predicate

    The LIKE predicate enables you to search for patterns in character string columns. In SQL, the percent sign (%) is awildcard character that represents zero or more characters. It can be used any place in the search string and as manytimes as you need it.

    The other wildcard character used with the LIKE predicate is the underline character (_). This character represents oneand only one character. For example, the SQL statement in Listing B.11 returns all the rows in the employee table wherethe lastname column starts with the letter M or the workdept  column contains three characters starting with 'D2'.

    Listing B.11: Example of a LIKE Predicate 

    SELECT empno, firstnme, lastname, workdept FROM employee WHERE lastname LIKE 'M%' OR workdept LIKE 'D2_' 

    EMPNO FIRSTNME LASTNAME WORKDEPT ------ ------------ --------------- --------000070 EVA PULASKI D21 000230 JAMES JEFFERSON D21 000240 SALVATORE MARINO D21 000250 DANIEL SMITH D21 000260 SYBIL JOHNSON D21 000270 MARIA PEREZ D21 

    000320 RAMLAL MEHTA E21 200240 ROBERT MONTEVERDE D21 

    8 record(s) selected. 

    The BETWEEN Predicate

    The BETWEEN predicate enables you to search for all the rows whose value falls between the values indicated. Forexample, the SQL statement in Listing B.12 returns all the rows from the employee table whose salary is between $40,000and $50,000.

    Listing B.12: Example of a BETWEEN Predicate 

    SELECT firstnme, lastname, salary FROM employee WHERE salary BETWEEN 40000 AND 50000 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 7 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    8/19

    FIRSTNME LASTNAME SALARY ------------ --------------- -----------SEAN O'CONNELL 49250.00 MASATOSHI YOSHIMURA 44680.00 JENNIFER LUTZ 49840.00 JAMES JEFFERSON 42180.00 SALVATORE MARINO 48760.00 DANIEL SMITH 49180.00 

    SYBIL JOHNSON 47250.00 WING LEE 45370.00 JASON GOUNOT 43840.00 DIAN HEMMINGER 46500.00 EILEEN SCHWARTZ 46250.00 

    11 record(s) selected. 

    The IN Predicate

    The IN predicate enables you to search rows based on a set of values. The SQL statement in Listing B.13 returns all therows from the SALES table where the value in the sales_date column is either 03/29/1996  or 04/01/2006 .

    Listing B.13: Example of an IN Predicate 

    SELECT * FROM sales WHERE sales_date IN ('03/29/1996', '04/01/2006') 

    SALES_DATE SALES_PERSON REGION SALES ---------- --------------- --------------- -----------03/29/1996 LEE Ontario-North 2 04/01/2006 LUCCHESSI Ontario-South 3 04/01/2006 LUCCHESSI Manitoba 1 04/01/2006 LEE Ontario-South 8 04/01/2006 LEE Ontario-North -04/01/2006 LEE Quebec 8 04/01/2006 LEE Manitoba 9 04/01/2006 GOUNOT Ontario-South 3 

    04/01/2006 GOUNOT Ontario-North 1 04/01/2006 GOUNOT Quebec 3 04/01/2006 GOUNOT Manitoba 7 

    11 record(s) selected. 

    The ORDER BY Clause

    SQL does not return the results retrieved in a particular order; the order of a result can be different each time a SELECT statement is executed. To sort the result set, use the ORDER BY clause as shown in Listing B.14.

    Listing B.14: Example of an ORDER BY Clause 

    SELECT empno, firstnme, lastname FROM employee 

    WHERE job='MANAGER' ORDER BY lastname ASC 

    EMPNO FIRSTNME LASTNAME ------ ------------ ---------------000050 JOHN GEYER 000090 EILEEN HENDERSON 000030 SALLY KWAN 000070 EVA PULASKI 000100 THEODORE SPENSER 000060 IRVING STERN 000020 MICHAEL THOMPSON 

    7 record(s) selected. 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 8 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    9/19

    You can specify column names or column numbers in the ORDER BY clause, so in the previous query, ORDER BYlastname ASC could be replaced with ORDER BY 3 ASC.

    Notice that LASTNAME  is sorted in ascending order. You can explicitly specify the keyword ASC or omit it because it is thedefault behavior. To sort the result set in descending order, simply use the DESC keyword instead.

    The GROUP BY...HAVING Clause

    When you need to group multiple rows into a single row based on one or more columns, the GROUP BY clause comes inhandy. Let’s use an example to explain the usage of this clause. Listing B.15 sums up the salary of all the employees. TheGROUP BY clause groups the results by workdept , which returns the total salary of the employees for each department.

    The HAVING clause then specifies which of the combined rows are to be retrieved. You can think of it as a WHERE clausethat is applied only to the GROUP BY clause group. In the statement in Listing B.15, only department names starting with E  are retrieved.

    Listing B.15: Example of GROUP BY and HAVING Clauses 

    SELECT workdept, SUM(salary) AS total_salary FROM employee 

    GROUP BY workdept HAVING workdept LIKE 'E%' 

    WORKDEPT TOTAL_SALARY -------- ---------------------------------E01 80175.00 E11 317140.00 E21 282520.00 

    3 record(s) selected. 

    Joins

    Sometimes information you want to retrieve does not reside in a single table. You can join the rows in two or more tables ina SELECT statement by listing the tables in the FROM clause. Consider the example in Listing B.16.

    Listing B.16: Example of an INNER Join 

    SELECT empno, firstnme, lastname, deptname, mgrno FROM employee, department 

    WHERE workdept = deptno AND admrdept = 'A00' 

    EMPNO FIRSTNME LASTNAME DEPTNAME MGRNO ------ ------------ -------------- ----------------------------- ------000010 CHRISTINE HAAS SPIFFY COMPUTER SERVICE DIV. 000010 000020 MICHAEL THOMPSON PLANNING 000020 000030 SALLY KWAN INFORMATION CENTER 000030 000050 JOHN GEYER SUPPORT SERVICES 000050 000110 VINCENZO LUCCHESSI SPIFFY COMPUTER SERVICE DIV. 000010 000120 SEAN O'CONNELL SPIFFY COMPUTER SERVICE DIV. 000010 000130 DELORES QUINTANA INFORMATION CENTER 000030 000140 HEATHER NICHOLLS INFORMATION CENTER 000030 200010 DIAN HEMMINGER SPIFFY COMPUTER SERVICE DIV. 000010 200120 GREG ORLANDO SPIFFY COMPUTER SERVICE DIV. 000010 200140 KIM NATZ INFORMATION CENTER 000030 

    11 record(s) selected. 

    Listing B.16 retrieves a list of employees, their department names, and manager ’s employee numbers whose administrative

    department (admrdept ) is A00 . Because the EMPLOYEE  table only stores the department number of the employees andnot the department names, you need to join the EMPLOYEE  table with the DEPARTMENT  table. Note that the two tables

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 9 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    10/19

    are joined in the FROM clause. Only records with matching department numbers (workdept = deptno) are retrieved.

    This type of join is called an inner join; it results in matched rows that are present in both joined tables. The INNER JOINkeywords can be omitted as demonstrated in Listing B.16. If you choose to explicitly use the INNER JOIN syntax, anexample can be seen at the bottom of Figure B.1, which also illustrates the information that is retrieved when using thistype of join.

    Figure B.1: Example of an INNER join

    Note that the INNER JOIN clause is used in the FROM clause. The ON keyword specifies the join predicates andcategorizes rows as either joined or not-joined. This is different from the WHERE clause, which is used to filter rows.

    There are three other types of joins: LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN. Outer joinsare useful when you want to include rows that are present in the left table, right table, or both tables, in addition to the rowsreturned from the implied inner join. A table specified on the left side of the OUTER JOIN operator is considered the lefttable, and the table specified on the right side of the OUTER JOIN operator is considered the right table.

     A left outer join includes rows from the left table that were missing from the inner join. A right outer join includes rows fromthe right table that were missing from the inner join. A full outer join includes rows from both the left and right tables thatwere missing from the inner join. Figure B.2, B.3, and B.4 demonstrate information to be retrieved and an example of each

     join.

    Figure B.2: Example of a LEFT OUTER join

    Note When joining tables, it is good practice to provide alias names to the tables being joined. This is particularly usefulwhen you need to distinguish between columns that have the same column names on different tables. Forexample, in the SELECT statement below, we use alias ‘E’ for table employee, and alias ‘D’ for table department.Then the columns can be prefixed with the appropriate table alias.

    SELECT empno, firstnme, lastname, deptname FROM employee E INNER JOIN department D 

    ON E.workdept = D.deptno 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 10 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    11/19

     Figure B.3: Example of a RIGHT OUTER join

    Figure B.4: Example of a FULL OUTER join

    Working with NULLs

     A NULL in DB2 represents an unknown value. The following is an example of how to return all rows where the value ofmidinit  is NULL.

    SELECT empno FROM employee WHERE midinit IS NULL

    When working with NULL values, the COALESCE function comes in handy: It checks whether the input is NULL andreplaces it with the specified value if it is NULL. See Listing B.17 for an example that returns 0 if the value in the comm column is NULL.

    Listing B.17: Example of the COALESCE Function 

    SELECT id, name, COALESCE(comm, 0) AS comm FROM staff 

    FETCH FIRST 6 ROWS ONLY 

    ID NAME COMM ------ --------- ---------------

    10 Sanders 0.00 20 Pernal 612.45 30 Marenghi 0.00 40 O'Brien 846.55 50 Hanes 0.00 60 Quigley 650.25 

    6 record(s) selected. 

    The CASE Expression

    When you want to perform different operations based on the evaluation of a column or value, you can use the CASE 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 11 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    12/19

    expression in an SQL statement to simplify your code. Listing B.18 introduces this expression.

    Listing B.18: Example of a CASE Expression 

    SELECT firstnme, lastname, CASE 

    WHEN salary IS NULL THEN 'Salary amount is missing' WHEN salary 40000 AND salary 35 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 12 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    13/19

     ROWID FIRSTNME LASTNAME -------------------- ------------ ---------------

    36 KIYOSHI YAMAMOTO 37 REBA JOHN 38 ROBERT MONTEVERDE 39 EILEEN SCHWARTZ 40 MICHELLE SPRINGER 41 HELENA WONG 42 ROY ALONZO 

    7 record(s) selected. 

    Modifying Table Data

    You modify table data using the INSERT, UPDATE, DELETE, and MERGE statements. Most of the clauses and functionsdescribed in the previous section also work with these statements. We use some examples to explain their basic usages.

    You can specify all the column values in the INSERT statement like this:

    INSERT INTO employee VALUES ( '000998', 'SMITH', 'A', 'JOHN', NULL, NULL, NULL, NULL, 18, 

    'M', NULL, NULL, NULL, NULL ) 

     Alternatively, you can explicitly specify the column list for which values are provided in the INSERT statement:

    INSERT INTO employee (empno, firstnme, midinit, lastname, edlevel)VALUES ( '000999', 'SMITH', 'A', 'JOHN', 18 );

    For columns that are not named in the INSERT statement, NULL or default value if defined are inserted for a nullablecolumn. On the other hand if a NOT NULL column has a default value defined, that value will be inserted. Otherwise, theINSERT fails with SQL0407 Assignment of a NULL value to a NOT NULL column is not allowed .

    You can also insert multiple rows in one INSERT statement as shown here:

    INSERT INTO employee (empno, firstnme, midinit, lastname, edlevel) VALUES ( '000999', 'SMITH', 'A', 'JOHN', 18 ) 

    , ( '000998', 'LOPEZ', 'M', 'JEN' , 18 ) 

    , ( '000997', 'FRASER', 'B', 'MARC', 28 ); 

     A multi-row insert can also be achieved with values obtained from a SELECT statement:

    INSERT INTO employee_temp ( SELECT * FROM employee );

    It is fairly straightforward to update one or more rows in a table by simply assigning the new values in the SET clause of anUPDATE statement:

    UPDATE employee SET salary = salary * 1.5, comm = 0WHERE empno='000999'; 

    The next UPDATE statement updates the EMPLOYEE  table and sets the hiredate with value of the DB2 special registerCURRENT DATE. It also sets the workdept  to the department number selected from the DEPARTMENT  table.

    UPDATE employee 

    SET (hiredate, workdept) = (SELECT CURRENT DATE, deptno FROM department 

    WHERE deptname='PLANNING') WHERE empno='000999'; 

    The DELETE statement is used to delete rows from a table. To remove all rows from the EMPLOYEE  table, use thefollowing statement:

    DELETE FROM employee;

    To remove only certain rows, use the WHERE clause to filter the rows:

    DELETE FROM employee WHERE workdept IS NULL;

    To remove rows with a relative position greater than 100 when ordered by empno, use the ROWNUMBER()OVER() 

    functions like this:DELETE FROM 

    (SELECT ROWNUMBER() OVER(ORDER BY empno) AS rowid 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 13 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    14/19

      FROM employee) WHERE rowid > 100 

    Selecting from UPDATE, DELETE, or INSERT

     Although the INSERT, UPDATE, and DELETE statements change data in the specified tables, they only return a messageindicating whether or not the statement completed successfully and an indicator of the number of rows being affected. If the

    statement completed successfully, you need to issue a separate SQL statement to find out what changed. In the nextexample, to determine which rows are to be deleted, you first issue a SELECT statement to capture the rows you will bedeleting with a subsequent DELETE statement. Both statements have the same WHERE condition to filter the same rows.

    SELECT empno, firstnme, lastname FROM employee WHERE workdept = 'A00';DELETE FROM employee WHERE workdept = 'A00';

    Rather than issuing two separate statements, they can be optimized to use just one SQL statement like this:

    SELECT empno, firstnme, lastname FROM OLD TABLE (DELETE FROM employee WHERE workdept = 'A00'); 

    The preceding statement deletes the rows and reports to you what was deleted. Because of referential integrity in theSAMPLE  database, this DELETE will not work in this case, but you can practice with this command with other tables ofyour own.

    Whenever a table is inserted, updated, or deleted, DB2 maintains one or more internal temporal tables known as transitiontables. You specify the transition tables with the NEW TABLE and OLD TABLE clauses. Depending on the SQL operation,different transition tables are available. Refer to Table B.2 for a summary of their availability.

    To demonstrate a SELECT from UPDATE query, consider the following example in which you want to increase the salary ofall the employees in department A00. If you use the OLD TABLE clause, you can perform the update and return the oldsalaries as they were before the update. This is good for building an audit table to track the changes to important tables.

    SELECT salary FROM OLD TABLE (UPDATE employee 

    SET salary = salary * 1.1 WHERE workdept = 'A00') 

    Similarly, if you want to retrieve the new salary, you can use the NEW TABLE clause instead:

    SELECT salary 

    FROM NEW TABLE (UPDATE employee SET salary = salary * 1.1 

    WHERE workdept IS NULL ) 

    SELECT from INSERT works just like the preceding example:

    SELECT salary FROM NEW TABLE (INSERT INTO employee 

    (empno, firstnme, midinit, lastname, edlevel, salary) VALUES ( '000999', 'SMITH', 'A', 'JOHN', 18, 45000 )) 

    You cannot retrieve the new and old salary values using both the NEW TABLE and OLD TABLE clauses. To do this, use

    the INCLUDE clause.SELECT salary as new_salary, old_salary 

    Table B.2: Availability of Transition Tables Depending on the SQL StatementIssued

    Note This can now be also achieved with the Time Travel Query feature explained in Chapter 7, “Working withDatabase Objects.” 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 14 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    15/19

      FROM NEW TABLE ( UPDATE employee INCLUDE (old_salary DECIMAL(9,2)) SET salary = salary * 1.10, 

    old_salary = salary WHERE workdept = 'A00') 

    The INCLUDE clause in the nested UPDATE statement creates a new column that can be selected using the outer SELECT statement. You can see that the old_salary  gets the old salary value, and the table column salary  is increased by 10percent.

    Finally, let’s look at the FINAL TABLE clause. When executing an INSERT, UPDATE, or DELETE statement, there can stillbe AFTER triggers or referential constraints that result in further modification of data in the table. Using FINAL TABLE canprevent these types of changes.

    For instance, assume that an AFTER trigger is defined to delete all rows from the EMPLOYEE  table when an employee’ssalary is updated. If FINAL TABLE is used, the UPDATE statement will fails. This protects you from any unforeseen side-effects not visible to the application.

    For example, an error is received if the following SQL statement is issued:

    SELECT salary FROM FINAL TABLE ( UPDATE employee 

    SET salary = salary * 1.1 

    WHERE workdept = 'A00') SQL0989N AFTER trigger "AUDIT_TRIG" attempted to modify a row in table "EMPLOYEE" that was modified by an SQL data change statement within a FROM clause. SQLSTATE=560C3 

    The MERGE Statement

    The MERGE statement combines an INSERT statement with an UPDATE or DELETE statement. For example, if a row intable T1 also exists in table T2 , the existing row in T2  should be updated. If a row in T1 does not exist in T2 , it should beinserted into T2 . A new and efficient way to code this logic can be implemented with one statement: the MERGE statement.

    Listing B.21 shows this MERGE statement.

    Listing B.21: Example of a MERGE Statement 

    MERGE INTO T2 as target USING (SELECT ... FROM T1) AS source 

    ON target.id=source.id WHEN NOT MATCHED THEN 

    INSERT INTO T2 ... WHEN MATCHED THEN 

    UPDATE T2 SET ... 

    Listing B.22 illustrates the syntax of the MERGE statement. The MERGE statement has many clauses; see the DB2 SQLReference manual for more examples and additional information.

    Listing B.22: Syntax of the MERGE Statement 

    >>-MERGE INTO--+-table-name-------+---------------------------->+-view-name--------+'-(--fullselect--)-'

    >--+------------------------+--USING--table-reference---------->'-| correlation-clause |-'

    >--ON--search-condition---------------------------------------->

    .----------------------------------------------------------------.V | 

    >----WHEN--| matching-condition |--THEN--+-| modification-operation |+> '-signal-statement---------'

    Note Trigger is a type of application database object that defines a set of actions to be performed in response to aninsert, update, or delete operation on a specified table. Triggers are discussed in Chapter 7, “Working withDatabase Objects.” 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 15 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    16/19

     .-ELSE IGNORE-.

    >--+-------------+--------------------------------------------><

    The UNION, INTERSECT, and EXCEPT Operators

    UNION, INTERSECT, and EXCEPT are operators that can be used to obtain the union, intersection, and difference amongfullselect , subselect , or values-clause. Listing B.23 shows the syntax diagram of the UNION, INTERSECT, and EXCEPT operators.

    Listing B.23: Syntax Diagram of the UNION, INTERSECT, and EXCEPT Operators  

    >>-+-subselect---------+--------------------------------------->+-(fullselect)------+'-| values-clause |-'

    .----------------------------------------------.V | 

    >----+------------------------------------------+-+------------>'-+-UNION---------+--+-subselect---------+-'+-UNION ALL-----+ +-(fullselect)------++-EXCEPT--------+ '-| values-clause |-'+-EXCEPT ALL----++-INTERSECT-----+'-INTERSECT ALL-'

    >--+-----------------+--+--------------------+----------------><'-order-by-clause-' '-fetch-first-clause-'

    The UNION and UNION ALL Operators

     A UNION operation combines two sets of columns and removes duplicate rows. Specifying UNION ALL gives the sameresult as the UNION operation, but it also includes the duplicate rows. Consider the two tables, R1 and R2, in Listing B.24.

    Listing B.24: R1 and R2 Tables 

    R1 R2 ------------  -----------Apple Apple Apple Apple Apple Banana Banana Banana Banana Banana Cranberry Cranberry Cranberry Mango 

    CranberryOrange

    Listing B.25 shows the results of the UNION and UNION ALL operations on the two tables illustrated in Listing B.24. Asyou can see, the UNION operator removes duplicates.

    Listing B.25: Examples of UNION and UNION ALL 

    SELECT R1 FROM R1 AS R1_UNION_R2 UNION SELECT R2 AS R1_UNION_R2 FROM R2 ORDER BY R1_UNION_R2 

    R1_UNION_R2

    ------------------AppleBanana

    Note Refer to Chapter 1, “Introduction to DB2,” for a description of the DB2 syntax diagram conventions.

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 16 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    17/19

    CranberryMangoOrange

    SELECT R1 AS R1_UNION_ALL_R2 FROM R1 UNION ALL 

    SELECT R2 AS R1_UNION_ALL_R2 FROM R2 ORDER BY R1_UNION_ALL_R2 

    R1_UNION_ALL_R2------------------------AppleAppleAppleAppleAppleBananaBananaBananaBananaBananaCranberryCranberryCranberryCranberryMangoOrange

    The INTERSECT and INTERSECT ALL Operators

     An INTERSECT operation retrieves the matching set of distinct values from two columns; INTERSECT ALL returns the setof matching rows. The examples in Listing B.26 use tables R1 and R2 from Listing B.24.

    Listing B.26: Examples of INTERSECT and INTERSECT ALL 

    SELECT R1 AS R1_INTERSECT_R2 FROM R1 INTERSECT SELECT R2 AS R1_INTERSECT_R2 FROM R2 

    ORDER BY R1_INTERSECT_R2 

    R1_INTERSECT_R2------------------AppleBananaCranberry

    SELECT R1 AS R1_INTERSECT_ALL_R2 FROM R1 INTERSECT ALL SELECT R2 AS R1_INTERSECT_ALL_R2 FROM R2 

    ORDER BY R1_INTERSECT_ALL_R2 

    R1_INTERSECT_ALL_R2------------------------AppleAppleBananaBananaCranberry

    The EXCEPT and EXCEPT ALL Operators

     An EXCEPT operation retrieves the set of distinct values that exist in the first table but not in the second table. EXCEPTALL returns the set of rows that exist only in the first table. The examples in Listing B.27 use tables R1 and R2 from Listing

    B.24.

    Listing B.27: Examples of EXCEPT and EXCEPT ALL 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 17 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    18/19

    SELECT R1 AS R1_EXCEPT_R2 FROM R1 EXCEPTSELECT R2 AS R1_EXCEPT_R2 FROM R2 ORDER BY R1_EXCEPT_R2 

    R1_EXCEPT_R2------------------

    Mango

    SELECT R1 AS R1_EXCEPT_ALL_R2 FROM R1 EXCEPT ALL SELECT R2 AS R1_EXCEPT_ALL_R2 FROM R2 ORDER BY R1_EXCEPT_ALL_R2 

    R1_EXCEPT_ALL_R2------------------------AppleCranberryCranberryMango

    Recursive SQL Statements

    Recursive SQL is a powerful way to query hierarchies of data. Organizational structures, bills-of-material, productclassifications, and document hierarchies are all examples of hierarchical data. It can also be used to generate randomdata. Recursive SQL is implemented using common table expressions (CTE), which temporarily store data as the queryexecution progresses. Multiple common table expressions can be specified following the single WITH keyword. It can alsobe referenced in other places within the query. Each use of a specific CTE within a complex query shares the sametemporary view. Listing B.28 illustrates the syntax of a common table expression.

    Listing B.28: Syntax of a Common Table Expression 

    >>-table-name--+------------------+----------------------------->| .-,-----------. | 

    | V | | '-(----column-name-+--)-----'

    >--AS — (--fullselect--)------------------------------------------><

    Let’s use an example to demonstrate how a recursive SQL statement is written.

     Assume that there is a table called CHILDREN  with definitions and data, as shown in Listing B.29.

    Listing B.29: Sample Data in the children Table 

    CREATE TABLE children ( person_id INTEGER , name VARCHAR(50) , age INTEGER 

    , gender CHAR(1) , parent_id INTEGER ); 

    SELECT * FROM children; 

    PERSON_ID NAME AGE GENDER PARENT_ID ----------- -------- ----------- ----- -----------

    1 Apple 10 F 10 2 Zoe 11 F 3 3 John 30 M 13 4 Mary 25 F 24 5 Peter 14 M 4 6 Jenny 13 F 4 

    24 Robert 60 M 30 

    7 record(s) selected. 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Reprinted for ibm\[email protected], IBM IBM Press, International Business Machines Corporation (c) 2014, Copying Prohibited

    Page 18 / 19

  • 8/17/2019 9780133461909 Appendix B Introduction to SQL

    19/19

    To retrieve the ancestors of Jenny , you would use the recursive query shown in Listing B.30.

    Listing B.30: A Recursive SQL Example 

    WITH temptab (person_id, name, parent_id) AS (1) (SELECT person_id, name, parent_id (2) 

    FROM children WHERE name = 'Jenny' 

    UNION ALL (3) 

    SELECT c.person_id, c.name, c.parent_id (4) FROM children c, temptab super 

    WHERE c.person_id = super.parent_id 

    ) SELECT * FROM temptab (5) 

    In Listing B.30, the CTE is called temptab, and it is created with the WITH clause on line (1). The definition of the CTE isspecified at lines (2), (3), and (4) inside the parentheses.

    Line (2) obtains the initial result set that contains the record with the name 'Jenny'. Then, the recursion takes place by

     joining each row in temptab with its parents (4). The result of one execution of this recursion is added to temptab viaUNION ALL at line (3).

    The final query (5) extracts the person_id , name, and parent_id  out of the temptab CTE.

    The recursive SQL returns Jenny’s parents and their parents, as shown in Listing B.31.

    Listing B.31: Result of a Recursive SQL 

    PERSON_ID NAME PARENT_ID ----------- ----------------------------- -----------SQL0347W The recursive common table expression "DB2ADMIN.TEMPTAB" may contain an infinite loop. SQ 

    6 Jenny 4 4 Mary 24 

    24 Robert 30 

    3 record(s) selected with 1 warning messages printed. 

    Notice that a warning message is also returned indicating that the CTE might contain an infinite loop. To avoid an infiniteloop, you can specify the maximum number of recursive levels in the query. For example, in Listing B.32 the maximumnumber of recursive levels must be fewer than 5.

    Listing B.32: A Recursive SQL Example with a Maximum Number of Recursive Levels 

    WITH temptab (person_id, name, parent_id, level) AS 

    (SELECT person_id, name, parent_id, 1 FROM children 

    WHERE name = 'Jenny' 

    UNION ALL 

    SELECT c.person_id, c.name, c.parent_id, super.level + 1 FROM children c, temptab super 

    WHERE c.person_id = super.parent_id  AND level < 5 

    ) SELECT * FROM temptab 

    DB2 Essentials: Understanding DB2 in a Big Data World, Third Edition

    Page 19 / 19