SlideShare a Scribd company logo
Advanced SQL Training Seminar William Bishop August 17 th , 2009
Content SQL Statement Types  SQL Data Types Aggregate Functions NULL Comparison Operators & Keywords Set Operations CASE Statement Mathematical Operators & Functions  Joins  Subquerys Views Materialized Views Inline Views Optimizing Select Clauses Data Sharing Q&A, Discussion & Problem Solving
SQL (Structured Query Language)   is a  database computer language  designed for managing data in relational database management systems (RDBMS). Its scope includes  data query  and  update, schema creation  and  modification , and  data access control .
SQL Statement Types
SQL Statement Types DML  -   stands for  Data Manipulation Language , it’s the part of SQL that deals with querying updating and inserting records in tables and views. DDL  – stands for  Data Definition Language , it’s the part of SQL that deals with the construction and alteration of database structures like tables, views and the further entities inside these tables like columns.  It may be used to set the properties of columns as well.
SQL Statement Types DML SELECT … FROM … WHERE ….  INSERT INTO ….  WHERE … DELETE FROM …. WHERE … UPDATE … WHERE …. DDL CREATE [TABLE | VIEW | ROLE …  DROP [TABLE | VIEW | ROLE … GRANT …. TO …. ON … ALTER [TABLE | COLUMN … Examples :
SQL Data Types
SQL Data Types
SQL Data Types (Descriptions)
SQL Data Types (Descriptions)
Aggregate Functions
Aggregate Functions  assist with the  summarization  of large volumes of data.  They return a single result row based on groups of rows, rather than on single rows.  Aggregate functions can appear in select lists and in ORDER BY and HAVING clauses.
Aggregate Functions Function Access SQL Server MySQL SQL Server Oracle Sum SUM SUM SUM SUM SUM Average AVG AVG AVG AVG AVG Count COUNT COUNT COUNT COUNT, COUNT_BIG* COUNT COUNT(*) Standard Deviation STDEV STDEV STD, STDEV STDEVP, VAR, VARP STDEV, STDEV_POP, STDEV_SAMP Minimum MIN MIN MIN MIN MIN Maximum MAX MAX MAX MAX MAX Others CHECKSUM, CHECKSUM_AGG BINARY_CHECKSUM MEDDAN, LAST, GROUPING
Aggregate Functions Effect of NULL on aggregate function output No effect, NULL values are eliminated from the result set. Except for the COUNT function, NULL values are grouped and returned as part of the result set. Use GROUP BY when using aggregate functions in a multiple column in a select statement Use HAVING when using aggregate functions are referred to in the condition.
Aggregate Functions SELECT state, count(*) FROM test WHERE state IN(‘CA’,’LA’) GROUP BY state ORDER BY state SELECT state, count(*) FROM test GROUP BY state HAVING state IN(‘CA’,’LA’) ORDER BY state Which is better?? WHERE CLAUSE restricts the number of rows that are counted HAVING is applied to the result set last after all rows are counted.
Aggregate Functions SELECT state,  SUM (baldue) FROM test WHERE state IN(‘CA’,’LA’) GROUP BY  state ORDER BY state State  Sum(baldue) ===== =========== CA  250.00 CO  58.75  GA  3987.50  LA  0.0 MN  510.00 NY  589.50 TX  62.00 VT  439.00  SELECT state,  SUM (baldue) FROM test WHERE state IN(‘CA’,’LA’) GROUP BY  state HAVING  SUM (baldue) > 0 ORDER BY state State  Sum(baldue) ===== =========== CA  250.00
NULL
NULL What is it?? NULL is not a data value  Indicates the absence of data Using NULL in any equation yields NULL as the result. Must modify NULL via a function to produce non null result. NULLIF()  - SQL Server, MySQL NVL() - Oracle CASE statement Checking for NULL ISNULL()  - SQL Server, MySQL IFNULL() – MySQL
Comparison Operators  & Keywords
Comparison Operators & Keywords Operators Keywords LIKE BETWEEN, IN EXISTS =, !=*, <, >, <=, >=  Oracle *not ISO standard >, <, >=, <>, !=*, !<*, !>* T-SQL *not ISO standard =, >, <, >=, <>, !=*, !<*, !>*, -=*, ->*, -<* T-SQL *not ISO standard
Comparison Operators & Keywords Precedence Operators with higher precedence are applied first When operators have the same precedence they are handled differently depending on the db.  Oracle – applied in no particular order SQL Server – applied in left to right order Parenthesis can be used the control the order of precedence . Evaluation is done from innermost set to outer when nested.
Set Operations
Set Operations   let you combine rows from different sets, locate which rows exist in both sets, or find which rows exist in one set but not the other.
Set Operations UNION [DISTINCT | ALL] SELECT … FROM …. WHERE … UNION SELECT … FROM … WHERE … INTERSECT [DISTINCT | ALL] SELECT … FROM …. WHERE … INTERSECT SELECT … FROM … WHERE … EXCEPT [DISTINCT | ALL]   SELECT … FROM …. WHERE … EXCEPT SELECT … FROM … WHERE …
Set Operators Multiple select statements can be combined using different set operators in the same statement. SELECT … FROM …. WHERE … INTERSECT SELECT … FROM … WHERE … UNION SELECT … FROM … WHERE …
Set Operators In a UNION  both  statements must have same number of columns. NULL values are considered equal. Precedence Operators with higher precedence are applied first When operators have the same precedence they are handled differently depending on the db.  Oracle – applied in no particular order SQL Server – applied in left to right order Parenthesis, (), can be used the control the order of precedence . Evaluation is done from innermost set to outer when nested.
CASE Statement
CASE Statement SELECT    (CASE <expression>    WHEN <value1> THEN <result1>    WHEN <value2> THEN <result2> …      WHEN <valueN> THEN <resultN>     [ ELSE <else-result> ]  END)  FROM … WHERE …
CASE Statement Used to provide if-then-else type logic to SQL Uses two types case expressions; simple and search Simple  expressions provides only an equality check Search  expressions allows the use of comparison operators (AND, OR) between each boolean expression. Can be used in queries or sub queries
Mathematical Operators & Functions
Mathematical Operators & Functions Operators (Division, Subtraction, Multiplication, Addition) Operators with higher precedence are applied first Multiply, Divide and Modulo have higher precedence than Addition and Subtraction Operators with the same precedence are handled differently depending on the db.  Oracle – applied in no particular order SQL Server – applied in left to right order Parenthesis, (), are used to control the order of precedence . Evaluation is done from innermost set to outer when nested.
Mathematical Operators & Functions Functions  absolute value [ABS]  rounding [ROUND]  square root [SQRT]  sign values [SIGN]  ceiling [CEIL] and floor [FLOOR]  exponential value [SIN,COS,TAN] Can be used in most SQL statements (select, update, etc)
Joins
Joins  are one of the basic constructions of  SQL  and  Databases  and as such they combine records from two or more database tables into one set of rows with the same source columns.  These columns can originate from either of the joined tables as well as be formed using expressions and built-in or user-defined functions.
Join Types INNER Only rows satisfying selection criteria from both joined tables are returned. OUTER Left - remaining row from the left joined table are returned along with nulls instead of actual right hand joined table rows. Right - remaining row from the right joined table are returned along with nulls instead of actual left hand joined table rows. Full – remaining rows from both right and left table are returned. SELF Single table is joined to itself; the second table is same as the first but renamed using an alias. CROSS (cartesian product)* All rows are returned from both tables,  no condition is specified, or the condition is always true .
Joins Syntax Cross Join: SELECT <column list> FROM <left joined table>, <right joined table> Inner Join: SELECT <column list>  FROM <left joined table>, <right joined table>  WHERE <join condition>  SELECT <column list> FROM <left joined table>  [INNER] JOIN <right joined table> ON <join condition>
Joins Syntax Outer Join: SELECT <column list>  FROM <left joined table>, <right joined table>  WHERE <join condition>  SELECT <column list> FROM <left joined table>  LEFT | RIGHT | FULL [OUTER] JOIN <right joined table> ON <join condition>
Joins Syntax Outer Join (Oracle): SELECT <column list>  FROM <left joined table>, <right joined table>  WHERE <left joined column> (+) = <right joined table column> Outer Join (DB2): SELECT <column list>  FROM <left joined table>, <right joined table>  WHERE <left joined column> #= <right joined table column>
Joins – Example (Data)
Cross Join – Examples
Inner Join - Examples
Outer Joins - Examples
Subqueries
Subqueries  are queries that are nested inside a SELECT, INSERT, UPDATE or DELETE statement, or inside of another subquery.  A subquery can be used anywhere an expression is allowed .
Subqueries A subquery must be enclosed in the parenthesis.  A subquery must be put in the right hand of the comparison operator A subquery cannot contain a ORDER-BY clause.  A query can contain more than one sub-queries.
Subqueries 3 Subquery Types Single-row subquery - where the subquery returns only one row.  Multiple-row subquery - where the subquery returns multiple rows. Multiple column subquery - where the subquery returns multiple columns.  Another name for these query types is: Correlated Subquery.
Subqueries Correlated Subqueries Are dependent on the their outer query* Will be executed many times while it’s outer queries is being processed, running once for each row selected by the outer query. Can be in the HAVING OR WHERE clauses
Subqueries SELECT  store_name,     sum(quantity)  store_sales,     (SELECT sum(quantity)  FROM sales)/ (SELECT count(*) FROM store) avg_sales FROM store  s, sales  sl  WHERE  s.store_key = sl.store_key  HAVING  sum(quantity) > (SELECT sum(quantity) from sales)/(select count(*)  FROM store)  GROUP BY  store_name;
Views
Views A view is a virtual table based on the result-set of a SQL statement Views contain rows and columns, just like a real table.  The fields in a view are fields from one or more real tables in the database. Views (virtual) always show up-to-date data. The db engine recreates the data using the view’s SQL statement every time a user queries the view.
Views Why create views?? Hide data complexity Security Save space Read-only vs. Update Usually only a single table can be updated ( for inherently updateable views ). For multi-table views usually the tables whose unique index is accessed in the view can be updated. For some DBs only one table in the view may be updated. INSTEAD OF database triggers can be created on any view to make updatable.
View Syntax Create CREATE VIEW <view name> AS SELECT <column list> FROM <table list> WHERE expression Update CREATE OR  REPLACE VIEW <view name> AS SELECT <column list> FROM <table list> WHERE expression Drop DROP VIEW <view name>
View Syntax  (Updatable Views) INSTEAD OF  Triggers Syntax CREATE OR REPLACE TRIGGER <trigger name> INSTEAD OF < INSERT | UPDATE | DELETE > FOR EACH ROW BEGIN … END <trigger name>
View Syntax  (Updatable Views) INSTEAD OF  Triggers Example CREATE OR REPLACE TRIGGER ioft_emp_perm INSTEAD OF DELETE ON employee_permission_view FOR EACH ROW BEGIN    DELETE FROM dept_code   WHERE dept_code = :OLD.dept_code;    UPDATE employee    SET  dept_code = NULL,          mod_user_id = USER,        mod_user_date = SYSDATE    WHERE dept_code = :OLD.dept_code;    DELETE FROM test    WHERE test = 'Z'; END ioft_emp_perm;
Materialized Views
Materialized Views   are created in the same way that ordinary views are created from a query.  The resulting data set is  cached  and can be updated from the original database tables.
Materialized Views Why use Materialized Views?? Data warehousing. Stage tables, summarization (pre-calculation) More efficient access than ordinary views. Have all the advantages of database tables. Names (depending on DB) Materialized View  (Oracle) Materialized Query  (DB2) . Indexed Views  (SQL Server) . Flexviews  (MySQL).
Materialized Views Types of Materialized Views Read only Cannot be updated Updatable Can be update even when disconnected from the master site Are refreshed on demand Consume fewer resources Writable Created with the FOR UPDATE clause Changes are lost when view is refreshed
Materialized Views - Syntax ORACLE: CREATE MATERIALIZED VIEW <name> TABLESPACE <tablespace> BUILD <IMMEDIATE | DEFERRED> AS SELECT … ; DB2: CREATE TABLE <name> AS ( SELECT  … ) [INITIALLY DEFERRED] REFRESH DEFERRED IN <tablespace> INDEXES IN <tablespace>;
Materialized Views - Syntax SQL SERVER: CREATE VIEW <name> WITH SCHEMABINDING AS SELECT … ;
Inline Views
Inline Views   are  select statements  in the FROM-clause of  another select statement .  In-line views are commonly used to simplify queries by removing join operations and condensing several separate queries into a single query.
Inline Views Act as tables in select statements Can be columns in some DBs (SQL Server, Oracle) Known as  Nested Subqueries  in DB2 Cannot  be indexed
Inline Views - Examples Inline View as Table:   SELECT s.first_name   FROM employee s   INNER JOIN (SELECT ID   FROM title   WHERE job_title = 'tester') d  ON s.ID = d.ID;  … OR…    SELECT s.first_name   FROM employee s,   (SELECT ID   FROM title   WHERE job_title = 'tester') d  WHERE s.ID = d.ID;
Inline Views - Examples Inline View as Column:  SELECT cu CompanyName, (SELECT MIN(OrderDate) FROM Orders o WHERE o.CustomerID = cu.CustomerID) AS OrderDate   FROM Customers cu;
Optimizing Select Clauses
Optimizing Select Clauses Why?? Performance improvement Order of listed tables and views Search largest tables as few times as possible Use of functions (or not) Avoid using functions in the where clause Use of database hints/options (Oracle vs. SQL Server) Index selection Search (join) type Order of tables in join
Data Sharing
Data Sharing Global Variables (DB2*, Oracle) DB2 Connect Database PIPES (Oracle, DB2) SQL Data Services (db-as-a-service) “aka db in a cloud” (http://www.azurejournal.com/2009/05/sql-data-services-your-database-in-the-cloud/)
Q&A, Discussion and Problem Solving

More Related Content

What's hot (20)

PPT
SQL Queries
Nilt1234
 
PPTX
Sql operator
Pooja Dixit
 
PPTX
Date and time functions in mysql
V.V.Vanniaperumal College for Women
 
PPT
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
PPTX
Sql commands
Pooja Dixit
 
PPT
Introduction to structured query language (sql)
Sabana Maharjan
 
PPTX
Aggregate function
Rayhan Chowdhury
 
PDF
MySQL JOINS
HripsimeGhaltaghchya
 
PPTX
Sql(structured query language)
Ishucs
 
PPTX
Sql and Sql commands
Knowledge Center Computer
 
PPTX
introdution to SQL and SQL functions
farwa waqar
 
PPTX
SQL Commands
Sachidananda M H
 
PDF
Triggers in SQL | Edureka
Edureka!
 
PPTX
Basic sql Commands
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
ODP
Ms sql-server
Md.Mojibul Hoque
 
PDF
Query optimization in SQL
Abdul Rehman
 
PPT
Advanced sql
Dhani Ahmad
 
PDF
Sql delete, truncate, drop statements
Vivek Singh
 
PPTX
Joins And Its Types
Wings Interactive
 
PPTX
Group By, Having Clause and Order By clause
Deepam Aggarwal
 
SQL Queries
Nilt1234
 
Sql operator
Pooja Dixit
 
Date and time functions in mysql
V.V.Vanniaperumal College for Women
 
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
Sql commands
Pooja Dixit
 
Introduction to structured query language (sql)
Sabana Maharjan
 
Aggregate function
Rayhan Chowdhury
 
Sql(structured query language)
Ishucs
 
Sql and Sql commands
Knowledge Center Computer
 
introdution to SQL and SQL functions
farwa waqar
 
SQL Commands
Sachidananda M H
 
Triggers in SQL | Edureka
Edureka!
 
Basic sql Commands
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Ms sql-server
Md.Mojibul Hoque
 
Query optimization in SQL
Abdul Rehman
 
Advanced sql
Dhani Ahmad
 
Sql delete, truncate, drop statements
Vivek Singh
 
Joins And Its Types
Wings Interactive
 
Group By, Having Clause and Order By clause
Deepam Aggarwal
 

Viewers also liked (20)

PPTX
advanced sql(database)
welcometofacebook
 
PDF
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Beat Signer
 
PPTX
SQL Basics
Hammad Rasheed
 
PPT
Sql ppt
Anuja Lad
 
PDF
Oracle sql & plsql
Sid Xing
 
PDF
Oracle Essentials Oracle Database 11g
Paola Andrea Gonzalez Montoya
 
PPTX
MS Sql Server: Advanced Query Concepts
DataminingTools Inc
 
PPT
SQL : introduction
Shakila Mahjabin
 
PPT
SQL Tutorial - Basic Commands
1keydata
 
PPTX
Hasil tes UKG sebagai cermin dari kualitas pendidikan guru di Indonesia
Vina Serevina
 
PPTX
Advanced SQL Webinar
Ram Kedem
 
DOCX
Master of Computer Application (MCA) – Semester 4 MC0077
Aravind NC
 
PDF
Performance Tuning Best Practices
webhostingguy
 
PDF
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
Zohar Elkayam
 
PPTX
Exploring Advanced SQL Techniques Using Analytic Functions
Zohar Elkayam
 
PDF
Join-fu: The Art of SQL Tuning for MySQL
ZendCon
 
PPTX
Step By Step How To Install Oracle XE
Achmad Solichin
 
PDF
Tutorial Instalisasi Oracle 10g dan Setting User
Imam Halim Mursyidin
 
PPT
Intro oracle10gexpress
jatin Sareen
 
PPT
Intro to Application Express
José Angel Ibarra Espinosa
 
advanced sql(database)
welcometofacebook
 
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Beat Signer
 
SQL Basics
Hammad Rasheed
 
Sql ppt
Anuja Lad
 
Oracle sql & plsql
Sid Xing
 
Oracle Essentials Oracle Database 11g
Paola Andrea Gonzalez Montoya
 
MS Sql Server: Advanced Query Concepts
DataminingTools Inc
 
SQL : introduction
Shakila Mahjabin
 
SQL Tutorial - Basic Commands
1keydata
 
Hasil tes UKG sebagai cermin dari kualitas pendidikan guru di Indonesia
Vina Serevina
 
Advanced SQL Webinar
Ram Kedem
 
Master of Computer Application (MCA) – Semester 4 MC0077
Aravind NC
 
Performance Tuning Best Practices
webhostingguy
 
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
Zohar Elkayam
 
Exploring Advanced SQL Techniques Using Analytic Functions
Zohar Elkayam
 
Join-fu: The Art of SQL Tuning for MySQL
ZendCon
 
Step By Step How To Install Oracle XE
Achmad Solichin
 
Tutorial Instalisasi Oracle 10g dan Setting User
Imam Halim Mursyidin
 
Intro oracle10gexpress
jatin Sareen
 
Intro to Application Express
José Angel Ibarra Espinosa
 
Ad

Similar to Advanced Sql Training (20)

PPT
Module03
Sridhar P
 
PPTX
1. dml select statement reterive data
Amrit Kaur
 
PDF
SQL Overview
Stewart Rogers
 
PPTX
OracleSQLraining.pptx
Rajendra Jain
 
PPTX
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
PPT
Transact SQL (T-SQL) for Beginners (A New Hope)
Andrea Allred
 
PDF
Sql overview-1232931296681161-1
sagaroceanic11
 
PPTX
Day-2 SQL Theory_V1.pptx
uzmasulthana3
 
ODP
Oracle SQL Advanced
Dhananjay Goel
 
PPTX
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
PPT
SQL select statement and functions
Vikas Gupta
 
PPTX
vnd.openxmlformats-officedocument.presentationml.presentation&rendition=1.pptx
jaijsr12345
 
PDF
Tech Jam 01 - Database Querying
Rodger Oates
 
PDF
0808.pdf
ssuser0562f1
 
PPTX
SQL Fundamentals
Brian Foote
 
PPT
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
MuhamedAhmed35
 
PPTX
SQL Server Learning Drive
TechandMate
 
PPT
Toc
Sudharsan S
 
PPT
Toc
Sudharsan S
 
Module03
Sridhar P
 
1. dml select statement reterive data
Amrit Kaur
 
SQL Overview
Stewart Rogers
 
OracleSQLraining.pptx
Rajendra Jain
 
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
Transact SQL (T-SQL) for Beginners (A New Hope)
Andrea Allred
 
Sql overview-1232931296681161-1
sagaroceanic11
 
Day-2 SQL Theory_V1.pptx
uzmasulthana3
 
Oracle SQL Advanced
Dhananjay Goel
 
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
SQL select statement and functions
Vikas Gupta
 
vnd.openxmlformats-officedocument.presentationml.presentation&rendition=1.pptx
jaijsr12345
 
Tech Jam 01 - Database Querying
Rodger Oates
 
0808.pdf
ssuser0562f1
 
SQL Fundamentals
Brian Foote
 
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
MuhamedAhmed35
 
SQL Server Learning Drive
TechandMate
 
Ad

Advanced Sql Training

  • 1. Advanced SQL Training Seminar William Bishop August 17 th , 2009
  • 2. Content SQL Statement Types SQL Data Types Aggregate Functions NULL Comparison Operators & Keywords Set Operations CASE Statement Mathematical Operators & Functions Joins Subquerys Views Materialized Views Inline Views Optimizing Select Clauses Data Sharing Q&A, Discussion & Problem Solving
  • 3. SQL (Structured Query Language) is a database computer language designed for managing data in relational database management systems (RDBMS). Its scope includes data query and update, schema creation and modification , and data access control .
  • 5. SQL Statement Types DML - stands for Data Manipulation Language , it’s the part of SQL that deals with querying updating and inserting records in tables and views. DDL – stands for Data Definition Language , it’s the part of SQL that deals with the construction and alteration of database structures like tables, views and the further entities inside these tables like columns. It may be used to set the properties of columns as well.
  • 6. SQL Statement Types DML SELECT … FROM … WHERE …. INSERT INTO …. WHERE … DELETE FROM …. WHERE … UPDATE … WHERE …. DDL CREATE [TABLE | VIEW | ROLE … DROP [TABLE | VIEW | ROLE … GRANT …. TO …. ON … ALTER [TABLE | COLUMN … Examples :
  • 9. SQL Data Types (Descriptions)
  • 10. SQL Data Types (Descriptions)
  • 12. Aggregate Functions assist with the summarization of large volumes of data. They return a single result row based on groups of rows, rather than on single rows. Aggregate functions can appear in select lists and in ORDER BY and HAVING clauses.
  • 13. Aggregate Functions Function Access SQL Server MySQL SQL Server Oracle Sum SUM SUM SUM SUM SUM Average AVG AVG AVG AVG AVG Count COUNT COUNT COUNT COUNT, COUNT_BIG* COUNT COUNT(*) Standard Deviation STDEV STDEV STD, STDEV STDEVP, VAR, VARP STDEV, STDEV_POP, STDEV_SAMP Minimum MIN MIN MIN MIN MIN Maximum MAX MAX MAX MAX MAX Others CHECKSUM, CHECKSUM_AGG BINARY_CHECKSUM MEDDAN, LAST, GROUPING
  • 14. Aggregate Functions Effect of NULL on aggregate function output No effect, NULL values are eliminated from the result set. Except for the COUNT function, NULL values are grouped and returned as part of the result set. Use GROUP BY when using aggregate functions in a multiple column in a select statement Use HAVING when using aggregate functions are referred to in the condition.
  • 15. Aggregate Functions SELECT state, count(*) FROM test WHERE state IN(‘CA’,’LA’) GROUP BY state ORDER BY state SELECT state, count(*) FROM test GROUP BY state HAVING state IN(‘CA’,’LA’) ORDER BY state Which is better?? WHERE CLAUSE restricts the number of rows that are counted HAVING is applied to the result set last after all rows are counted.
  • 16. Aggregate Functions SELECT state, SUM (baldue) FROM test WHERE state IN(‘CA’,’LA’) GROUP BY state ORDER BY state State Sum(baldue) ===== =========== CA 250.00 CO 58.75 GA 3987.50 LA 0.0 MN 510.00 NY 589.50 TX 62.00 VT 439.00 SELECT state, SUM (baldue) FROM test WHERE state IN(‘CA’,’LA’) GROUP BY state HAVING SUM (baldue) > 0 ORDER BY state State Sum(baldue) ===== =========== CA 250.00
  • 17. NULL
  • 18. NULL What is it?? NULL is not a data value Indicates the absence of data Using NULL in any equation yields NULL as the result. Must modify NULL via a function to produce non null result. NULLIF() - SQL Server, MySQL NVL() - Oracle CASE statement Checking for NULL ISNULL() - SQL Server, MySQL IFNULL() – MySQL
  • 19. Comparison Operators & Keywords
  • 20. Comparison Operators & Keywords Operators Keywords LIKE BETWEEN, IN EXISTS =, !=*, <, >, <=, >=  Oracle *not ISO standard >, <, >=, <>, !=*, !<*, !>* T-SQL *not ISO standard =, >, <, >=, <>, !=*, !<*, !>*, -=*, ->*, -<* T-SQL *not ISO standard
  • 21. Comparison Operators & Keywords Precedence Operators with higher precedence are applied first When operators have the same precedence they are handled differently depending on the db. Oracle – applied in no particular order SQL Server – applied in left to right order Parenthesis can be used the control the order of precedence . Evaluation is done from innermost set to outer when nested.
  • 23. Set Operations let you combine rows from different sets, locate which rows exist in both sets, or find which rows exist in one set but not the other.
  • 24. Set Operations UNION [DISTINCT | ALL] SELECT … FROM …. WHERE … UNION SELECT … FROM … WHERE … INTERSECT [DISTINCT | ALL] SELECT … FROM …. WHERE … INTERSECT SELECT … FROM … WHERE … EXCEPT [DISTINCT | ALL] SELECT … FROM …. WHERE … EXCEPT SELECT … FROM … WHERE …
  • 25. Set Operators Multiple select statements can be combined using different set operators in the same statement. SELECT … FROM …. WHERE … INTERSECT SELECT … FROM … WHERE … UNION SELECT … FROM … WHERE …
  • 26. Set Operators In a UNION both statements must have same number of columns. NULL values are considered equal. Precedence Operators with higher precedence are applied first When operators have the same precedence they are handled differently depending on the db. Oracle – applied in no particular order SQL Server – applied in left to right order Parenthesis, (), can be used the control the order of precedence . Evaluation is done from innermost set to outer when nested.
  • 28. CASE Statement SELECT    (CASE <expression>    WHEN <value1> THEN <result1>    WHEN <value2> THEN <result2> …      WHEN <valueN> THEN <resultN>     [ ELSE <else-result> ]  END) FROM … WHERE …
  • 29. CASE Statement Used to provide if-then-else type logic to SQL Uses two types case expressions; simple and search Simple expressions provides only an equality check Search expressions allows the use of comparison operators (AND, OR) between each boolean expression. Can be used in queries or sub queries
  • 31. Mathematical Operators & Functions Operators (Division, Subtraction, Multiplication, Addition) Operators with higher precedence are applied first Multiply, Divide and Modulo have higher precedence than Addition and Subtraction Operators with the same precedence are handled differently depending on the db. Oracle – applied in no particular order SQL Server – applied in left to right order Parenthesis, (), are used to control the order of precedence . Evaluation is done from innermost set to outer when nested.
  • 32. Mathematical Operators & Functions Functions absolute value [ABS] rounding [ROUND] square root [SQRT] sign values [SIGN] ceiling [CEIL] and floor [FLOOR] exponential value [SIN,COS,TAN] Can be used in most SQL statements (select, update, etc)
  • 33. Joins
  • 34. Joins are one of the basic constructions of SQL and Databases and as such they combine records from two or more database tables into one set of rows with the same source columns. These columns can originate from either of the joined tables as well as be formed using expressions and built-in or user-defined functions.
  • 35. Join Types INNER Only rows satisfying selection criteria from both joined tables are returned. OUTER Left - remaining row from the left joined table are returned along with nulls instead of actual right hand joined table rows. Right - remaining row from the right joined table are returned along with nulls instead of actual left hand joined table rows. Full – remaining rows from both right and left table are returned. SELF Single table is joined to itself; the second table is same as the first but renamed using an alias. CROSS (cartesian product)* All rows are returned from both tables, no condition is specified, or the condition is always true .
  • 36. Joins Syntax Cross Join: SELECT <column list> FROM <left joined table>, <right joined table> Inner Join: SELECT <column list> FROM <left joined table>, <right joined table> WHERE <join condition> SELECT <column list> FROM <left joined table> [INNER] JOIN <right joined table> ON <join condition>
  • 37. Joins Syntax Outer Join: SELECT <column list> FROM <left joined table>, <right joined table> WHERE <join condition> SELECT <column list> FROM <left joined table> LEFT | RIGHT | FULL [OUTER] JOIN <right joined table> ON <join condition>
  • 38. Joins Syntax Outer Join (Oracle): SELECT <column list> FROM <left joined table>, <right joined table> WHERE <left joined column> (+) = <right joined table column> Outer Join (DB2): SELECT <column list> FROM <left joined table>, <right joined table> WHERE <left joined column> #= <right joined table column>
  • 40. Cross Join – Examples
  • 41. Inner Join - Examples
  • 42. Outer Joins - Examples
  • 44. Subqueries are queries that are nested inside a SELECT, INSERT, UPDATE or DELETE statement, or inside of another subquery. A subquery can be used anywhere an expression is allowed .
  • 45. Subqueries A subquery must be enclosed in the parenthesis. A subquery must be put in the right hand of the comparison operator A subquery cannot contain a ORDER-BY clause. A query can contain more than one sub-queries.
  • 46. Subqueries 3 Subquery Types Single-row subquery - where the subquery returns only one row. Multiple-row subquery - where the subquery returns multiple rows. Multiple column subquery - where the subquery returns multiple columns. Another name for these query types is: Correlated Subquery.
  • 47. Subqueries Correlated Subqueries Are dependent on the their outer query* Will be executed many times while it’s outer queries is being processed, running once for each row selected by the outer query. Can be in the HAVING OR WHERE clauses
  • 48. Subqueries SELECT  store_name,    sum(quantity)  store_sales,    (SELECT sum(quantity) FROM sales)/ (SELECT count(*) FROM store) avg_sales FROM store  s, sales  sl WHERE s.store_key = sl.store_key HAVING sum(quantity) > (SELECT sum(quantity) from sales)/(select count(*) FROM store) GROUP BY  store_name;
  • 49. Views
  • 50. Views A view is a virtual table based on the result-set of a SQL statement Views contain rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. Views (virtual) always show up-to-date data. The db engine recreates the data using the view’s SQL statement every time a user queries the view.
  • 51. Views Why create views?? Hide data complexity Security Save space Read-only vs. Update Usually only a single table can be updated ( for inherently updateable views ). For multi-table views usually the tables whose unique index is accessed in the view can be updated. For some DBs only one table in the view may be updated. INSTEAD OF database triggers can be created on any view to make updatable.
  • 52. View Syntax Create CREATE VIEW <view name> AS SELECT <column list> FROM <table list> WHERE expression Update CREATE OR REPLACE VIEW <view name> AS SELECT <column list> FROM <table list> WHERE expression Drop DROP VIEW <view name>
  • 53. View Syntax (Updatable Views) INSTEAD OF Triggers Syntax CREATE OR REPLACE TRIGGER <trigger name> INSTEAD OF < INSERT | UPDATE | DELETE > FOR EACH ROW BEGIN … END <trigger name>
  • 54. View Syntax (Updatable Views) INSTEAD OF Triggers Example CREATE OR REPLACE TRIGGER ioft_emp_perm INSTEAD OF DELETE ON employee_permission_view FOR EACH ROW BEGIN   DELETE FROM dept_code   WHERE dept_code = :OLD.dept_code;   UPDATE employee   SET dept_code = NULL,       mod_user_id = USER,       mod_user_date = SYSDATE   WHERE dept_code = :OLD.dept_code;   DELETE FROM test   WHERE test = 'Z'; END ioft_emp_perm;
  • 56. Materialized Views are created in the same way that ordinary views are created from a query. The resulting data set is cached and can be updated from the original database tables.
  • 57. Materialized Views Why use Materialized Views?? Data warehousing. Stage tables, summarization (pre-calculation) More efficient access than ordinary views. Have all the advantages of database tables. Names (depending on DB) Materialized View (Oracle) Materialized Query (DB2) . Indexed Views (SQL Server) . Flexviews (MySQL).
  • 58. Materialized Views Types of Materialized Views Read only Cannot be updated Updatable Can be update even when disconnected from the master site Are refreshed on demand Consume fewer resources Writable Created with the FOR UPDATE clause Changes are lost when view is refreshed
  • 59. Materialized Views - Syntax ORACLE: CREATE MATERIALIZED VIEW <name> TABLESPACE <tablespace> BUILD <IMMEDIATE | DEFERRED> AS SELECT … ; DB2: CREATE TABLE <name> AS ( SELECT … ) [INITIALLY DEFERRED] REFRESH DEFERRED IN <tablespace> INDEXES IN <tablespace>;
  • 60. Materialized Views - Syntax SQL SERVER: CREATE VIEW <name> WITH SCHEMABINDING AS SELECT … ;
  • 62. Inline Views are select statements in the FROM-clause of another select statement . In-line views are commonly used to simplify queries by removing join operations and condensing several separate queries into a single query.
  • 63. Inline Views Act as tables in select statements Can be columns in some DBs (SQL Server, Oracle) Known as Nested Subqueries in DB2 Cannot be indexed
  • 64. Inline Views - Examples Inline View as Table:   SELECT s.first_name FROM employee s INNER JOIN (SELECT ID FROM title WHERE job_title = 'tester') d  ON s.ID = d.ID; … OR…   SELECT s.first_name FROM employee s, (SELECT ID FROM title WHERE job_title = 'tester') d  WHERE s.ID = d.ID;
  • 65. Inline Views - Examples Inline View as Column:  SELECT cu CompanyName, (SELECT MIN(OrderDate) FROM Orders o WHERE o.CustomerID = cu.CustomerID) AS OrderDate FROM Customers cu;
  • 67. Optimizing Select Clauses Why?? Performance improvement Order of listed tables and views Search largest tables as few times as possible Use of functions (or not) Avoid using functions in the where clause Use of database hints/options (Oracle vs. SQL Server) Index selection Search (join) type Order of tables in join
  • 69. Data Sharing Global Variables (DB2*, Oracle) DB2 Connect Database PIPES (Oracle, DB2) SQL Data Services (db-as-a-service) “aka db in a cloud” (http://www.azurejournal.com/2009/05/sql-data-services-your-database-in-the-cloud/)
  • 70. Q&A, Discussion and Problem Solving

Editor's Notes

  • #19: Select NULL + 1 FROM dual vs Select NVL(NULL,0) + 1 FROM dual
  • #21: SELECT … FROM … WHERE … LIKE (%...) SELECT … FROM … WHERE IN (… ,… ,…) OR SELECT … FROM … WHERE EXISTS (SELECT … FROM … WHERE …) SELECT … FROM … WHERE IN (SELECT … FROM … WHERE …)
  • #22: SELECT … FROM … WHERE … LIKE (%...) SELECT … FROM … WHERE IN (… ,… ,…) OR SELECT … FROM … WHERE EXISTS (SELECT … FROM … WHERE …) SELECT … FROM … WHERE IN (SELECT … FROM … WHERE …)
  • #25: Keywords ALL and DISTINCT can be used depending on the language to modify set operators to return either the entire result set (ie: duplicates) by specifying ALL or or to exclude the duplicates by specifying DISTINCT. The SQL standard does not enforce keyword Distinct and some DBMSs for example Oracle and SQL Server do not even allow it, therefore if you see just Union, Except or Intersect - these actually mean Union Distinct, Except Distinct and Intersect Distinct. Facts to remember: Column count must be the same; Data types of retrieved columns should match or at least should be implicitly convertible by database; Usually returned column names are taken from the first query; Order by clauses for each individual query except the last one cannot be there at all as is the case with Oracle or are ignored in MySQL.
  • #26: Facts to remember: Check DB documentation for the order of operators, because for example Oracle executes operators starting from left to right, but DB2 executes Intersect operators first;
  • #27: SELECT … FROM … WHERE … LIKE (%...) SELECT … FROM … WHERE IN (… ,… ,…) OR SELECT … FROM … WHERE EXISTS (SELECT … FROM … WHERE …) SELECT … FROM … WHERE IN (SELECT … FROM … WHERE …)
  • #30: SELECT last_name, commission_pct,    (CASE commission_pct      WHEN 0.1 THEN ‘Low’      WHEN 0.15 THEN ‘Average’      WHEN 0.2 THEN ‘High’      ELSE ‘N/A’   END ) Commission FROM employees ORDER BY last_name;   SELECT last_name, job_id, salary, (CASE      WHEN job_id LIKE &apos;SA_MAN&apos; AND salary &lt; 12000 THEN &apos;10%&apos;   WHEN job_id LIKE &apos;SA_MAN&apos; AND salary &gt;= 12000 THEN &apos;15%&apos;      WHEN job_id LIKE &apos;IT_PROG&apos; AND salary &lt; 9000 THEN &apos;8%&apos;      WHEN job_id LIKE &apos;IT_PROG&apos; AND salary &gt;= 9000 THEN &apos;12%&apos;      ELSE &apos;NOT APPLICABLE&apos;    END ) Raise FROM employees;  
  • #41: Facts to remember Usually cross joins are used quite rarely; some of the scenarios could be as follows: Possibility to generate high amount of rows. As we can see from relatively small tables there is possibility to get quite monstrous numbers. Find out all possible row combinations of some tables. Mostly this is useful for reports where one needs to generate all combinations for example all nationalities x genders for persons. To join a table with just one row. Most often used to get some configuration parameters.
  • #42: Facts to remember: All following facts are relevant to almost every join type, however the inner join is the most common join type. Databases are designed to do joins efficiently. Not client, not middle tier, but databases. Creating data model usually normalization is being done mostly to avoid data duplication. To show data in person-readable form, joining is one of the necessary prerequisites. Inner join is one of the most common join types and it should be done in database. Database means efficiently in the place where data resides and not in client/middle tier cycling through (possibly) many cycles. It should be noted that other clauses for SQL statements e.g. GROUP BY, HAVING, ORDER BY are of course usable for SQL statements with joins. One must be cautious using join conditions with columns without NOT NULL constraint. Comparing NULL values with different explicit values or even Nulls is probably a bit counter-intuitive on the first sight. The position of additional conditions is irrelevant for INNER joins. These can be written both as join condition and in WHERE clause, result is the same. For Inner join it is not relevant, which table is first one and which second one.
  • #43: Facts to remember: Outer joins should be used only when necessary. If it is possible (data model and business data allows) inner join should be used. Inner join offers greater flexibility for optimizer and doesn&apos;t mislead people to the thoughts that some rows of one cannot be joined to other table. Unlike Inner joins it is important where the condition is written - either as join condition or in WHERE clause. Not all Database management systems support Full outer joins. Use a (the set operator) UNION of Left and Right joins to simulate a FULL outer join. Oracle (MySQL) outer join operator has more restrictions than more modern ANSI syntax, for example: It doesn&apos;t support Full Outer join; It cannot be used together with modern syntax; Join condition cannot contain logical OR and IN operators.
  • #52: If the database system can determine the reverse mapping from the view schema to the schema of the underlying base tables, then the view is updatable. Insert, update and delete operations can be performed on updatable views. Notes on Updatable Views An updatable view is one you can use to insert, update, or delete base table rows. You can create a view to be inherently updatable, or you can create an INSTEAD OF trigger on any view to make it updatable. To learn whether and in what ways the columns of an inherently updatable view can be modified, query the USER_UPDATABLE_COLUMNS data dictionary view. The information displayed by this view is meaningful only for inherently updatable views. For a view to be inherently updatable, the following conditions must be met: Each column in the view must map to a column of a single table. For example, if a view column maps to the output of a TABLE clause (an unnested collection), then the view is not inherently updatable. The view must not contain any of the following constructs: A set operator a DISTINCT operator An aggregate or analytic function A GROUP BY, ORDER BY, MODEL, CONNECT BY, or START WITH clause A collection expression in a SELECT list A subquery in a SELECT list A subquery designated WITH READ ONLY Joins, with some exceptions, as documented in Oracle Database Administrator&apos;s Guide In addition, if an inherently updatable view contains pseudocolumns or expressions, then you cannot update base table rows with an UPDATE statement that refers to any of these pseudocolumns or expressions.
  • #53: If you want a join view to be updatable, then all of the following conditions must be true: 1.  The DML statement must affect only one table underlying the join. 2.  For an INSERT statement, the view must not be created WITH CHECK OPTION, and all columns into which values are inserted must come from a key-preserved table. A key-preserved table is one for which every primary key or unique key value in the base table is also unique in the join view. 3.  For an UPDATE statement, all columns updated must be extracted from a key-preserved table. If the view was created WITH CHECK OPTION, then join columns and columns taken from tables that are referenced more than once in the view must be shielded from UPDATE. For a DELETE statement, if the join results in more than one key-preserved table, then Oracle Database deletes from the first table named in the FROM clause, whether or not the view was created WITH CHECK OPTION.