Index

Symbols

* (asterisk), 195

|| (concatenation operator), 138

> (greater than), 186188

>= (greater than or equal to), 186

< (less than), 186188

<= (less than or equal to), 186

() (parentheses), 144, 210211, 216

CASE (conditional expressions), 684

% (percent sign), 195

? (question mark), 195

_ (underscore), 195

Numbers

2016 SQL Standard, 4

A

ABS, 142

Access, CASE (conditional expressions), 684

Actian, 74

adding sorting specifications to UNION, 382

aggregate expressions, 445

aggregate functions, 444446, 798799

AVG, calculating mean values, 451452

Bowling League Database, 844846

COUNT, 406408, 446

COUNT (value expression), 448449

COUNT(*), 446

counting all the rows, 446448

in filters, 457459

grouping data, 477478

MAX, 406408

finding largest values, 452454

MIN, finding smallest values, 454455

Null values, 445

OVER(), 803805

Sales Orders Database, 847850

sample statements, 459466

Bowling League Database, 463465

Entertainment Agency Database, 461462

Recipes Database, 465466

Sales Orders Database, 460461

School Scheduling Database, 462463

School Scheduling Database, 851852

subqueries, 457458

SUM, computing totals, 450451

syntax diagrams, 444

using more than one function, 455457

windows functions, 827834

alias names, assigning to tables, INNER JOIN, 282284

ALL, 371, 375, 417420

all rows, deleting with DELETE statement, 605607

alphabetical order, sorting by, 108

American National Standards Institute (ANSI), 2, 75

evolution of SQL/86, 7679

analytical databases, 16

AND, 205206

finding multiple matches in the same table

EXISTS, 646648

GROUP BY, 648652

HAVING, 648652

IN, 644646

INNER JOIN, 642644

sample statements, 652657

Bowling League Database, 663668

Entertainment Agency Database, 657659

Recipes Database, 668671

Sales Orders Database, 653657

School Scheduling Database, 659663

sets with multiple AND criteria, 628629

using with OR, 209211

ANSI (American National Standards Institute), 2, 75

evolution of SQL/86, 7679

ANSI NCITS-H2, 80

ANSI/ISO standards, 7679

ANY, 417420

APPROXIMATE NUMERIC, 129

arithmetic operations

IBM DB2, 890908

Microsoft Access, 893

Microsoft SQL Server, 895

MySQL, 898

Oracle, 902

PostgreSQL, 904

artificial primary keys, 60

assigning correlation names to tables, INNER JOIN, 282284

asterisks (*), 195

shortcuts, 102103

ASYMMETRIC, 189190

attributes. See columns

AVG, calculating mean values, 451452

avoiding HAVING COUNT trap, 513518

B

base tables, views (relational databases), 23

BETWEEN comparisons, 189190

BETWEEN predicate, 178, 189192

checking for overlapping ranges, 219220

BETWEEN…AND, 189

BINARY, 128

blank spaces, 160

BOOLEAN, 129

Bowling League Database

aggregate functions, 463465, 844846

AND, 663668

CASE (conditional expressions), 702705

CUBE, 783784

DELETE statement, 618620

driver tables, 742743

expressions, 169171

GROUP BY, 496499

GROUPING SETS, 786787

HAVING clause, 525526

INNER JOIN

looking for matching values, 311314

more than two tables, 302305

two tables, 298299

INSERT statement, 596597

NOT, 663668

NTILE(), 842843

OUTER JOIN, 359361

search conditions, 231232

SELECT statement, 119120

subqueries

in expressions, 428429

in filters, 434435

UNION, 392394

unlinked tables, 734735

UPDATE statement, 565568

C

calculated columns, 40

calculating

mean values with AVG, 451452

row number with ROW_NUMBER, 814818

totals on combinations using CUBE, 765770

Call-Level Interface (CLI), 80

Cartesian product, 277278, 325, 711712

cascade deletion rule, 63

CASE (conditional expressions), 678

parentheses, 684

reasons for using, 678

sample statements, 692693

Bowling League Database, 702705

Entertainment Agency Database, 696698

Sales Orders Database, 693696

School Scheduling Database, 698702

searched CASE, solving problems, 688691

simple CASE, solving problems, 683687

syntax, 678682

WHERE clause, 691

CASE expression, syntax diagrams, 679

case sensitivity, 183

string comparison, 197

CAST function

changing data types, 130132

concatenation expressions, 141

CEIL, 142

Chamberlin, Dr. Donald, 72

changing, data types, CAST function, 130132

CHARACTER, 127

character string literals, 133134

checking, relationships, INNER JOIN, 291292

classic set operations versus SQL, 259262

EXCEPT, 265268

INTERSECT, 262265

UNION, 268271

clauses

FROM clause, 279280, 343

embedding SELECT statements, 286

OUTER JOIN, 324325, 327

CORRESPONDING clause, 373

HAVING. See HAVING clause

ORDER BY clause. See ORDER BY clause

SELECT statement, 91, 9293

USING clause, 280281

VALUES clause, INSERT statement, 575578

WHERE clause. See WHERE clause

CLI (Call-Level Interface), 80

cloud servers, 83

Codd, Dr. Edgar F.16–17, 72

collating sequences, 107

column expressions, subqueries, 402405, 422423

column references, 275276

syntax diagrams, 276

column restrictions, GROUP BY, 486488

columns

calculated columns, 40

counting values in, 448449

fine-tuning, 35

naming conventions, 3538

structure of columns, 3840

mixing with expressions, 481483

multipart columns, 39

resolving, 4043

multiple columns, updating, 541543

multivalued columns, 39

resolving, 4345

relational databases, 21

requesting all columns with SELECT statement, 101103

resolving duplicate columns, 5056

retrieving multiple columns with SELECT statement, 100101

