使用 WITH ... AS ... 语句增强 SQL 查询的可读性 | 始终

eg:

good

WITH t1 AS (
  SELECT
    foo,
    bar
  FROM
    tb1
  WHERE
    <conds>
),
t2 AS (
  SELECT
    baz
  FROM
    tb2
  WHERE
    <conds>
)

SELECT
  t1.foo,
  t1.bar,
  t2.baz
FROM
  t1 INNER JOIN t2 ON <conds>
WHERE
  <conds>;

bad

SELECT
  t1.foo,
  t1.bar,
  t2.baz
FROM (
    SELECT
      foo,
      bar
    FROM
      tb1
    WHERE
      <conds>
  ) AS t1 INNER JOIN (
    SELECT
      baz
    FROM
      tb2
    WHERE
      <conds>
  ) AS t2 ON <conds>
WHERE
  <conds>;