Yet More SQL




CS185c

Chris Pollett

Apr 6, 2010

Outline

Introduction

SELECT

Parts of a SELECT

Quiz

Which of the following statements is true?

  1. The catalog table SYSIBM.SYSTABLES is changed when one creates a view.
  2. The KEYSEQ column of SYSIBM.SYSCOLUMNS is used to handle autoincrementing columns.
  3. You can set the bufferpool when creating a database but not when creating a tablespace in DB2.

Example SELECTs and their results

Three select queries and their results

These examples show that if you use * in the select_list it means all columns.

Conceptual Evaluation of SELECT

It should be noted this kind of brute force evaluation of SELECT statement typically won't be very efficient.

Cartesian Product (aka Cross Product) of two tables

A cartesian product of two tables

Example Predicate Evaluation

A query with predicate evaluation

Projections

A query with a projection

Aggregate Functions

Examples of Aggregate Function

GROUP BY

Conceptual Evaluation of Aggregates

Example

As an example, consider the query:

SELECT CARDNO, MIN( DUEDATE)
FROM CHECKOUTS 
GROUP BY CARDNO 
HAVING COUNT(*) > 1;
Steps in evaluating a group by clause

Joins

Inner Join

Outer Join

Results of performing an outer join

Outer Join Examples

Results of performing an left and full outer join

Subqueries

Example -- Non-correlated Subquery

Example -- Correlated Subquery

Set Operations

SQL also supports set operations of union, difference, and intersection:

Views

CREATE VIEW newEmp	
AS SELECT name, empno, eduYear 
FROM Employee WHERE hireYear > 2000;

can be queried with 

SELECT * FROM newEMP WHERE eduYear > 17

DCL commands

GRANT

GRANT and REVOKE