combining

sets, UNION, 257259, 268271

UNION. See UNION

commercial implementations of SQL, 83

COMMIT, 540

common values, finding with INTERSECT, 262265

comparing string values, 181184

comparison predicate, 178, 181

aggregate functions as filters, 457458

comparing string values, 181184

equality and inequality, 185186

less than and greater than, 186188

composite primary keys, 57

Computer Associates International, Inc., 74

computing totals with SUM, 450451

concatenation expressions, 138141

CAST function, 141

syntax diagrams, 138

conditional expressions. See CASE (conditional expressions)

conditions

expressing, 225226

multiple conditions, 204205

converting values into data types, CAST function, 130132

correlation names, assigning to tables (INNER JOIN), 282284

CORRESPONDING clause, 373

COUNT, 406408, 445446

counting all the rows, 446448

counting values in columns or expressions, 448449

HAVING clause, 513518

COUNT (value expression), 446, 448449

COUNT(*), 827828

counting all the rows, 446448

counting

all the rows, COUNT(*), 446448

values in columns or expressions, 448449

CROSS JOIN, 713

deciding when to use, 713714

sample statements, 725726

unlinked tables, 711712, 715716

CUBE, 778779

Bowling League Database, 783784

calculating totals on combinations, 765770

Sales Orders Database, 784786

D

data

fetching from two tables

with JOIN, 370

with UNION, 371

grouping, 472474

with aggregate functions, 477478

with GROUP BY. See GROUP BY

mixing columns and expressions, 481483

versus information, 9395

inserting

with INSERT statements. See INSERT statement

with SELECT expressions, 581587

ranking, with RANK(), 818824

splitting into quintiles, 824827

unlinked data. See unlinked data

data types

APPROXIMATE NUMERIC, 129

BINARY, 128

BOOLEAN, 129

changing with CAST function, 130132

CHARACTER, 127

DATETIME, 129

EXACT NUMERIC, 128

extended data types, 130

for IBM DB2, 857, 889

INTERVAL, 129, 147148

JOIN eligible data types, 275

for Microsoft Access, 893

for Microsoft SQL Server, 895

MySQL, 897

NATIONAL CHARACTER, 128

Oracle, 901

PostgreSQL, 904

data warehouses, 19

database design, 3031, 3435

database structures, 3435

database theory, 3031

databases

analytical databases, 16

operational databases, 16

relational database model, 1617

relational databases. See relational databases

sample databases, 911

types of, 1516

DATE, 136, 138

Date, C. J., 76

date and time arithmetic expressions, 138, 146

date expressions, 147148

time expressions, 149150

date expressions, 147148, 156157

SELECT statement, time expressions, 156

syntax diagrams, 147

DATETIME, 129

datetime literals, 135138

DB2, 74

DEFAULT keyword, 576

default pad character, 184

degree of participation, relationships, 6668

DELETE statement, 604605

deleting

all rows, 605607

some rows, 607611

sample statements, 612613

Bowling League Database, 618620

Entertainment Agency Database, 614617

Sales Orders Database, 613614

School Scheduling Database, 617618

syntax diagrams, 604

uses for, 611612

VALUES clause, 603604

WHERE clause, 603604

deleting. See also resolving

all rows with DELETE statement, 605607

duplicate rows with SELECT statement, 5056

some rows with DELETE statement, 607611

deletion rule, relationships, 6364

delimited identifiers, 37, 48

DENSE_RANK(), 820, 823824

derived tables, 284

descending order, sorting by, 109

design, database design, 3031, 3435

diagrams. See also Appendix A; syntax diagrams

predicates, 681682

Search Condition, 680

SELECT queries, 106

SELECT statement, 92

utilizing, 59

difference, 243, 250

problems you can solve with, 256257

result sets, 252256

set theory, 250252

DISTINCT, 103105, 415, 576

GROUP BY, 485

MAX, 454

MIN, 455

UNION, 389

driver tables, 717

Bowling League Database, 742743

Entertainment Agency Database, 737739

Sales Orders Database, 736737

School Scheduling Database, 739742

setting up, 717720

using, 720725

dynamic data, 16

E

eliminating. See deleting

embedding

INNER JOIN in SELECT statements, 284286

JOINs within JOINs, 286291

OUTER JOIN, 333344

SELECT statements in OUTER JOINs, 330333

Entertainment Agency Database

aggregate functions, 461462

AND, 657659

CASE (conditional expressions), 696698

DELETE statement, 614617

driver tables, 737739

expressions, 165167

GROUP BY, 493494

GROUPING SETS, 787788

HAVING clause, 521522

INNER JOIN

looking for matching values, 309311

more than two tables, 301302

two tables, 296297

INSERT statement, 593595

NOT, 657659

NTILE(), 843844

OUTER JOIN, 354355

ROW_NUMBER(), 835837

search conditions, 228230

SELECT statement, 116117

subqueries

in expressions, 426427

in filters, 432433

UNION, 389390

unlinked tables, 728731

UPDATE statement, 558562

Entry SQL, 78

equality, comparison predicate, 185186

ESCAPE option, LIKE predicate, 198199

Euler, Leonard, 246247

Euler diagram, 246247

events, relational databases, 20

evolution of SQL/86, 7679

EXACT NUMERIC, 128

EXCEPT, 265268

excluding rows with NOT, 201204, 211214

executing queries, 112

EXISTS, 420422

AND, 654655, 657658

finding multiple matches in the same table, 646648

EXP, 142

explicit values, specifying, 132133

expressing conditions, 225226

expressions, 126

aggregate expressions, 445

counting values in, 448449

grouping with GROUP BY, 488490

mixing with columns, 481483

sample statements, 163

Bowling League Database, 169171

Entertainment Agency Database, 165167

