720 likes | 1.01k Views
Advanced SQL. SMSU Computer Services Short Course. Contact Information. Greg Snider – MIS Database Analyst Ext. 6-4397 Email – SGS345B Don’t hesitate to email or call if you have any questions after you start using QM and SQL. Agenda. SQL Limits FETCH FIRST n ROWS ONLY
E N D
Advanced SQL SMSU Computer Services Short Course
Contact Information • Greg Snider – MIS Database Analyst • Ext. 6-4397 • Email – SGS345B Don’t hesitate to email or call if you have any questions after you start using QM and SQL.
Agenda • SQL Limits • FETCH FIRST n ROWS ONLY • Quantified predicates • Named expressions • Scalar Subselect
Agenda • Case • Multiple Join Types • Common Table Expressions • In-line Views/Table Expressions/Nested Table Expressions / Derived Tables
SQL Limits • SQL statements can now be 65, 535 characters long • 256 tables can be referenced in a single SQL statement • Approximately 8000 elements in a SELECT list
FETCH FIRST n ROWS ONLY • Sets the maximum number of rows to be returned • Follows the ORDER BY clause if it is present • If the ORDER BY clause is present, then the FETCH FIRST operation is performed on the sorted data
Quantified Predicates • Compares a value with a set of values • Expression [=, <>, <, >, <=, >=] [Some, Any, All] (subselect)
Quantified Predicates • Expression > ALL (subselect) • The predicate is true if the expression is greater than each individual value returned by the subselect. If the subselect returns no values, the predicate is true. The result is false if the specified relationship is false for at least one value. • The <> All predicate is equivalent to the NOT IN predicate.
Quantified Predicates • Expression > ANY (subselect • The predicate is true if the expression is greater than at least on of the values returned by the subselect. If the subselect returns no values, the predicate is false. • The = ANY quantified operator is equivalent to the IN predicate • SOME is synonymous with ANY
Named Expressions • The AS clause lets you assign a meaningful name to an expression, which makes it easier to refer back to that expression. • SELECT YEAR(CURRENT DATE) – BIRTH_DATE) AS AGE • The named column cannot be used in a WHERE clause • The named column can be used in a ORDER BY clause or GROUP BY clause
Scalar Subselect • A scalar subselectis a subselect, enclosed in parentheses, placed in the SELECT list that returns a single result row and a single result column. If the result of the subselect is no rows, then the null value is returned. An error is returned if there is more than one row in the result.
CASE • The CASE expression can be used to easily manipulate the data in a column • It can be used to change codes in to meaningful words • It can be used to protect against exceptions such as division by zero
Can I use more than one column in a CASE expression? • Yes, any number of WHEN conditions can be used in the CASE expression, and each WHEN can reference a different column or expression. CASE ( WHEN COL7 > 15 THEN COL7 * .0133 WHEN COL3 = 34 THEN COL3 / .0133 WHEN COL8 < 98 THEN 999 ELSE NULL END) AS MULTICALC
Where can I use CASE expressions? • Anywhere an expression is allowed in a SQL statement • SELECT clause • WHERE clause • HAVING clause • Built-in Function • IN (list)
CASE Example 5 (join ORing) SELECT columns FROM T1, T2 WHERE (CASE WHEN T1.ID1 IS NULL THEN T1.ID2 END) = T2.COLX
CASE Example 6 (table pivoting) • The table looks like this:
Multiple Join types • Inner Join • Left Outer Join • Right Outer Join • Left Exception Join • Right Exception Join • Full Outer Join
Inner Join • An Inner Join returns only the rows from each table that have matching values in the join columns. Any rows that do not have a match between the tables will not appear in the result table. • This is the join you’re used to using. FROM T1 INNER JOIN T2 ON T1.COLA = T2.COL1
Left Outer Join • A Left Outer Join returns values for all of the rows from the first table (the table on the left) and the values from the second table for the rows that match. Any rows that do not have a match in the second table will return the null value for all columns from the second table. FROM T1 LEFT OUTER JOIN T2 ON T1.COLA = T2.COL1
Left Outer Join – Example • List students and the classes they are taking, if any.
Right Outer Join • A Right Outer Join return values for all of the rows from the second table (the table on the right) and the values from the first table for the rows that match. Any rows that do not have a match in the first table will return the null value for all columns from the first table. FROM T1 RIGHT OUTER JOIN T2 ON T1.COLA = T2.COL1
Left Exception Join • A Left Exception Join returns only the rows from the left table that do not have a match in the right table. Columns in the result table that come from the right table have the null value. FROM T1 LEFT EXCEPTION JOIN T2 ON T1.COLA = T2.COL1
Left Exception Join – Example • List students who are not taking classes
Right Exception Join • A Right Exception Join returns only the rows from the right table that do not have a match in the left table. Columns in the result table that come from the left table have the null value. FROM T1 RIGHT EXCEPTION JOIN T2 ON T1.COLA = T2.COL1
Full Outer Join • The Full Outer Joinincludes the inner join (the rows that match) and the rows from both the left and right tables that do not match. • The Full Outer Join is not included in this version of SQL, but it can be simulated. • You simulate a Full Outer Join by using a Left Outer Join union’ed with a Right Exception Join
How to know which join to use? • If the request contains one of the key condition phrases: “whether or not”, “regardless of”, “no matter what”, “if any”. • If any of these phrases fit in the generic information request of “get all information from this table—condition phrase—other information is present in the other table”, then a left outer join will be necessary.