Advanced SQL Topics Edward Wu.

Slides:



Advertisements
Similar presentations
1
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
Structured Query Language (SQL)
UNITED NATIONS Shipment Details Report – January 2006.
RXQ Customer Enrollment Using a Registration Agent (RA) Process Flow Diagram (Move-In) Customer Supplier Customer authorizes Enrollment ( )
1 Hyades Command Routing Message flow and data translation.
Using the Set Operators
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Microsoft Access 2007 Advanced Level. © Cheltenham Courseware Pty. Ltd. Slide No 2 Forms Customisation.
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
Create an Application Title 1A - Adult Chapter 3.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
SQL: The Query Language Part 2
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
Information Systems Today: Managing in the Digital World
Database Performance Tuning and Query Optimization
PP Test Review Sections 6-1 to 6-6
Bright Futures Guidelines Priorities and Screening Tables
Chapter 7 Working with Databases and MySQL
© Abdou Illia MIS Spring 2014
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
1 Chapter 12 Data Manipulation Language (DML) View Dr. Chitsaz Objectives Definition Create a view Retrieve data from a view Insert, delete, update to/from.
Yong Choi School of Business CSU, Bakersfield
Microsoft Access.
Displaying Data from Multiple Tables
Chapter Information Systems Database Management.
EIS Bridge Tool and Staging Tables September 1, 2009 Instructor: Way Poteat Slide: 1.
Access Tables 1. Creating a Table Design View Define each field and its properties Data Sheet View Essentially spreadsheet Enter fields You must go to.
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23, 03-46,
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
Adding Up In Chunks.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Page 1 of 43 To the ETS – Bidding Query by Map Online Training Course Welcome This training module provides the procedures for using Query by Map for a.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
Subtraction: Adding UP
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Analyzing Genes and Genomes
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Energy Generation in Mitochondria and Chlorplasts
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
Management Information Systems, 10/e
South Dakota Library Network MetaLib User Interface South Dakota Library Network 1200 University, Unit 9672 Spearfish, SD © South Dakota.
Guide to Oracle10G1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
ITBIS373 Database Development
Creating and Maintaining
Contents Preface I Introduction Lesson Objectives I-2
Presentation transcript:

Advanced SQL Topics Edward Wu

Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries Useful Function (Decode, Case, Rownum)

Database Indexes Similar to an index in a book Table with list of sorted data values and corresponding physical location Used to speed searches Primary key indexed automatically Unlimited number allowed, but more indexes means more processing time for action queries (insert, update, delete)

Creating an Index Create index after table data is loaded CREATE INDEX index_name ON tablename (index_fieldname); Convention for naming index: tablename_fieldname.

Composite Index Contains multiple (up to 16) sorted columns Used for queries with multiple search conditions CREATE INDEX index_name ON tablename(index_fieldname1, index_fieldname2, …);

Viewing Index Information Use data dictionary view USER_INDEXES

Dropping an Index If an index is no longer needed or does not improve performance, delete it DROP INDEX index_name;

Use an Index When Table contains a large number of records (a rule of thumb is that a large table contains over 100,000 records) The field contains a wide range of values The field contains a large number of NULL values Application queries frequently use the field in a search condition or join condition Most queries retrieve less than 2% to 4% of the table rows

Do Not Use an Index When The table does not contain a large number of records Applications do not use the proposed index field in a query search condition Most queries retrieve more than 2% to 4% of the table records Applications frequently insert or modify table data

Database Views Logical table based on a query Does not physically exist in the database Presents data in a different format from underlying tables Uses: Security Simplifying complex queries

Database Views Creating a view: CREATE VIEW view_name AS SQL_command; Views can be queried just like tables: SELECT * FROM view_name;

Simple Views Based on SQL query that retrieves data from only one table (limit data for different user) View can support all table DML operations: INSERT UPDATE DELETE