Sales Orders Database, 164165

School Scheduling Database, 167169

SELECT clause, 150151

concatenation expressions, 151152

naming, 152154

SELECT expression, 372

SELECT statement

date expressions, 156157

mathematical expressions, 154156

subqueries, sample statements, 425430

types of

data, 127130

concatenation expressions, 138141

date and time arithmetic expressions, 138, 146150

mathematical expressions, 138, 142146

UPDATE expressions, subqueries, 548551

value expressions, 157159

syntax diagrams, 549

extended data types, 130

eXtensible Markup Language (XML), 19

extensions, SQL/92, 79

F

Federal Information Processing Standard (FIPS), 80

fetching data

from two tables with JOIN, 370

from two tables with UNION, 371

fields. See columns

filter rows, subqueries, 543546

filtering

deciding between WHERE and HAVING, 510513

grouped data, 506510

filters

aggregate functions, 457459

subqueries, 408411

subqueries as, 423424

sample statements, 430437

finding

common values, INTERSECT, 262265

largest values with MAX, 452454

matching values with INNER JOINs, 293294

sample statements, 306316

missing values

EXCEPT, 265268

OUTER JOIN, 349

multiple matches in the same table

EXISTS, 646648

GROUP BY, 648652

HAVING, 648652

IN, 644646

INNER JOIN, 642644

partially matched information, OUTER JOIN, 349350

related rows, INNER JOIN, 293

smallest values with MIN, 454455

fine-tuning

columns, 35

naming conventions, 3538

resolving multipart columns, 4043

resolving multivalued columns, 4345

structure of columns, 3840

tables, 46

naming conventions, 4648

relationships, 6063

relationships, deletion rule, 6364

relationships, participation, 6468

resolving duplicate columns, 5056

structure of tables, 4850

FIPS (Federal Information Processing Standard), 80

FLOOR, 142

foreign keys, relational databases, 2223

FROM clause, 279280, 343

embedding, SELECT statements, 286

INNER JOIN, 277

OUTER JOIN, 324325, 327

SELECT statement, 92, 331

FULL OUTER JOIN, 344347

non-key values, 347348

syntax, 344347

syntax diagrams, 344

Full SQL, 79

functions

aggregate functions. See aggregate functions

CAST function, 130132

IBM DB2, 890892

Microsoft Access, 893894

Microsoft SQL Server, 896897

MySQL, 898901

Oracle, 902903

future of SQL (Structured Query Language), 8384

G

generating primary key values with INSERT statements, 578580

greater than (>), 186188

greater than or equal to (>=), 186

GROUP BY, 475, 692, 725, 757, 761

column restrictions, 486488

finding multiple matches in the same table, 648652

grouping expressions, 488490

mixing columns and expressions, 481483

versus OVER(), 800

sample statements, 491501

Bowling League Database, 496499

Entertainment Agency Database, 493494

Recipes Database, 499500

Sales Orders Database, 491492

School Scheduling Database, 495496

SELECT statement, 93

sets with multiple NOT criteria, 638641

simulating SELECT DISTINCT statements, 484485

in subqueries in WHERE clauses, 483484

syntax, 475481, 753

syntax diagrams, 475

uses for, 490491

grouped data, filtering, 506510

grouping

calculating totals on combinations with CUBE, 765770

creating a union of totals with GROUPING SETS, 771775

CUBE, 778779

data, 472474

aggregate functions, 477478

GROUP BY. See GROUP BY

mixing columns and expressions, 481483

expressions, with GROUP BY, 488490

GROUPING SETS, 775776

ROLLUP, 777778

sample statements, 780781

CUBE, 783786

GROUPING SETS, 786788

ROLLUP, 781783

in sub-groups, 750753

totals in hierarchies, ROLLUP, 754765

variations on techniques, 775780

GROUPING function, 758

GROUPING SETS, 775776

Bowling League Database, 786787

creating union of totals, 771775

Entertainment Agency Database, 787788

H

HAVING

filtering, 510513

grouped data, 508510

finding multiple matches in the same table, 648652

sample statements, 519527

Bowling League Database, 525526

Entertainment Agency Database, 521522

Recipes Database, 526527

Sales Orders Database, 520521

School Scheduling Database, 522525

SELECT statement, 93

sets with multiple NOT criteria, 638641

uses for, 518519

HAVING COUNT trap, avoiding, 513518

Hernandez, Mike, 17

hierarchies, totals, ROLLUP, 754765

history of SQL (Structured Query Language)

commercial implementations, 83

early vendor implementations, 7374

origins of, 7273

standards, 7576

I

IBM, 18, 19, 74

early vendor implementations, 7374

System R, 72, 73

IBM DB2

arithmetic operations, 890908

data types, 857, 889

functions, 890892

Identity data type, 579

identifiers

delimited identifiers, 37, 48

regular identifiers, 37, 48

Identity data type, 579

IN, finding multiple matches in the same table, 644646

IN predicate, 178

membership condition, 192194

for subqueries, 411417

INCITS DM32.2, 80

inequality, comparison predicate, 185186

information

versus data, 9395

sorting with SELECT statement, 105107

Ingres, 74

INGRES (Interactive Graphics Retrieval System), 18

Inmon, William H., 19

INNER JOIN, 274, 713

assigning correlation names to tables, 282284

FROM clause, 277

embedding

JOINs within JOINs, 286291

SELECT statements, 284286

finding

matching values, 293294

multiple matches in the same table, 642644

related rows, 293

relationships, checking, 291292

sample statements, 295

looking for matching values, 306316

more than two tables, 300306

two tables, 295300

syntax, 276

syntax diagrams, 277

tables, 277282

INSERT INTO, 576

INSERT statement, 573575

generating primary key values, 578580

inserting values, 575578

