SQL Server CREATE TABLE statement - sql

I have created the following table:
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)
);
I was wondering how to run a query to show the year level and the number of students in each year level I need only to show year levels that have at least two students.

Looks like homework assignment!
select year, count(1)
from Student
group by year
having count(1) >= 2

You can do like below. You need to group by year and count and filter where count >= 2
select year, count(*) as count from student group by year having count >= 2

SELECT Year,COUNT(StudentID) from Student GROUP BY Year having COUNT(studentID) > 2
since each student ID is for a unique student I assume you can count on them as well.

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

Related

How to group data which have same Id number in SQL

Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer.
The STATION table is described as follows:
CREATE TABLE STATION
(
Id int,
CITY varchar(50),
STATE varchar(50),
LAT_N int,
LONG_W int
)
I have this SQL which throws an error when I run:
SELECT CITY, STATE
FROM STATION
WHERE ID % 2 = 0
GROUP BY ID, CITY, STATE
There is no need to group by ID
select CITY,STATE from STATION where (ID % 2)=0 group by CITY,STATE
But you don't need a grouping function, DISTINCT will do also the trick
select DISTINCT CITY, STATE from STATION where (ID % 2)=0

SQL: select count with min rows in table

I have a table:
TABLE employee (
ID bigint,
name varchar,
department bigint
);
I would like to find a department that has minimal employees. (Count of rows in this table)
I believe this would require a HAVING statement with a nested sub-query, any help would be much appreciated.
I am using H2 database.
You could group by department and get the count of users in each department, order by the count and select top 1?
SELECT TOP 1
[department],
COUNT(*) AS [NoOfEmployees]
FROM [employee]
GROUP BY [department]
ORDER BY COUNT(*) ASC

SQL Query for getting total students who registered for more then 5 courses in a semester

I am new with sql queries so dont know much
i have a table named registration
this table has the following structure
Student_ID int,
Course varchar(15),
Score int,
Semester varchar(15),
Discipline varchar(10),
Campus varchar(15),
Degree varchar(10),
Year int
it does not contain any primary key it has the data of students from 4 different campuses of the same university so student_id is repeated
i am required total number of students who have taken more then 5 courses in a particular semester
i hope i have made the question clear kindly help if any one can.
Use GROUP BY and HAVING COUNT to get all the students that have taken more than five courses:
SELECT student_id
FROM yourtable
WHERE Semester = ....
GROUP BY student_id
HAVING COUNT(DISTINCT Course) > 5
To get the number of students you can count the number of rows that query returns:
SELECT COUNT(*) AS total FROM
(
SELECT student_id
FROM yourtable
WHERE Semester = ....
GROUP BY student_id
HAVING COUNT(DISTINCT Course) > 5
)
To get the students who are registered for more than 5 courses you do this...
SELECT CAMPUS, STUDENT_ID
FROM REGISTRATION
WHERE SEMESTER = 'GIVEN SEMESTER'
GROUP BY CAMPUS, STUDENT_ID
HAVING COUNT(*) > 5;
To get the number of students who are registered for more than 5 courses you do this...
SELECT COUNT(*)
FROM REGISTRATION
WHERE SEMESTER = 'GIVEN SEMESTER'
GROUP BY CAMPUS, STUDENT_ID
HAVING COUNT(*) > 5;
You need to group by campus AND student id since student id is repeated for different campuses. Also, the primary key should be composite (campus, student_id, semester, year, course) if I understand you correctly.
SELECT COUNT(DISTINCT(sub.Student_ID)) FROM
(
SELECT un.Student_ID, COUNT(*) FROM university un
GROUP BY un.Student_ID, un.Semester
HAVING
COUNT(un.Course)>5
) AS sub
I assume the table name is university

Need to correct Sql Query

I need a little help with the following query. I need to find:
Average number of people per semester of each campus
My table structure is:
table name - registration
columns
student_id
campus
year
batch
semester
and with the help of campus, year, semester and batch I can identify each unique semester. More over my student_id repeats itself in the db.
I did following but it won't help. So I need some help.
SELECT semester,year,campus
FROM regestration
GROUP BY semester, year, campus
ORDER BY count(*) desc
Try something like:
SELECT year,campus, AVG(CountOfStudents)
FROM
(
SELECT semester,year,campus, count(*) as CountOfStudents
FROM regestration
GROUP BY semester, year, campus
) t
GROUP BY year, campus
To get the number of students in each campus each semester you need to add COUNT(*) to your query:
SELECT semester, year, campus, COUNT(*) as students
FROM registration
GROUP BY semester, year, campus
I don't know what average you want here, though.
How about
select avg(students), campus from (
select count(student_id) students, campus from registration
group by semester, year, campus
) group by campus

SQL Server query correction in nested query

SELECT campus,semester, AVG(CountOfStudents)
FROM
(
SELECT semester,year,campus, count(*) as CountOfStudents
FROM regestration
GROUP BY semester, year, campus,student_id
) t
GROUP BY campus,semester
I need to do is to find the average number of people per semester of each campus
My table structure is:
Table name - registration
student_id
campus
year
batch
semester
campus, year, semester and batch these can help identify unique records, where as student_id may repeat itself the query above gives wrong answer.
Follow these steps:
remove student_ID from the GROUP BY clause and
add DISTINCT inside COUNT()
query,
SELECT campus, semester, AVG(CountOfStudents)
FROM
(
SELECT semester, year, campus, count(DISTINCT student_id) as CountOfStudents
FROM registration
GROUP BY semester, year, campus
) t
GROUP BY campus, semester