SQL Server CREATE TABLE语句

时间:2018-07-05 00:35:22

标签: sql sql-server

我已经创建了下表:

CREATE TABLE Student 
(
    StudentID int PRIMARY KEY,
    Name varchar(30),
    Age int,
    Course varchar(30), 
    Year int,
    Address varchar(50),
    Phone varchar(12),
    Email varchar(50)
); 

我想知道如何运行查询以显示年级以及每个年级的学生人数,我只需要显示至少有两个学生的年级。

4 个答案:

答案 0 :(得分:1)

看起来像作业!

select year, count(1)
from Student
group by year
having count(1) >= 2

答案 1 :(得分:0)

您可以按照以下方式进行操作。您需要group by年,countfilter,其中count >= 2

select year, count(*) as count from student group by year having count >= 2 

答案 2 :(得分:0)

SELECT Year,COUNT(StudentID) from Student GROUP BY Year having COUNT(studentID) > 2

由于每个学生证都是一个唯一的学生,所以我想您也可以依靠他们。

答案 3 :(得分:0)

select Year, count (*) from Student
group by Year
having count(*) > 1