sample statements, 588589

Bowling League Database, 596597

Entertainment Agency Database, 593595

Sales Orders Database, 589592

School Scheduling Database, 595596

SELECT expression, 581

syntax diagrams with VALUES clause, 575

uses for, 587588

inserting

data

with INSERT statement. See INSERT statement

with SELECT expressions, 581587

values, with INNER JOINs, 575578

Interactive Graphics Retrieval System (INGRES), 18

Intermediate SQL, 78

International Organization for Standardization. See ISO (International Organization for Standardization)

INTERSECT, 262265

intersection, 243, 244

problems you can solve with, 249250

result sets, 246249

set theory, 244245

INTERVAL, 129, 147148

IS NULL, 634635

IS NULL predicate, 178

ISO (International Organization for Standardization), 2, 76

evolution of SQL/86, 7679

J

JOIN, 273274

determining what is legal to join, 275

embedding within JOINs, 286291

OUTER JOIN, 333344

fetching data from two tables, 370

INNER JOIN. See INNER JOIN

NATURAL JOIN, 329330

OUTER JOIN. See OUTER JOIN

UNION JOIN, 348

UPDATE clause, 546548

JOIN eligible data types, 275

K

keys

primary keys, tables, 5660

relational databases, 2223

keywords

DATE, 138

DEFAULT keyword, 576

DISTINCT, 103105, 576

GROUP BY, 93

HAVING, 93

INSERT INTO, 576

special predicate keywords

ALL, 417420

ANY, 417420

EXISTS, 420422

IN, 411417

SOME, 417420

TIME, 138

TOP, 111

VALUES, 576

WHERE, 93

L

largest values, finding with MAX, 452454

LEFT OUTER JOIN, 323324, 332

less than (<), 186188

less than or equal to (<=), 186

LIKE predicate, 178

ESCAPE option, 198199

pattern match condition, 194199

linking

multiple tables, embedding JOINs within JOINs, 286291

SELECT statements with INTERSECT, 265

linking tables, 4445

literal values, 132133

character string literals, 133134

datetime literals, 135138

numeric literals, 135

LN, 142

M

mandatory participation, 64

many-to-many relationships, 6162

relational databases, 2729

matches, finding multiple matches in the same table

EXISTS, 646648

GROUP BY, 648652

HAVING, 648652

IN, 644646

INNER JOIN, 642644

matching values, finding with INNER JOINs, 293294

sample statements, 306316

mathematical expressions, 138, 142146

Nulls, 163

parentheses, 144

SELECT statement, 154156

syntax diagrams, 143

MAX, 406408

finding largest values, 452454

mean values, calculating with AVG, 451452

membership condition, IN predicate, 192194

mere mortals, 12

Microsoft Access, 19, 195

arithmetic operations, 893

data types, 893

functions, 893894

Microsoft Office Access, CASE (conditional expressions), 684

Microsoft SQL Server

arithmetic operations, 895

data types, 895

functions, 896897

Identity data type, 579

ORDER BY clause, 653, 757

MIN, finding smallest values, 454455

missing values, 161

finding with OUTER JOIN, 349

MOD, 142

multicolumn sorts, 109110

multipart columns, 39

resolving, 4043

multiple columns, updating, 541543

multiple conditions, 204205

multiple tables, linking (embedding JOINs within JOINs), 286291

multivalued columns, 39

resolving, 4345

MySQL

arithmetic operations, 898

data types, 897

functions, 898901

ROLLUP, 765

N

names, qualifying in ON clause, 279280

naming conventions

for columns, 3538

for tables, 4648

naming expressions (SELECT clause), 152154

NATIONAL CHARACTER, 128

National Committee for Information Technology Standards (NCITS), 80

National Institute of Standards and Technology (NIST), 80

NATURAL JOIN, 281, 329330

NCITS (National Committee for Information Technology Standards), 80

nested parenthetical operations, 144

NEXTVAL property, Oracle, 579

NIST (National Institute of Standards and Technology), 80

non-key values, FULL OUTER JOIN, 347348

NOT

excluding rows, 201204, 211214

sample statements, 652657

Bowling League Database, 663668

Entertainment Agency Database, 657659

Recipes Database, 668671

Sales Orders Database, 653657

School Scheduling Database, 659663

sets with multiple NOT criteria, 629630

GROUP BY, 638641

HAVING, 638641

NOT EXISTS, 637638

NOT IN, 635637

OUTER JOIN, 632635

syntax diagrams, 202

NOT EXISTS, sets with multiple NOT criteria, 637638

NOT IN, sets with multiple NOT criteria, 635637

NTILE(), 824827

Bowling League Database, 842843

Entertainment Agency Database, 843844

Null, 159162, 199201, 221225

aggregate functions, 445

problems with, 162163

Null condition, syntax diagrams, 199

numeric literals, 135

O

objects, relational databases, 20

ODBC (Open Database Connectivity), 80

ON clause

qualifying names, 279280

search conditions, 278

one-to-many relationships, 61

relational databases, 26

one-to-one relationships, 6061

relational databases, 2526

Open Database Connectivity (ODBC), 80

operational databases, 16

operations, set operations, 243244

operators

AND, 205206

using with OR, 209211

NOT operator, excluding rows, 211214

OR, 206209

determining result sets, 224

using with AND, 209211

optional participation, 64

OR, 205, 206209

determining result sets, 224

using with AND, 209211

Oracle

arithmetic operations, 902

data types, 901

functions, 902903

NEXTVAL property, 579

Oracle Corporation, 74

ORDER BY, 106107, 474, 652653, 692, 725, 757

predicates, 805810

sorting SELECT queries, 108111

UNION, 382

order of precedence, 143

search conditions, 214215

less is more, 217218

prioritizing, 215217

