@@ -440,27 +440,26 @@ SELECT DISTINCT city
440
440
Thus far, our queries have only accessed one table at a time.
441
441
Queries can access multiple tables at once, or access the same
442
442
table in such a way that multiple rows of the table are being
443
- processed at the same time. A query that accesses multiple rows
444
- of the same or different tables at one time is called a
445
- <firstterm>join</firstterm> query. As an example, say you wish to
446
- list all the weather records together with the location of the
447
- associated city. To do that, we need to compare the <structfield>city</structfield>
443
+ processed at the same time. Queries that access multiple tables
444
+ (or multiple instances of the same table) at one time are called
445
+ <firstterm>join</firstterm> queries. They combine rows from one table
446
+ with rows from a second table, with an expression specifying which rows
447
+ are to be paired. For example, to return all the weather records together
448
+ with the location of the associated city, the database needs to compare
449
+ the <structfield>city</structfield>
448
450
column of each row of the <structname>weather</structname> table with the
449
451
<structfield>name</structfield> column of all rows in the <structname>cities</structname>
450
- table, and select the pairs of rows where these values match.
451
- <note>
452
+ table, and select the pairs of rows where these values match.<footnote>
452
453
<para>
453
454
This is only a conceptual model. The join is usually performed
454
455
in a more efficient manner than actually comparing each possible
455
456
pair of rows, but this is invisible to the user.
456
457
</para>
457
- </note >
458
+ </footnote >
458
459
This would be accomplished by the following query:
459
460
460
461
<programlisting>
461
- SELECT *
462
- FROM weather, cities
463
- WHERE city = name;
462
+ SELECT * FROM weather JOIN cities ON city = name;
464
463
</programlisting>
465
464
466
465
<screen>
@@ -497,23 +496,13 @@ SELECT *
497
496
<literal>*</literal>:
498
497
<programlisting>
499
498
SELECT city, temp_lo, temp_hi, prcp, date, location
500
- FROM weather, cities
501
- WHERE city = name;
499
+ FROM weather JOIN cities ON city = name;
502
500
</programlisting>
503
501
</para>
504
502
</listitem>
505
503
</itemizedlist>
506
504
</para>
507
505
508
- <formalpara>
509
- <title>Exercise:</title>
510
-
511
- <para>
512
- Attempt to determine the semantics of this query when the
513
- <literal>WHERE</literal> clause is omitted.
514
- </para>
515
- </formalpara>
516
-
517
506
<para>
518
507
Since the columns all had different names, the parser
519
508
automatically found which table they belong to. If there
@@ -524,8 +513,7 @@ SELECT city, temp_lo, temp_hi, prcp, date, location
524
513
<programlisting>
525
514
SELECT weather.city, weather.temp_lo, weather.temp_hi,
526
515
weather.prcp, weather.date, cities.location
527
- FROM weather, cities
528
- WHERE cities.name = weather.city;
516
+ FROM weather JOIN cities ON weather.city = cities.name;
529
517
</programlisting>
530
518
531
519
It is widely considered good style to qualify all column names
@@ -535,15 +523,24 @@ SELECT weather.city, weather.temp_lo, weather.temp_hi,
535
523
536
524
<para>
537
525
Join queries of the kind seen thus far can also be written in this
538
- alternative form:
526
+ form:
539
527
540
528
<programlisting>
541
529
SELECT *
542
- FROM weather INNER JOIN cities ON (weather.city = cities.name);
530
+ FROM weather, cities
531
+ WHERE city = name;
543
532
</programlisting>
544
533
545
- This syntax is not as commonly used as the one above, but we show
546
- it here to help you understand the following topics.
534
+ This syntax pre-dates the <literal>JOIN</literal>/<literal>ON</literal>
535
+ syntax, which was introduced in SQL-92. The tables are simply listed in
536
+ the <literal>FROM</literal> clause, and the comparison expression is added
537
+ to the <literal>WHERE</literal> clause. The results from this older
538
+ implicit syntax and the newer explicit
539
+ <literal>JOIN</literal>/<literal>ON</literal> syntax are identical. But
540
+ for a reader of the query, the explicit syntax makes its meaning easier to
541
+ understand: The join condition is introduced by its own key word whereas
542
+ previously the condition was mixed into the <literal>WHERE</literal>
543
+ clause together with other conditions.
547
544
</para>
548
545
549
546
<indexterm><primary>join</primary><secondary>outer</secondary></indexterm>
@@ -556,12 +553,12 @@ SELECT *
556
553
found we want some <quote>empty values</quote> to be substituted
557
554
for the <structname>cities</structname> table's columns. This kind
558
555
of query is called an <firstterm>outer join</firstterm>. (The
559
- joins we have seen so far are inner joins.) The command looks
560
- like this:
556
+ joins we have seen so far are <firstterm> inner joins</firstterm>.)
557
+ The command looks like this:
561
558
562
559
<programlisting>
563
560
SELECT *
564
- FROM weather LEFT OUTER JOIN cities ON ( weather.city = cities.name) ;
561
+ FROM weather LEFT OUTER JOIN cities ON weather.city = cities.name;
565
562
</programlisting>
566
563
567
564
<screen>
@@ -591,10 +588,9 @@ SELECT *
591
588
</para>
592
589
</formalpara>
593
590
591
+ <indexterm><primary>join</primary><secondary>self</secondary></indexterm>
592
+ <indexterm><primary>alias</primary><secondary>for table name in query</secondary></indexterm>
594
593
<para>
595
- <indexterm><primary>join</primary><secondary>self</secondary></indexterm>
596
- <indexterm><primary>alias</primary><secondary>for table name in query</secondary></indexterm>
597
-
598
594
We can also join a table against itself. This is called a
599
595
<firstterm>self join</firstterm>. As an example, suppose we wish
600
596
to find all the weather records that are in the temperature range
@@ -608,10 +604,9 @@ SELECT *
608
604
609
605
<programlisting>
610
606
SELECT w1.city, w1.temp_lo AS low, w1.temp_hi AS high,
611
- w2.city, w2.temp_lo AS low, w2.temp_hi AS high
612
- FROM weather w1, weather w2
613
- WHERE w1.temp_lo < w2.temp_lo
614
- AND w1.temp_hi > w2.temp_hi;
607
+ w2.city, w2.temp_lo AS low, w2.temp_hi AS high
608
+ FROM weather w1 JOIN weather w2
609
+ ON w1.temp_lo < w2.temp_lo AND w1.temp_hi > w2.temp_hi;
615
610
</programlisting>
616
611
617
612
<screen>
@@ -628,8 +623,7 @@ SELECT w1.city, w1.temp_lo AS low, w1.temp_hi AS high,
628
623
queries to save some typing, e.g.:
629
624
<programlisting>
630
625
SELECT *
631
- FROM weather w, cities c
632
- WHERE w.city = c.name;
626
+ FROM weather w JOIN cities c ON w.city = c.name;
633
627
</programlisting>
634
628
You will encounter this style of abbreviating quite frequently.
635
629
</para>
0 commit comments