SQL:选择没有嵌套查询的查询

发布于 2024-12-01 10:16:48 字数 542 浏览 1 评论 0原文

我有下一个输入数据, 我需要编写两个表

create table Courses(
id number(19) not null,
name varchar(100),
);

create table Students (
id number (19) not null,
name varchar(100),
course_id number (19) not null,
);

来获取所有学生人数(例如超过 10 人)的课程 所以,我写了带有嵌套查询的变体,如下所示

select distinct courses.name from student, courses where courses.id=student.courses_id and (select count(Students.id) from Students where student.course_id = course.id)  > 10 

!没有测试,只是写这篇文章,它是例子! 所以,我的问题是如何在没有嵌套查询的情况下编写相同的查询?

I have next input data,
There are two tables

create table Courses(
id number(19) not null,
name varchar(100),
);

create table Students (
id number (19) not null,
name varchar(100),
course_id number (19) not null,
);

I need to write query for get all cources with count of student for example more than 10
So, I've written variant with nested query like this

select distinct courses.name from student, courses where courses.id=student.courses_id and (select count(Students.id) from Students where student.course_id = course.id)  > 10 

!!! Didn't test, just wrote for this post, it is example!!!
So, My question is how to write the same query without nested query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

腹黑女流氓 2024-12-08 10:16:48

使用GROUP BY/HAVING子句:

SELECT courses.name /*, COUNT(*)*/
FROM student JOIN courses ON courses.id = student.courses_id
GROUP BY courses.id
HAVING COUNT(*) > 10

Use the GROUP BY/HAVING clause:

SELECT courses.name /*, COUNT(*)*/
FROM student JOIN courses ON courses.id = student.courses_id
GROUP BY courses.id
HAVING COUNT(*) > 10
夕嗳→ 2024-12-08 10:16:48

我会和

Select c.name, count(s.id) as StudentsCount from Courses c join stundets S on c.id = s.course_id group by c.name having count(s.id) > 10

I would go with

Select c.name, count(s.id) as StudentsCount from Courses c join stundets S on c.id = s.course_id group by c.name having count(s.id) > 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文