origins of SQL (Structured Query Language), 7273

orphaned rows, 63

OUTER JOIN, 321323, 349, 713

embedding

JOINs within JOINs, 333344

SELECT statements, 330333

finding

missing values, 349

partially matched information, 349350

FULL OUTER JOIN. See FULL OUTER JOIN

grouping data types, 478

LEFT OUTER JOIN, 323324

RIGHT OUTER JOIN, 323324

sample statements, 350365

Bowling League Database, 359361

Entertainment Agency Database, 354355

Recipes Database, 362364

Sales Orders Database, 351353

School Scheduling Database, 356359

sets with multiple NOT criteria, 632635

syntax, 324330

tables, 324330

OVER(), 799, 811

aggregate functions, 803805

versus GROUP BY, 800

predicates

ORDER BY, 805810

PARTITION BY, 800801

ROWS (or RANGE), 811814

overlapping ranges, checking for, 219221

P

parentheses, 210211, 216

CASE expression, 684

mathematical expressions, 144

partially matched information, finding with OUTER JOIN, 349350

participation, relationships, 6468

PARTITION BY, 800801

pattern match condition, 194199

syntax diagrams, 195

pattern strings, samples, 195196

percent sign (%), 195

PERCENT_RANK(), 820821, 824

PostgreSQL, 904905

arithmetic operations, 904

data types, 904

functions, 904905

POWER, 142

predicates, 93, 177

BETWEEN … AND, 189

BETWEEN, 178

range condition, 189192

combining with AND, 205206

comparison, 178

comparison predicate, aggregate functions as filters, 457458

diagrams, 681682

IN, 178

membership condition, 192194

IS NULL, 178

unknown values, 199201

LIKE, 178

ESCAPE option, 198199

pattern match condition, 194199

OR, 206209

ORDER BY, 805810

PARTITION BY, 800801

ROWS (or RANGE), 811814

primary key values, generating with INSERT statements, 578580

primary keys, 20

relational databases, 2223

tables, 5660

primary tables, one-to-one relationships, 25

prioritizing search conditions, order of precedence, 215217

problems, solving

with searched CASE, 688691

with simple CASE, 683687

with unlinked data, 714716

Q

qualifying names, ON clause, 279280

quantified predicates, (SOME, ANY, ALL), 417420

QUEL (Query Language), 74

queries

executing, 112

RDBMS (relational database management system), 24

SELECT queries. See SELECT query

query expression, 106107

Query Language (QUEL), 74

query optimizers, 218

query specification, 106107

question mark (?), 195

quintiles, splitting, data, 824827

quotes, single quote, 133

R

RANGE, 811814, 827828

range condition, 189192

ranges, overlapping ranges (checking for), 219221

RANK(), 818824

ranking data, with RANK(), 818824

RDBMS (relational database management system), 1719, 74

REAL, 146

reasons for learning SQL (Structured Query Language), 84

Recipes Database

aggregate functions, 465466

AND, 668671

GROUP BY, 499501

HAVING clause, 526527

INNER JOIN

looking for matching values, 314315

more than two tables, 305306

two tables, 299300

NOT, 668671

OUTER JOIN, 362364

ROW_NUMBER(), 837838

search conditions, 232233

SELECT statement, 120121

subqueries

in expressions, 429430

in filters, 435437

UNION, 394395

records. See rows

referential integrity, 6263

refining searches, 176

regular identifiers, 37, 48

related rows, finding with INNER JOINs, 293

relational database management system (RDBMS), 1719, 74

relational database model, 1617

relational databases

columns, 21

keys, 2223

reasons for learning, 2930

relations, 19

relationships, 25

many-to-many, 2729

one-to-many, 26

one-to-one, 2526

rows, 2122

tables, 2021

views, 2324

Relational Software, Inc., 74

Relational Technology, Inc., 74

relations. See tables

relationships

checking with INNER JOIN, 291292

deletion rule, 6364

many-to-many relationships, 6162

one-to-many relationships, 61

one-to-one relationships, 6061

participation, 6468

relational databases, 25

many-to-many, 2729

one-to-many, 26

one-to-one, 2526

tables, 6063

requests

translating into SQL, SELECT statement, 9599

writing with UNION, 372

combining complex SELECT statements, 375379

using simple SELECT statements, 372375

resolving. See also deleting

duplicate columns, 5056

multipart columns, 4043

multivalued columns, 4345

restrict deletion rule, 63

restrictions, column restrictions (GROUP BY), 486488

result sets

determining with OR, 224

difference, 252256

intersection, 246249

union, combining, 259261

retrieving multiple columns, with SELECT statement, 100101

RIGHT OUTER JOIN, 323324

ROLLBACK, 540

ROLLUP, 777778

MySQL, 765

Sales Orders Database, 781782

sample statements, 781782

School Scheduling Database, 782783

totals in hierarchies, 754765

row numbers, calculating with ROW_NUMBER, 814818

row subqueries, 400402

row value constructors, 401

ROW_NUMBER(), 814818, 827828, 829

Entertainment Agency Database, 835837

Recipes Database, 837838

rows

counting with COUNT(*), 446448

deleting all rows with DELETE statement, 605607

deleting some rows with DELETE statement, 607611

eliminating duplicates with SELECT statement, 103105

excluding, with NOT, 201204, 211214

filter rows, subqueries, 543546

related rows, finding with INNER JOIN, 293

relational databases, 2122

selected rows, updating, 631633

ROWS (or RANGE), 811814, 827828

S

SAA (Systems Application Architecture), 80

Sales Orders Database

aggregate functions, 460461, 847850

AND, 653657

CASE (conditional expressions), 693696

CUBE, 784786

DELETE statement, 613614

driver tables, 736737

expressions, 164165

GROUP BY, 491492

