|
1 | 1 | <!--
|
2 |
| -$Header: /cvsroot/pgsql/doc/src/sgml/sql.sgml,v 1.15 2000/12/12 05:07:58 tgl Exp $ |
| 2 | +$Header: /cvsroot/pgsql/doc/src/sgml/sql.sgml,v 1.16 2001/01/09 15:48:18 momjian Exp $ |
3 | 3 | -->
|
4 | 4 |
|
5 | 5 | <chapter id="sql">
|
@@ -1029,6 +1029,239 @@ SELECT S.SNAME, P.PNAME
|
1029 | 1029 | named attributes have to be equal). Finally we project out all
|
1030 | 1030 | columns but S.SNAME and P.PNAME.
|
1031 | 1031 | </para>
|
| 1032 | + |
| 1033 | + <para> |
| 1034 | + Another way to perform joins is to use the SQL JOIN syntax as follows: |
| 1035 | + <programlisting> |
| 1036 | +select sname, pname from supplier |
| 1037 | +JOIN sells USING (sno) |
| 1038 | +JOIN part USING (pno); |
| 1039 | +</programlisting> |
| 1040 | +giving again: |
| 1041 | +<programlisting> |
| 1042 | + sname | pname |
| 1043 | +-------+------- |
| 1044 | + Smith | Screw |
| 1045 | + Adams | Screw |
| 1046 | + Smith | Nut |
| 1047 | + Blake | Nut |
| 1048 | + Adams | Bolt |
| 1049 | + Blake | Bolt |
| 1050 | + Jones | Cam |
| 1051 | + Blake | Cam |
| 1052 | +(8 rows) |
| 1053 | + </programlisting> |
| 1054 | + </para> |
| 1055 | + |
| 1056 | + <para> |
| 1057 | + A joined table, created using JOIN syntax, is a table reference list |
| 1058 | + item that occurs in a FROM clause and before any WHERE, GROUP BY, |
| 1059 | + or HAVING clause. Other table references, including table names or |
| 1060 | + other JOIN clauses, may be included in the FROM clause if separated |
| 1061 | + by commas. A JOIN of two tables is logically like any other |
| 1062 | + table listed in the FROM clause. A JOINed table can only be JOINed |
| 1063 | + to additional tables in a Qualified JOIN as indicated by the |
| 1064 | + elipses below. |
| 1065 | + </para> |
| 1066 | + |
| 1067 | + <variablelist> |
| 1068 | + <title>Join Types</title> |
| 1069 | + <varlistentry> |
| 1070 | + <term>CROSS JOIN</term> |
| 1071 | + <listitem> |
| 1072 | +<cmdsynopsis> |
| 1073 | + <arg choice="req"> <replaceable class="parameter">T1</replaceable> </arg> |
| 1074 | +<arg choice="plain">CROSS</arg> |
| 1075 | + <command> JOIN </command> |
| 1076 | + <arg choice="req"> <replaceable class="parameter">T2</replaceable> </arg> |
| 1077 | + </cmdsynopsis> |
| 1078 | + |
| 1079 | +<para> |
| 1080 | +A cross join takes two tables T1 and T2 having N and M rows |
| 1081 | +respectively, and returns a joined table containing a cross |
| 1082 | +product, NxM, of joined rows. For each row R1 of T1, each row |
| 1083 | +R2 of T2 is joined with R1 to yield a joined table row JR |
| 1084 | +consisting of all fields in R1 and R2. |
| 1085 | +</para> |
| 1086 | +</listitem> |
| 1087 | +</varlistentry> |
| 1088 | + |
| 1089 | +<varlistentry> |
| 1090 | + <term>Qualified JOINs</term> |
| 1091 | + <listitem> |
| 1092 | +<cmdsynopsis> |
| 1093 | +<arg choice="req"> <replaceable class="parameter">T1</replaceable> </arg> |
| 1094 | +<arg> |
| 1095 | +<group> |
| 1096 | +<arg choice="plain"> INNER </arg> |
| 1097 | +<arg> |
| 1098 | +<group> |
| 1099 | +<arg> LEFT </arg> |
| 1100 | +<arg> RIGHT </arg> |
| 1101 | +<arg> FULL </arg> |
| 1102 | +</group> |
| 1103 | +<arg> OUTER </arg> |
| 1104 | + </arg> |
| 1105 | +</group> |
| 1106 | + </arg> |
| 1107 | +<command> JOIN </command> |
| 1108 | +<arg choice="req"> <replaceable class="parameter">T2</replaceable> </arg> |
| 1109 | +<arg choice="req"> |
| 1110 | +<group> |
| 1111 | +<arg> ON <replaceable>search condition</replaceable></arg> |
| 1112 | +<arg> USING ( <replaceable>join column list</replaceable> ) </arg> |
| 1113 | +</group> |
| 1114 | +</arg> |
| 1115 | +<arg choice="plain"> ... </arg> |
| 1116 | +</cmdsynopsis> |
| 1117 | + |
| 1118 | +<para> |
| 1119 | +Only the qualified JOIN types can use ON or USING clauses. The ON clause |
| 1120 | +takes a <replaceable>search condition</replaceable>, which is the same |
| 1121 | +as in a WHERE clause. The USING clause takes a comma-separated list of |
| 1122 | +column names, which the joined tables must have in common, and joins |
| 1123 | +the tables on those columns, resulting in a joined table having one |
| 1124 | +column for each common column and all of the other columns from both tables. |
| 1125 | +Like all SELECT queries, the <replaceable>select list</replaceable> of the |
| 1126 | +SELECT query, before the FROM clause, decides which columns from the joined |
| 1127 | +table are in the resulttable returned. |
| 1128 | +</para> |
| 1129 | + |
| 1130 | +<!-- begin join semantics --> |
| 1131 | +<variablelist> |
| 1132 | +<varlistentry> |
| 1133 | +<term> |
| 1134 | +<cmdsynopsis> |
| 1135 | +<arg> INNER </arg> |
| 1136 | +<command> JOIN </command> |
| 1137 | +</cmdsynopsis> |
| 1138 | +</term> |
| 1139 | +<listitem> |
| 1140 | +<para> |
| 1141 | +For each row R1 of T1, the joined table has a row for each row |
| 1142 | +in T2 that satisfies the join specification with R1. |
| 1143 | +</para> |
| 1144 | +<tip> |
| 1145 | +<para> |
| 1146 | +The words INNER and OUTER are optional for all JOINs. |
| 1147 | +INNER is the default. LEFT, RIGHT, and FULL are for |
| 1148 | +OUTER JOINs only. |
| 1149 | +</para> |
| 1150 | +</tip> |
| 1151 | +</listitem> |
| 1152 | + </varlistentry> |
| 1153 | +<varlistentry> |
| 1154 | +<term> |
| 1155 | +<cmdsynopsis> |
| 1156 | +<arg choice="plain"> LEFT </arg> |
| 1157 | +<arg> OUTER </arg> |
| 1158 | +<command> JOIN </command> |
| 1159 | +</cmdsynopsis> |
| 1160 | + </term> |
| 1161 | +<listitem> |
| 1162 | +<para> |
| 1163 | + First, an INNER JOIN is performed. |
| 1164 | + Then, where a row in T1 does not satisfy the join specification |
| 1165 | +with any row in T2, a joined row is returned with null fields in |
| 1166 | +columns from T2. |
| 1167 | +</para> |
| 1168 | +<tip> |
| 1169 | +<para> |
| 1170 | +The joined table unconditionally has a row for each row in T1. |
| 1171 | +</para> |
| 1172 | +</tip> |
| 1173 | +</listitem> |
| 1174 | +</varlistentry> |
| 1175 | +<varlistentry> |
| 1176 | +<term> |
| 1177 | +<cmdsynopsis> |
| 1178 | +<arg choice="plain"> RIGHT </arg> |
| 1179 | +<arg> OUTER </arg> |
| 1180 | +<command> JOIN </command> |
| 1181 | + </cmdsynopsis> |
| 1182 | + </term> |
| 1183 | +<listitem> |
| 1184 | +<para> |
| 1185 | +Rule 1: For each row R2 of T2, the joined table has a row for each |
| 1186 | +row in T1 that satisfies the join specification with R2 (transposed |
| 1187 | +[INNER] JOIN). |
| 1188 | + Rule 2: Where a row in T2 does not satisfy the join specification |
| 1189 | +with any row in T1, a joined row is returned with null fields in |
| 1190 | +columns from T1. |
| 1191 | +</para> |
| 1192 | +<tip> |
| 1193 | +<para> |
| 1194 | +The joined table unconditionally has a row for each row in T2. |
| 1195 | +</para> |
| 1196 | +</tip> |
| 1197 | +</listitem> |
| 1198 | + </varlistentry> |
| 1199 | +<varlistentry> |
| 1200 | +<term> |
| 1201 | +<cmdsynopsis> |
| 1202 | +<arg choice="plain"> FULL </arg> |
| 1203 | +<arg> OUTER </arg> |
| 1204 | +<command> JOIN </command> |
| 1205 | + </cmdsynopsis> |
| 1206 | + </term> |
| 1207 | + <listitem> |
| 1208 | +<para> |
| 1209 | +First, a LEFT [OUTER] JOIN is performed. |
| 1210 | +Then, Rule 2 of a RIGHT [OUTER] JOIN is performed. |
| 1211 | +</para> |
| 1212 | +<tip> |
| 1213 | +<para> |
| 1214 | + The joined table unconditionally has a row for every row of T1 |
| 1215 | +and a row for every row of T2. |
| 1216 | +</para> |
| 1217 | +</tip> |
| 1218 | +</listitem> |
| 1219 | + </varlistentry> |
| 1220 | +</variablelist> |
| 1221 | +<!-- end join semantics --> |
| 1222 | + |
| 1223 | +</listitem> |
| 1224 | + </varlistentry> |
| 1225 | + |
| 1226 | +<varlistentry> |
| 1227 | + <term>NATURAL JOINs</term> |
| 1228 | + <listitem> |
| 1229 | +<cmdsynopsis> |
| 1230 | +<arg choice="req"> <replaceable class="parameter">T1</replaceable> </arg> |
| 1231 | +<arg> |
| 1232 | +<arg choice="plain"> NATURAL </arg> |
| 1233 | +<group> |
| 1234 | +<arg choice="plain"> INNER </arg> |
| 1235 | +<arg> |
| 1236 | +<group> |
| 1237 | +<arg> LEFT </arg> |
| 1238 | +<arg> RIGHT </arg> |
| 1239 | +<arg> FULL </arg> |
| 1240 | +</group> |
| 1241 | +<arg> OUTER </arg> |
| 1242 | + </arg> |
| 1243 | +</group> |
| 1244 | + </arg> |
| 1245 | +<command> JOIN </command> |
| 1246 | +<arg choice="req"> <replaceable class="parameter">T2</replaceable> </arg> |
| 1247 | +</cmdsynopsis> |
| 1248 | + |
| 1249 | +<para> |
| 1250 | +A natural join creates a joined table where every pair of matching |
| 1251 | +column names between the two tables are merged into one column. The |
| 1252 | +join specification is effectively a USING clause containing all the |
| 1253 | + common column names and is otherwise like a Qualified JOIN except |
| 1254 | +additional JOINs to the JOINed table are not permitted. |
| 1255 | +</para> |
| 1256 | +</listitem> |
| 1257 | +</varlistentry> |
| 1258 | + |
| 1259 | +<varlistentry> |
| 1260 | + <term>UNION JOIN</term> |
| 1261 | + <listitem><para>Deprecated.</para></listitem> |
| 1262 | +</varlistentry> |
| 1263 | + </variablelist> |
| 1264 | + |
1032 | 1265 | </sect3>
|
1033 | 1266 |
|
1034 | 1267 | <sect3>
|
|