












Internet Technology Coverage and Pointers
Select
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,…] }
FROM TableName [alias] [, …]
[WHERE condition]
[GROUP BY columnList] [HAVING condition]
[ORDER BY columnList]
FROM Specifies table(s) to be used.
WHERE Filters rows.
GROUP BY Forms groups of rows with same
column value.
HAVING Filters groups subject to some
condition.
SELECT Specifies which columns are to
appear in output.
ORDER BY Specifies the order of the output.
Order of the clauses cannot be changed.
Only SELECT and FROM are mandatory.
Use of DISTINCT
List the property numbers of all properties that have been viewed.
Use DISTINCT to eliminate duplicates:
Calculated Fields
To name column, use AS clause:
Range Search Condition
Also a negated version NOT BETWEEN.
BETWEEN does not add much to SQL’s expressive power.
Useful, though, for a range of values.

SELECT Statement - Aggregates
ISO standard defines five aggregate functions:
COUNT returns number of values in specified column.
SUM returns sum of values in specified column.
AVG returns average of values in specified column.
MIN returns smallest value in specified column.
MAX returns largest value in specified column.
Each operates on a single column of a table and returns a single value.
COUNT, MIN, and MAX apply to numeric and non-numeric fields, but SUM and AVG may be used on numeric fields only.
Apart from COUNT(*), each function eliminates nulls first and operates only on remaining non-null values.
COUNT(*) counts all rows of a table, regardless of whether nulls or duplicate values occur.
Can use DISTINCT before column name to eliminate duplicates.
DISTINCT has no effect with MIN/MAX, but may have with SUM/AVG.
Aggregate functions can be used only in SELECT list and in HAVING clause.
If SELECT list includes an aggregate function and there is no GROUP BY clause, SELECT list cannot reference a column out with an aggregate function. For example, the following is illegal:
SELECT Statement - Grouping
All column names in SELECT list must appear in GROUP BY clause unless name is used only in an aggregate function.
If WHERE is used with GROUP BY, WHERE is applied first, then groups are formed from remaining rows satisfying predicate.
ISO considers two nulls to be equal for purposes of GROUP BY.
Restricted Groupings – HAVING clause
HAVING clause is designed for use with GROUP BY to restrict groups that appear in final result table.
Similar to WHERE, but WHERE filters individual rows whereas HAVING filters groups.
Column names in HAVING clause must also appear in the GROUP BY list or be contained within an aggregate function.
Subqueries
Some SQL statements can have a SELECT embedded within them.
A subselect can be used in WHERE and HAVING clauses of an outer SELECT, where it is called a subquery or nested query.
Subselects may also appear in INSERT, UPDATE, and DELETE statements.
Subquery Rules
ORDER BY clause may not be used in a subquery (although it may be used in outermost SELECT).
Subquery SELECT list must consist of a single column name or expression, except for subqueries that use EXISTS.
By default, column names refer to table name in FROM clause of subquery. Can refer to a table in FROM using an alias.
When subquery is an operand in a comparison, subquery must appear on right-hand side.
A subquery may not be used as an operand in an expression.
ANY and ALL
ANY and ALL may be used with subqueries that produce a single column of numbers.
With ALL, condition will only be true if it is satisfied by all values produced by subquery.
With ANY, condition will be true if it is satisfied by any values produced by subquery.
If subquery is empty, ALL returns true, ANY returns false.
SOME may be used in place of ANY.
Multi-Table Queries
Can use subqueries provided result columns come from same table.
If result columns come from more than one table must use a join.
To perform join, include more than one table in FROM clause.
Use comma as separator and typically include WHERE clause to specify join column(s).
Also possible to use an alias for a table named in FROM clause.
Alias is separated from table name with a space.
Alias can be used to qualify column names when there is ambiguity.
Also possible to use an alias for a table named in FROM clause.
Alias is separated from table name with a space.
Alias can be used to qualify column names when there is ambiguity.
Alternative JOIN Constructs
SQL provides alternative ways to specify joins:
FROM Client c JOIN Viewing v ON c.clientNo = v.clientNo
FROM Client JOIN Viewing USING clientNo
FROM Client NATURAL JOIN Viewing
In each case, FROM replaces original FROM and WHERE. However, first produces table with two identical clientNo columns.
Computing a Join
Procedure for generating results of a join are:
1. Form Cartesian product of the tables named in FROM clause.
2. If there is a WHERE clause, apply the search condition to each row of the product table, retaining those rows that satisfy the condition.
3. For each remaining row, determine value of each item in SELECT list to produce a single row in result table.
4. If DISTINCT has been specified, eliminate any duplicate rows from the result table.
5. If there is an ORDER BY clause, sort result table as required.
Outer Joins
If one row of a joined table is unmatched, row is omitted from result table.
Outer join operations retain rows that do not satisfy the join condition.
EXISTS and NOT EXISTS
EXISTS and NOT EXISTS are for use only with subqueries.
Produce a simple true/false result.
True if and only if there exists at least one row in result table returned by subquery.
False if subquery returns an empty result table.
NOT EXISTS is the opposite of EXISTS.
As (NOT) EXISTS check only for existence or non-existence of rows in subquery result table, subquery can contain any number of columns.
Common for subqueries following (NOT) EXISTS to be of form:
(SELECT * …)
Union, Intersect, and Difference (Except)
Can use normal set operations of Union, Intersection, and Difference to combine results of two or more queries into a single result table.
Union of two tables, A and B, is table containing all rows in either A or B or both.
Intersection is table containing all rows common to both A and B.
Difference is table containing all rows in A but not in B.
Two tables must be union compatible.
Format of set operator clause in each case is:
op [ALL] [CORRESPONDING [BY {column1 [, …]}]]
If CORRESPONDING BY specified, set operation performed on the named column(s).
If CORRESPONDING specified but not BY clause, operation performed on common columns.
If ALL specified, result can include duplicate rows.
INSERT
INSERT INTO TableName [ (columnList) ]
VALUES (dataValueList)
columnList is optional; if omitted, SQL assumes a list of all columns in their original CREATE TABLE order.
Any columns omitted must have been declared as NULL when table was created, unless DEFAULT was specified when creating column.
dataValueList must match columnList as follows:
number of items in each list must be same;
must be direct correspondence in position of items in two lists;
data type of each item in dataValueList must be compatible with data type of corresponding column.
UPDATE
UPDATE TableName
SET columnName1 = dataValue1
[, columnName2 = dataValue2…]
[WHERE searchCondition]
TableName can be name of a base table or an updatable view.
SET clause specifies names of one or more columns that are to be updated.
WHERE clause is optional:
if omitted, named columns are updated for all rows in table;
if specified, only those rows that satisfy searchCondition are updated.
New dataValue(s) must be compatible with data type for corresponding column.
DELETE
searchCondition]
TableNDELETE FROM TableName
[WHERE ame can be name of a base table or an updatable view.
searchCondition is optional; if omitted, all rows are deleted from table. This does not delete table. If search_condition is specified, only those rows that satisfy condition are deleted.