HAVING clause, 520521

INNER JOIN

looking for matching values, 306309

more than two tables, 300301

two tables, 295296

INSERT statement, 589592

NOT, 653657

OUTER JOIN, 351353

ROLLUP, 781782

search conditions, 227228

SELECT statement, 114115

subqueries

in expressions, 425426

in filters, 430431

UNION, 385389

unlinked tables, 726728

UPDATE statement, 554557

windows functions, 838839

sample databases, 911

sample statements

aggregate functions, 459466

Bowling League Database, 463465

Entertainment Agency Database, 461462

Recipes Database, 465466

Sales Orders Database, 460461

School Scheduling Database, 462463

AND, 652657

Bowling League Database, 663668

Entertainment Agency Database, 657659

Recipes Database, 668671

Sales Orders Database, 653657

School Scheduling Database, 659663

CASE (conditional expressions), 692693

Bowling League Database, 702705

Entertainment Agency Database, 696698

Sales Orders Database, 693696

School Scheduling Database, 698702

CROSS JOIN, 725726

DELETE statement, 612613

Bowling League Database, 618620

Entertainment Agency Database, 614617

Sales Orders Database, 613614

School Scheduling Database, 617618

expressions, 163171

Bowling League Database, 169171

Entertainment Agency Database, 165167

Sales Orders Database, 164165

School Scheduling Database, 167169

GROUP BY, 491501

Bowling League Database, 496499

Entertainment Agency Database, 493494

Recipes Database, 499500

Sales Orders Database, 491492

School Scheduling Database, 495496

grouping, 780781

CUBE, 783786

GROUPING SETS, 786788

ROLLUP, 781783

HAVING clause, 519527

Bowling League Database, 525526

Entertainment Agency Database, 521522

Recipes Database, 526527

Sales Orders Database, 520521

School Scheduling Database, 522525

INNER JOIN, 295

looking for matching values, 306316

more than two tables, 300306

two tables, 295300

INSERT statement, 588589

Bowling League Database, 596597

Entertainment Agency Database, 593595

Sales Orders Database, 589592

School Scheduling Database, 595596

NOT, 652657

Bowling League Database, 663668

Entertainment Agency Database, 657659

Recipes Database, 668671

Sales Orders Database, 653657

School Scheduling Database, 659663

OUTER JOIN, 350365

Bowling League Database, 359361

Entertainment Agency Database, 354355

Recipes Database, 362364

Sales Orders Database, 351353

School Scheduling Database, 356359

search conditions, 226227

Bowling League Database, 231232

Entertainment Agency Database, 228229

Recipes Database, 232233

Sales Orders Database, 227228

School Scheduling Database, 230231

SELECT queries, 113121

SELECT statement, 113121

Bowling League Database, 119120

Entertainment Agency Database, 116117

Recipes Database, 120121

Sales Orders Database, 114115

School Scheduling Database, 117118

subqueries, 424425

Bowling League Database, 428429, 434435

Entertainment Agency Database, 426427, 432433

in expressions, 425430

in filters, 430437

Recipes Database, 429430, 435437

Sales Orders Database, 425426, 430431

School Scheduling Database, 427428, 433434

UNION, 385395

Bowling League Database, 392394

Entertainment Agency Database, 389390

Recipes Database, 394395

Sales Orders Database, 385389

School Scheduling Database, 390392

unlinked data

driver tables, 736743

unlinked tables, 726736

UPDATE statement, 552553

Bowling League Database, 565568

Entertainment Agency Database, 558562

Sales Orders Database, 554557

School Scheduling Database, 562565

windows functions, 834835

aggregate functions, 844852

NTILE(), 842844

RANK(), DENSE_RANK, and PERCENT_RANK, 838841

ROW_NUMBER(), 835838

samples, defined pattern strings, 195196

saved queries, RDBMS (relational database management system), 24

saving SELECT statement, 111112

scalar subqueries, 400, 402

schema for sample databases. See Appendix B

School Scheduling Database

aggregate functions, 462463, 851852

AND, 659663

CASE (conditional expressions), 698702

DELETE statement, 617618

driver tables, 739742

expressions, 167169

GROUP BY, 495496

HAVING clause, 522525

INNER JOIN

looking for matching values, 311

two tables, 297298

INSERT statement, 595596

NOT, 659663

OUTER JOIN, 356359

ROLLUP, 782783

search conditions, 230231

SELECT statement, 117118

subqueries

in expressions, 427428

in filters, 433434

UNION, 390392

unlinked tables, 731734

UPDATE statement, 562565

windows functions, 840841

search conditions, 177

AND, 205206

OR, 206209

order of precedence, 214215

less is more, 217218

prioritizing, 215217

sample statements, 226233

Bowling League Database, 231232

Entertainment Agency Database, 228229

Recipes Database, 232233

Sales Orders Database, 227228

School Scheduling Database, 230231

syntax diagrams, 205, 680

searched CASE, solving problems, 688691

searching, for unknown values, 199201

secondary tables, one-to-one relationships, 25

SELECT clause, expressions, 150151

concatenation expressions, 151152

naming, 152154

SELECT DISTINCT statements, simulating, 484485

SELECT expression, 372

inserting data, 581587

SELECT queries, 106

diagrams, 106

sample statements, 113121

sorting information, 108111

SELECT statement, 90, 9193

FROM clause, 331

clauses, 91, 9293

complex SELECT statements, combining, 375379

data versus information, 9395

date expressions, time expressions, 156

diagrams, 92

embedding

INNER JOIN, 284286

OUTER JOINs, 330333

expressions

date expressions, 156157

mathematical expressions, 154156

GROUP BY, syntax diagrams, 475

requesting all columns, 101103

retrieving multiple columns, 100101

rows, eliminating duplicates, 103105

