gpt4 book ai didi

sql - 如何在 SQL 中编写 "exclusive"查询?

转载 作者:太空狗 更新时间:2023-10-30 01:51:42 24 4
gpt4 key购买 nike

我正在复习我正在参加的数据库类(class)的过去论文,但我遇到了一个 SQL 问题

这是提供的架构

  • Country(name, capital, area), name为key

  • People(country, population, children, adult) 其中国家是指国家名称,人口为总人口, child 和成人是 child 和成人人口的百分比。

  • Language(country,language,percentage) – 对于在国家/地区,它列出了说该语言的人口百分比。

问题是:

Write the following query in SQL: Find languages that are only spoken in countries whose total population exceeds 10^7.

这是我目前所拥有的:

SELECT l.language
FROM people p, language l
WHERE l.country = p.country AND
p.population > 10^7

我感到困惑的一点是如何检查是否没有其他国家/地区使用某种语言但人口少于 10^7。

有什么建议吗?谢谢

最佳答案

获取所有语言。从该集合中删除人口 <= 10^7 的国家/地区使用的所有语言。提醒应该是仅在人口 > 10^7 的国家/地区使用的语言。

select language from languages
where language not in (
select language from languages l
join people p on l.country = p.country
where p.population <= 10^7)

这是基于您的设计具有必须在至少一个国家/地区使用每种语言的限制;)

关于sql - 如何在 SQL 中编写 "exclusive"查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10096346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com