Advanced SQL - Subqueries and Complex Joins



Similar documents
Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Oracle Database 12c: Introduction to SQL Ed 1.1

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database: SQL and PL/SQL Fundamentals

Relational Division and SQL

In this Lecture SQL SELECT. Example Tables. SQL SELECT Overview. WHERE Clauses. DISTINCT and ALL SQL SELECT. For more information

Oracle Database: Introduction to SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: Introduction to SQL

IENG2004 Industrial Database and Systems Design. Microsoft Access I. What is Microsoft Access? Architecture of Microsoft Access

Introduction to Querying & Reporting with SQL Server

THE OPEN UNIVERSITY OF TANZANIA FACULTY OF SCIENCE TECHNOLOGY AND ENVIRONMENTAL STUDIES BACHELOR OF SIENCE IN DATA MANAGEMENT

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

Effective Use of SQL in SAS Programming

Querying Microsoft SQL Server

Programming with SQL

Introduction to Microsoft Jet SQL

Querying Microsoft SQL Server 2012

Course ID#: W 35 Hrs. Course Content

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Course 10774A: Querying Microsoft SQL Server 2012

Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Published: May 25, 2012 Language(s): English Audience(s): IT Professionals

A basic create statement for a simple student table would look like the following.

Querying Microsoft SQL Server 20461C; 5 days

SQL Query Evaluation. Winter Lecture 23

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

Oracle Database: SQL and PL/SQL Fundamentals

D B M G Data Base and Data Mining Group of Politecnico di Torino

MOC QUERYING MICROSOFT SQL SERVER

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

SQL SELECT Query: Intermediate

Oracle Database: Introduction to SQL

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Relational Database: Additional Operations on Relations; SQL

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

Instant SQL Programming

SQL Programming. Student Workbook

Oracle Database 10g: Introduction to SQL

Data Tool Platform SQL Development Tools

SQL Server. 1. What is RDBMS?

DATABASE DESIGN AND IMPLEMENTATION II SAULT COLLEGE OF APPLIED ARTS AND TECHNOLOGY SAULT STE. MARIE, ONTARIO. Sault College

AV-005: Administering and Implementing a Data Warehouse with SQL Server 2014

Querying Microsoft SQL Server 2012

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Oracle 10g PL/SQL Training

Advanced Query for Query Developers

Oracle Database 11g SQL

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

The process of database development. Logical model: relational DBMS. Relation

SQL Server Developer Training Program. Topics Covered

Using Microsoft Access

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

Business Intelligence Extensions for SPARQL

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

In this session, we use the table ZZTELE with approx. 115,000 records for the examples. The primary key is defined on the columns NAME,VORNAME,STR

Chapter 2 Probability Topics SPSS T tests

SQL NULL s, Constraints, Triggers

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

SQL. by Steven Holzner, Ph.D. ALPHA. A member of Penguin Group (USA) Inc.

Databases 2011 The Relational Model and SQL

OLH: Oracle Loader for Hadoop OSCH: Oracle SQL Connector for Hadoop Distributed File System (HDFS)

Welcome to the topic on queries in SAP Business One.

Financial Data Access with SQL, Excel & VBA

Information Systems (Informationssysteme)

IT2304: Database Systems 1 (DBS 1)

ETL TESTING TRAINING

MySQL for Beginners Ed 3

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

IT2305 Database Systems I (Compulsory)

Database Design and Database Programming with SQL - 5 Day In Class Event Day 1 Activity Start Time Length

Toad for Data Analysts, Tips n Tricks

Querying Microsoft SQL Server (20461) H8N61S

Using Temporary Tables to Improve Performance for SQL Data Services

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

How To Learn To Write A Report In A Database On A Microsoft Powerbook

ICAB4136B Use structured query language to create database structures and manipulate data

Using Excel in Research. Hui Bian Office for Faculty Excellence

SQL SUBQUERIES: Usage in Clinical Programming. Pavan Vemuri, PPD, Morrisville, NC

SQL Injection for newbie

Efficient Data Access and Data Integration Using Information Objects Mica J. Block

3.GETTING STARTED WITH ORACLE8i

Information Systems SQL. Nikolaj Popov

LearnFromGuru Polish your knowledge

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

Netezza SQL Class Outline

Unit 3. Retrieving Data from Multiple Tables

Querying Microsoft SQL Server Querying Microsoft SQL Server D. Course 10774A: Course Det ails. Co urse Outline

Chapter 1 Overview of the SQL Procedure

RECURSIVE COMMON TABLE EXPRESSIONS DATABASE IN ORACLE. Iggy Fernandez, Database Specialists INTRODUCTION