sample statements, 113121

Bowling League Database, 119120

Entertainment Agency Database, 116117

Recipes Database, 120121

Sales Orders Database, 114115

School Scheduling Database, 117118

saving, 111112

simple SELECT statements

syntax diagrams, 403

writing requests with UNION, 372375

sorting information, 105107

translating requests into SQL, 9599

WHERE clause, 176178

using, 179181

selected rows, updating, 631633

SEQUEL (Structured English Query Language), 72

SEQUEL-XRM, 72

set diagrams, 246247

set membership, IN predicate, 411417

set operations, 243244

classic set operations versus SQL

EXCEPT, 265268

INTERSECT, 262265

UNION, 268271

classic versus SQL, 259262

difference, 243, 250

problems you can solve with, 256257

result sets, 252256

set theory, 250252

intersection, 243, 244

problems you can solve with, 249250

result sets, 246249

set theory, 244245

union, 243

combining result sets, 259261

problems you can solve with, 261262

set theory, 257259

set theory

difference, 250252

intersection, 244245

union, 257259

sets, 242243, 628

AND, multiple AND criteria, 628629

combining

with UNION, 268271

with union, 257259

finding multiple matches in the same table

EXISTS, 646648

GROUP BY, 648652

HAVING, 648652

IN, 644646

INNER JOIN, 642644

including some criteria but excluding others, 630631

NOT

GROUP BY, 638641

HAVING, 638641

multiple NOT criteria, 629630

NOT EXISTS, 637638

NOT IN, 635637

OUTER JOIN, 632635

shortcuts, requesting all columns with SELECT statement, 101103

simple CASE, solving problems, 683687

simple primary keys, 57

simple SELECT statements, syntax diagrams, 403

simple UPDATE expression, 537538

simple WHERE clause, deleting some rows, 607

simulating SELECT DISTINCT statements, GROUP BY, 484485

single quote, 133

smallest values, finding, with MIN, 454455

solving problems

with searched CASE, 688691

with simple CASE, 683687

with unlinked data, 714716

SOME, 417420

some rows, deleting with DELETE statement, 607611

sort order, 108109

sorting

by alphabetical order, 108

information

ORDER BY clause, SELECT queries, 108111

SELECT statement, 105107

UNION, 381383

special predicate keywords, subqueries

ALL, 417420

ANY, 417420

EXISTS, 420422

IN predicate, 411417

SOME, 417420

specifying, explicit values, 132133

Specifying Queries As Relational Expressions (SQUARE), 73

splitting data into quintiles, 824827

SQL (Structured Query Language)

commercial implementations, 83

future of, 8384

history of SQL (Structured Query Language)

early vendor implementations, 7374

evolution of SQL/86, 7679

standards, 7576

origins of, 7273

reasons for learning, 84

set operations

classic versus SQL, 259262

EXCEPT, 265268

INTERSECT, 262265

UNION, 268271

standards. See standards

SQL Standard, structure of, 8182

SQL/86, 76

evolution of, 7679

SQL/89, 7677

SQL/92, 7879

SQL:2016, 8384

SQL/Data System (SQL/DS), 74

SQL/DS (SQL/Data System), 74

SQRT, 142

SQUARE (Specifying Queries As Relational Expressions), 73

standards

evolution of SQL/86, 7679

FIPS (Federal Information Processing Standard), 80

history of SQL (Structured Query Language), 7576

ODBC (Open Database Connectivity), 80

SAA (Systems Application Architecture), 80

SQL Standard, structure of, 8182

SQL/89, 7677

SQL/92, 7879

X/OPEN, 79

START TRANSACTION, 540

static data, 16

Stonebraker, Michael, 74

string comparison, case sensitivity, 197

string values, comparing, 181184

structure of

columns, fine-tuning, 3840

databases, 3435

SQL Standard, 8182

tables, 4850

Structured English Query Language (SEQUEL), 72

sub-groups, grouping, 750753

subqueries, 400, 422

aggregate functions, 457458

COUNT, 406408

MAX, 406408

as column expressions, 402405, 422423

deleting some rows, DELETE statement, 609611

in filter rows, 543546

as filters, 408411, 423424

row subqueries, 400402

sample statements, 424425

Bowling League Database, 428429, 434435

Entertainment Agency Database, 426427, 432433

in expressions, 425430

in filters, 430437

Recipes Database, 429430, 435437

Sales Orders Database, 425426, 430431

School Scheduling Database, 427428, 433434

scalar subqueries, 400, 402

special predicate keywords

ALL, 417420

ANY, 417420

EXISTS, 420422

IN predicate, 411417

SOME, 417420

table subqueries, 400, 402

UPDATE expressions, 548551

in WHERE clauses, GROUP BY, 483484

subtotal combinations, calculating with CUBE, 765770

SUM, computing totals, 450451

SYMMETRIC, 189190

syntax

CASE (conditional expressions), 678682

FULL OUTER JOIN, 344347

GROUP BY, 475481, 753

INNER JOIN, 276

OUTER JOIN, 324330

subqueries as column expressions, 402405

subqueries as filters, 408411

windows functions, 798800

syntax diagrams

aggregate functions, 444

CASE expression, 679

column references, 276

comparison condition, 181

concatenation expressions, 138

date and time literals, 136

date expressions, 147

DELETE statement, 604

FULL OUTER JOIN, 344

INSERT statement with SELECT expressions, 581

INSERT statement with VALUES clause, 575

mathematical expressions, 143

naming expressions, 153

NOT operator, 202

Null condition, 199

numeric literals, 135

pattern match condition, 195

predicates, 681682

query using INNER JOIN on two tables, 277

range condition, 189

search conditions, 205

SELECT statement that includes value expression, 159

SELECT statement with GROUP BY clause, 475

