mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-09-11 01:25:24 +02:00
44 lines
2.4 KiB
TeX
44 lines
2.4 KiB
TeX
\newpage
|
|
\subsubsection{Join}
|
|
Specifying two (or more) tables in the \texttt{FROM} clause will perform a cartesian product, e.g.
|
|
\mint{sql}|SELECT * FROM (Table1, Table2);|
|
|
returns all rows of the tables, next to each other.
|
|
|
|
Here, naming the tables in the \texttt{FROM} clause may come in handy, to not write out the entire name of the tables:
|
|
\mint{sql}|SELECT * FROM (Table1 t, Table2 s) WHERE t.id = s.id;|
|
|
\mint{sql}|SELECT * FROM (Table1, Table2) WHERE Table1.id = Table2.id;|
|
|
Both of the above statements produce the same result.
|
|
|
|
|
|
\paragraph{Nested Queries}
|
|
\label{sec:nested-queries}
|
|
To not have the cartesian product executed in the \texttt{FROM} clause, or to get a much more complex starting table, we can use a \gls{subquery}.
|
|
These are parenthesized normal SQL queries.
|
|
|
|
We can create an alias for them using an \texttt{AS name} clause after the parenthesis as follows
|
|
\mint{sql}|SELECT r.name FROM (SELECT * FROM Table) AS r;|
|
|
or alternatively, using a \texttt{WITH} clause (replace the dummy queries with real queries):
|
|
\mint{sql}|WITH CteName AS (SELECT * FROM Table) SELECT name FROM CteName;|
|
|
Note that it is possible to have multiple with statements as follows:
|
|
\mint{sql}|WITH with1 AS (SELECT * FROM Table1), with2 AS (SELECT * FROM with1) SELECT name FROM with2|
|
|
\texttt{WITH} clauses are an implementation of \bi{Common Table Expressions} (CTE)
|
|
|
|
|
|
\paragraph{Join Operations}
|
|
If we don't specify the kind of join, an \texttt{INNER JOIN} is executed. The following three queries are equivalent
|
|
\mint{sql}|SELECT * FROM Student, Tests WHERE Student.PersNr = Tests.PersNr;|
|
|
\mint{sql}|SELECT * FROM Student JOIN Tests ON Student.PersNr = Tests.PersNr;|
|
|
\mint{sql}|SELECT * FROM Student JOIN Tests USING (PersNr);|
|
|
For the latter of the three, the parenthesis around the column are important, as omitting them is invalid syntax.
|
|
|
|
Other types of \texttt{JOIN} are (the latter two are considered to be \texttt{OUTER JOIN}s):
|
|
\begin{itemize}
|
|
\item \texttt{NATURAL JOIN} (no \texttt{ON} required), joins on columns with same name
|
|
\item \texttt{LEFT/RIGHT JOIN} (requires \texttt{ON}), also returns non-matching rows from left or right side, respectively
|
|
\item \texttt{FULL JOIN} (or \texttt{FULL OUTER JOIN}; requires \texttt{ON}), also returns non-matching rows from both sides
|
|
\end{itemize}
|
|
|
|
\begin{center}
|
|
\includegraphics[width=0.5\textwidth]{assets/join-types.png}
|
|
\end{center}
|