Complex Views Based on query that retrieves data from multiple tables Can only be used to support SELECT operations No DML operations supported For reporting purposes. (Sales Report) Can create index on view.

Join Queries Retrieve data from multiple tables by joining tables using foreign key references Join query types: Outer Self Inequality

Inner Join with Group by Find the total inventory amount on hand for each item Different price

Outer Joins Limitation of inner joins: some records may be omitted if corresponding records don’t exist in one of the tables Example: retrieve records for all students, along with their corresponding ENROLLMENT information

Outer Joins Student 105 (Michael Connoly) does not have any ENROLLMENT records

Outer Joins No records retrieved for Michael:

Outer Joins To include records in first (inner) table, even when they do not have matching records in second (outer) table, place outer join marker (+) beside outer table name in join clause

Outer Joins Outer join marker

A B A.id = B.id A B (+)A.id = B.id A B A.id = B.id(+) Note: Orange is the area that is returned A B A.id = B.id A B (+)A.id = B.id A B A.id = B.id(+)

Self Joins Used to join a table to itself when the table has a hierarchical relationship

Self Joins To create a self-join, you need to create a table alias, which gives an alternate name to the table so you can create a join condition Syntax to create table alias in FROM clause: FROM table1 alias1, table2 alias2

PARENT_PROJECT Self Joins SUB_PROJECT PROJECT

Self Join Example

Inequality Joins Join created by placing making join condition satisfy an inequality condition Only makes sense when primary/foreign key values are not surrogate keys

Inequality Joins

Nested Queries Created when a subquery is nested within a main query Main query: first query listed in SELECT command Subquery: retrieves one or more values that specify the main query’s search condition

Nested Query Where Subquery Returns a Single Value Syntax: SELECT column1, column2, … FROM table1, table2, … WHERE join conditions AND search_column1 = (SELECT column1 FROM table1, table2, … WHERE search and join conditions) Subquery that returns one value Note: subquery is just another sql command contained within a parenthesis

Example Nested Query Where Subquery Returns a Single Value Find all the student that goes to Amanda class Find the class that Amanda goes to.

Nested Query Where Subquery Returns Multiple Values Syntax: SELECT column1, column2, … FROM table1, table2, … WHERE join conditions AND search_column1 IN (SELECT column1 FROM table1, table2, … WHERE search and join conditions) Subquery that returns multiple values

Example Nested Query Where Subquery Returns Multiple Values Find all the students that enrolled in c_sec_id in (1010, 1011) Find all the course that Amanda enrolled in. which returned c_sec_id: 1010, 1011

Example of nested queries (cont…) select c_sec_id from student s, enrollment e, course_section c where s.s_id = e.s_id and s_last = 'Mobley‘ and s_first = 'Amanda‘ and c_sec_id in (1000, 1002, 1007, 1010); Find the c_sec_id that use bldg_code=‘CR’ which is 1000, 1002, 1007, 1010

Remember previous example of Inner Join with Group by Find the total inventory amount on hand for each item Different price

Creating Table Alias from select statement Add an extra column for the percentage of inventory on hand

Using Set Operators in Queries Performs set operations on outputs of two unrelated queries Both queries must have: same number of display fields corresponding display fields must have same data type

Query Set Operators UNION: combines results, suppresses duplicate rows UNION ALL: combines results, displays duplicates INTERSECT: finds matching rows MINUS: returns the difference between returned record sets

Decode Function Facilitates conditional inquiries by doing the work of a CASE or IF-THEN-ELSE statement Syntax Decode(column/expression, search1, result1 [, search2, result2, …, ] [, default ] );

The Decode Function The DECODE function decodes an expression in a way similar to the IF-THEN-ELSE logic used in various languages. The DECODE function decodes expression after comparing it to each search value. If the expression is the same as search, result is returned. Note: If the default value is omitted, a null value is returned where a search value does not match any of the result value.

Using the DECODE Function

Row Num

Search Result for web page