SELECT statement with WHERE clause, 177

simple SELECT statements, 403

time expressions, 149

UNION JOIN, 348

UNION statement, 370

using UNION to combine two simple SELECT statements, 372

utilizing, 59

Value Expression, 679

value expressions, 157, 549

System R, 18, 72, 73

T

table references, 345

table subqueries, 400, 402

tables

assigning correlation names to, INNER JOIN, 282284

derived tables, 284

driver tables, 717

Bowling League Database, 742743

Entertainment Agency Database, 737739

Sales Orders Database, 736737

School Scheduling Database, 739742

setting up, 717720

using, 720725

fine-tuning, 46

naming conventions, 4648

relationships, 6063

relationships, deletion rule, 6364

relationships, participation, 6468

resolving duplicate columns, 5056

structure of tables, 4850

INNER JOIN, 277282

linking, embedding JOINs within JOINs, 286291

linking tables, 4445

naming conventions, 4648

OUTER JOIN, 324330

primary keys, 5660

relational databases, 2021

unlinked tables, 713

creating, 710712

techniques for grouping, variations on, 775780

TIME, 136

time expressions, 149150

SELECT statement, 156

syntax diagrams, 149

TIMESTAMP, 137, 138

TOP, 111

totals

calculating totals on combinations, using CUBE, 765770

computing with SUM, 450451

creating a union of totals, with GROUPING SETS, 771775

in hierarchies, ROLLUP, 754765

transactions, 540541

translating requests into SQL, SELECT statement, 9599

translation statements, 108

triggers, 549, 551

tuple. See rows

type of participation, relationships, 6468

types of data, expressions, 127130

types of expressions

concatenation expressions, 138141

date and time arithmetic expressions, 138, 146

date expressions, 147148

time expressions, 149150

mathematical expressions, 138, 142146

U

underscore (_), 195

UNION, 268271, 369372

combining three tables, 380

DISTINCT, 389

fetching data from two tables, 371

sample statements, 385395

Bowling League Database, 392394

Entertainment Agency Database, 389390

Recipes Database, 394395

Sales Orders Database, 385389

School Scheduling Database, 390392

sorting, 381383

syntax diagrams, 370

combining two simple SELECT statements, 372

uses for, 383384

using more than once, 379381

writing requests, 372

combining complex SELECT statements, 375379

using simple SELECT statements, 372375

union, 243

problems you can solve with, 261262

result sets, combining, 259261

set theory, 257259

UNION ALL, 371

UNION JOIN, 348

union of totals, creating with GROUPING SETS, 771775

unknown values. See Null, 161

unlinked data, 710713

driver tables, 717

setting up, 717720

using, 720725

sample statements

driver tables, 736743

unlinked tables, 726736

solving problems, 714716

unlinked tables, 713

Bowling League Database, 734735

creating, 710712

Entertainment Agency Database, 728731

Sales Orders Database, 726728

School Scheduling Database, 731734

UPDATE clause, JOIN, 546548

UPDATE expressions

simple UPDATE expression, 537538

subqueries, 548551

UPDATE statement, 536537

sample statements, 552553

Bowling League Database, 565568

Entertainment Agency Database, 558562

Sales Orders Database, 554557

School Scheduling Database, 562565

subqueries in filter rows, 543546

updating

multiple columns, 541543

selected rows, 631633

uses for, 551552

updating

multiple columns, 541543

selected rows, 631633

USING clause, 280281

USING syntax, OUTER JOIN, 329

V

Value Expression, 679680

syntax diagram, 679

value expressions, 157159

syntax diagrams, 157, 549

values

converting into data types, CAST function, 130132

finding largest values, with MAX, 452454

inserting with INSERT statements, 575578

literal values, 132133

matching values, finding with INNER JOIN, 293294

mean values, calculating with AVG, 451452

missing values, 161

finding with OUTER JOIN, 349

non-key values, FULL OUTER JOIN, 347348

Null, 159162

smallest values, finding with MIN, 454455

unknown values, 161

VALUES clause

DELETE statement, 603604

INSERT statement, 575578

syntax diagrams, INSERT statement, 575

VALUES keyword, 576

Venn, John, 246247

Venn diagram, 246247

verifying you’re updating the correct rows, 632633

DELETE statement, 607608

views, relational databases, 2324

VM/CMS operating system, 74

W

WHERE clause, 176178

CASE (conditional expressions), 691

comparison predicate, 181

comparing string values, 181184

equality and inequality, 185186

less than and greater than, 186188

DELETE statement, 603604

deleting, some rows, 607

filtering, 510513

IS NULL, unknown values, 199201

LIKE predicate, pattern match condition, 194199

IN predicate, membership condition, 192194

range condition, 189192

row subqueries, 401402

SELECT statement, 93

subqueries

as filters, 423424

GROUP BY, 483484

using, 179181

WIDTH_BUCKET, 142

windows functions, 794798

aggregate functions, 827834

calculating row number, 814818

OVER()

ORDER BY clause, 805810

PARTITION BY, 800801

ROWS (or RANGE), 811814

ranking data with RANK(), 818824

Sales Orders Database, 838839

sample statements, 834835

aggregate functions, 844852

NTILE(), 842844

RANK(), DENSE_RANK, and PERCENT_RANK, 838841

ROW_NUMBER(), 835838

School Scheduling Database, 840841

splitting data into quintiles with NTILE(), 824827

syntax, 798800

WITH RECURSIVE, 717

Wong, Eugene, 74

writing requests with UNION, 372

combining complex SELECT statements, 375379

using simple SELECT statements, 372375

X–Y

X3, 75

X3H2, 7576

evolution of SQL/86, 7679

XML (eXtensible Markup Language), 19

X/OPEN, 79

Z

zero, 160

zero-length string, 160