Appendix A Practices and Solutions

Quiz 2: Database Systems I Instructor: Hassan Khosravi Spring 2012 CMPT 354

Transcription:

Advanced SQL - Subqueries and Complex Joins Outline for Today: The URISA Proceedings database - more practice with increasingly complicated SQL queries Advanced Queries: o Sub-queries: one way to nest or a cascade query is to stick a query in the 'where' clause: e.g., find parcels owned by XXX from that set of parcels that had a fire. This is a powerful way to take advantage of the fact that any SQL query returns a table - which can they be the starting point of another SQL query. o Self-joins: the 'where' clause can become quite complex with many joins and related 'and' and 'or' conditions. But handling 'and' conditions is a little tricky. How can you find papers the use both keyword Y and keyword Z if the table relating papers and keywords only shows one pair at a time? The zoning variance database o o Understanding the schema and rationale for the Boston zoning variance database (which we use later to map them as study spatial patterns as well as to illustrate concepts about distributed databases and community empowerment. Using the history of zoning database to understand how real databases evolve over time More URISA database Queries...from the URISA database * page Additional notes on SQL*Plus formatting * added to SQL Notes * Advanced Queries: Subqueries A subquery can be nested within a query

Example: Find the parcel with the highest estimated loss from a fire FROM FIRES WHERE ESTLOSS = (SELECT MAX(ESTLOSS) FROM FIRES); Alternatively, include the subquery as an inline "table" in the FROM clause: SELECT F.* FROM FIRES F, (SELECT MAX(ESTLOSS) MAXLOSS FROM FIRES) M WHERE F.ESTLOSS = M.MAXLOSS; Example: Find the parcels that have not had a fire FROM PARCELS WHERE PARCELID NOT IN (SELECT PARCELID FROM FIRES); or, more efficiently, FROM PARCELS P WHERE NOT EXISTS (SELECT NULL FROM FIRES F WHERE P.PARCELID = F.PARCELID); Example: Find the parcels that have not obtained a permit: FROM PARCELS WHERE (PID, WPB) NOT IN (SELECT PID, WPB FROM PERMITS); or, more efficiently, FROM PARCELS P

WHERE NOT EXISTS (SELECT NULL FROM FIRES F WHERE P.PARCELID = F.PARCELID); Advanced Queries: Self-Join A table can be joined to itself Example: Find the paper numbers in the URISA database for papers that use both keyword code 601 AND 602. The following query does not work, because it is not possible for value for a single column in a single row to contain two values at the same time: SELECT PAPER FROM MATCH WHERE CODE = 601 AND CODE = 602; This type of query requires a self-join, which acts as if we had two copies of the MATCH table and are joining them to each other. SELECT M1.PAPER FROM MATCH M1, MATCH M2 If you have trouble imagining the self-join, pretend that we actually created two copies of MATCH, M1 and M2: CREATE TABLE M1 AS FROM MATCH; CREATE TABLE M2 AS FROM MATCH; Then, we could join M1 and M2: SELECT M1.PAPER FROM M1, M2 The self-join allows us to perform this sort of operation without actually having to copy the table. We can just act as if we had two copies.

Now, let's add the titles to the paper numbers: SELECT M1.PAPER, T.TITLE FROM MATCH M1, MATCH M2, TITLES T AND M1.PAPER = T.PAPER Example: Find the time that passed between a fire on a parcel and all fires occurring within 300 days later on the same parcel SELECT F1.PARCELID, F1.FDATE FIRE1, F2.FDATE FIRE2, F2.FDATE - F1.FDATE INTERVAL FROM FIRES F1, FIRES F2 WHERE F1.PARCELID = F2.PARCELID AND F2.FDATE > F1.FDATE AND F2.FDATE <= F1.FDATE + 300; Note that a number of days can be added to a date. The Zoning Variance Database Zoning Variances * SQL examples using zoning variances * 1980 Census data (by Boston NSA) * Schema of Decision, Use, NSA, Neighbrhd Lookup Tables * Sub-Neighborhood lookup table * Grouping zoning applicants via 'lookup' tables * Schema of ZONING table (and listing of related lookup tables) Annotated SQL queries of ZONING table Schema of 1980 Boston Census data (and related lookup tables) Schema of Lookup tables (second half of Census data web page) The NSA and NEIGHBRHD tables (bottom of Zoning Variance web page) Annotated SQL queries illustrating

Zoning Variance Database Evolution Chart * use of lookup tables to categorize ownership of properties seeking zoning variances. (These topics are the focus of next week's lecture and lab #3.) Stages of evolution of the ZONING variance database