3

I'm doing Stanfords introduction to DB course and this is one of the homework assignments. My code does the job well, but I don't really like it how I reused the same SELECT-FROM-JOIN part twice:

SELECT name, grade
FROM Highschooler
WHERE
    ID IN (
        SELECT H1.ID
        FROM Friend
        JOIN Highschooler AS H1
            ON Friend.ID1 = H1.ID
        JOIN Highschooler AS H2
            ON Friend.ID2 = H2.ID
        WHERE H1.grade = H2.grade    
    ) AND
    ID NOT IN (
        SELECT H1.ID
        FROM Friend
        JOIN Highschooler AS H1
            ON Friend.ID1 = H1.ID
        JOIN Highschooler AS H2
            ON Friend.ID2 = H2.ID
        WHERE H1.grade <> H2.grade
    )
ORDER BY grade, name

This is the SQL schema for the two tables used in the code:

Highschooler(ID int, name text, grade int);
Friend(ID1 int, ID2 int);

I had to query all the Highschoolers that only have friends in the same grade, and not in any other grades. Is there a way to somehow write the code bellow only once, and reuse it two times for the two different WHERE clauses = and <>?

    SELECT H1.ID
    FROM Friend
    JOIN Highschooler AS H1
        ON Friend.ID1 = H1.ID
    JOIN Highschooler AS H2
        ON Friend.ID2 = H2.ID

EDIT: We are required to provide SQLite code.

4

4 回答 4

4

这是WHERE EXISTS查询的“典型孩子”示例:

SELECT name, grade
FROM Highschooler ME
WHERE EXISTS (
    SELECT 1
    FROM Friend F
    JOIN Highschooler OTHER on F.ID2=OTHER.ID
    WHERE F.ID1=ME.ID AND OTHER.Grade = ME.GRADE
)
AND NOT EXISTS (
    SELECT 1
    FROM Friend F
    JOIN Highschooler OTHER on F.ID2=OTHER.ID
    WHERE F.ID1=ME.ID AND OTHER.Grade <> ME.GRADE
)

EXISTS条件是它true是否SELECT返回一行或多行;否则为false。您需要做的就是内部子查询与外部子查询(F.ID1=ME.ID部分)相关联,并将您需要的剩余约束(theOTHER.Grade = ME.GRADE或 the OTHER.Grade <> ME.GRADE)添加到查询中。

于 2013-02-10T16:14:57.613 回答
4

这是关于与个人相关的群体的典型问题。当您遇到这样的问题时,一种方法是使用连接(成对查看事物)。通常更好的方法是使用聚合来一次查看整个组。

这里的见解是,如果您有一群朋友并且所有人都在同一年级,那么最低和最高成绩将是相同的。

该提示可能足以让您编写查询。如果是这样,请停在这里。

返回您想要的查询比您所做的要简单得多。你只需要看看朋友的成绩:

SELECT f.id1
FROM Friend f jJOIN
     Highschooler fh
     ON Friend.ID1 = fh.ID join
group by f.id1
having max(fh.grade) = min(fh.grade)

having子句确保所有内容都相同(忽略 NULL 值)。

编辑:

这个版本回答了这个问题:哪些高中生的朋友都在同一年级。你的问题模棱两可。也许你的意思是朋友原人都是同一个年级的。如果是这样,那么你可以通过一个小的修改来做到这一点。一种方法是将having子句更改为:

having max(fh.grade) = min(fh.grade) and
       max(fh.grade) = (select grade from Highschooler h where f.id1 = h.id1)

这将检查朋友原始人是否都在同一年级。

于 2013-02-10T16:16:47.507 回答
1

有时,当您将一些过滤连接转换为 UNION 或 MINUS/EXCEPT 等集合操作时,您可以获得更自然的查询形状。例如,您的查询可以写成(伪代码):

  SELECT H.id
  FROM Highschooler H
  JOIN .... | has a friend
  WHERE ... | in SAME grade

EXCEPT

  SELECT H.id
  FROM Highschooler H
  JOIN .... | has a friend
  WHERE ... | in OTHER grade

一些 SQL 引擎使用关键字“MINUS”,一些使用“EXCEPT”。

但请注意,与 UNION 非常相似,这将执行两个查询,然后过滤它们的结果。这可能具有与单个全能查询不同的性能,但请注意,这不一定更糟。很多时候我发现它甚至具有更好的性能,因为对单列的“例外”,特别是排序,非常快

此外,如果您的数据库引擎允许,您可能会尝试使用 View 或 CTE 来缩短原始查询,但我认为这样做没有多大意义,除了美学

于 2013-02-10T16:16:27.280 回答
0

一些数据库支持减号关键字。

select whatever
from wherever
where id in
(select id
 from somewhere
 where something
 minus
 select id
 from somewhere
 where something else
 )

其他数据库支持相同的概念,但使用关键字 except,而不是减号。

于 2013-02-10T16:14:04.977 回答