SQL Server query correction in nested query - sql

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

Related

Using Count and Average in one Query

I have a Table like that
and i want to see average score(if student have 3 scores for each course, student can join many course) for each student for each course.
But I couldn't create the sql query.
Are you just looking for aggregation?
select student_id, course_id, avg(score * 1.0) as avg_score
from exam_result
group by student_id, course_id
having count(*) >= 3;

SQL (Oracle) - Selecting rows containing a max() using 'having'

I have the following tables for a university database:
takes(ID, course_id, sec_id, semester, year, grade)
section(course_id, sec_id, semester, year)
I need to determine which sections had the maximum number of students enrolled in Fall semester of 2009.
I have tried variations of the following query:
with sec_enrollment as(
select course_id, sec_id, count(ID) as enrollment
from section natural join takes
where semester = 'Fall' and year = 2009
group by course_id, sec_id)
select course_id, sec_id, enrollment
from sec_enrollment
group by course_id, sec_id, enrollment
having enrollment = max(enrollment)
The above query returns all sections from Fall 2009 instead of just the sections with max enrollment. It seems that my 'having' clause is being ignored.
If I use the query:
with sec_enrollment as(
select course_id, sec_id, count(ID) as enrollment
from section natural join takes
where semester = 'Fall' and year = 2009
group by course_id, sec_id)
select max(enrollment)
from sec_enrollment
I can get the desired value for the maximum enrollment. I just can't figure out how to get the desired value along with course_id and sec_id of sections that contain that maximum. I'm thinking that the 'group by' clause is screwing things up, but I can't configure it any other way without drawing an error (ORA-00979: not a GROUP BY expression). Any help would be greatly appreciated
This is one way to do it using one more cte.
with sec_enrollment as (
select course_id, sec_id, count(ID) as enrollment
from section natural join takes
where semester = 'Fall' and year = 2009
group by course_id, sec_id)
, max_enrollment as (
select max(enrollment) as maxenrollment from sec_enrollment)
select course_id, sec_id
from sec_enrollment s
join max_enrollment m on s.enrollment = m.maxenrollment
Order the results in descending order by the enrollment count and then limit the results to 1 record.
with sec_enrollment as(
select course_id, sec_id, count(ID) as enrollment
from section natural join takes
where semester = 'Fall' and year = 2009
group by course_id, sec_id)
select course_id, sec_id, enrollment
from sec_enrollment
where ROWNUM = 1
order by enrollment DESC
You could write the query simpler as:
select course_id, sec_id, enrollment
from (
select s.course_id, s.sec_id, count(t.ID) as enrollment
from section s join takes t
where s.semester = 'Fall' and s.year = 2009
group by s.course_id, s.sec_id
order by count(t.ID)
)
where rownuw <= 1
When you have to find the item who has the max value, you don't need to find the max value and later find the item who has that value. There are two operations when you could just find the max and the item in the same operation, with a sort operation.

Get details semester wise highest marks without cursor

I have two tables.
Student (Roll_id,Student_name)
Student_mark (roll_id, semester, marks, subject)
I want student details who has got highest marks IN semester AND subject wise WITH their roll_id,mark,semester,subject
using that two table.
can we get results without using cursor?.
You can use the RANK() function to put the students in order by mark (partitioned by semester and subject):
WITH RankedResults AS
( SELECT s.Roll_ID,
s.Student_Name,
sm.Semester,
sm.Subject,
sm.Marks,
StudentRank = RANK() OVER(PARTITION BY sm.Semester, sm.Subject ORDER BY sm.Marks DESC)
FROM Student s
INNER JOIN Student_Mark sm
ON s.Roll_ID = sm.Roll_ID
)
SELECT Roll_ID, Student_name, Semester, Subject, Marks
FROM RankedResults
WHERE StudentRank = 1;
Example on SQL Fiddle

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