How to get the student academic progress? - sql

course Table
course_code course_name credit_points_reqd
1 Comp Science 300
2 Soft Engineering 300
subject Table
subject_code subject_name credit_points
CS123 C Prog 15
CS124 COBOL 15
enrolment table
student_id student_name course_code subject_code Results
1 Lara Croft 1 CS123 70
1 Lara Croft 1 CS124 50
2 Tom Raider 2 CS123 60
2 Tom Raider 2 CS124 40
3 James Bond 1 CS123 NULL
3 James Bond 1 CS124 40
OUTPUT TABLE
student_name course_name credit_points_obt credit_points_reqd
Lara Croft Comp Science 30 300
Tom Raider Soft Engineering 15 300
I'm currently using TSQL. So here's the situation. I've prepared these tables to get the output like the way it i showed u up there. I need to calculate the credit points obtained. Credit points are achieved if the student receives > 50 for a subject they took. I want to ignore students that has not received any credit points at all (eg, James Bond is ignored as he has not achieved any points yet)
select student_name, course_name,credit_points_obt,credit_points_reqd
FROM enrolment (SELECT student_full_name, SUM(credit_points) AS credit_points_obt
FROM enrolment
GROUP BY student_id),
Totally stuck...I have no idea where to go now.

You can sum conditionally to get points for subject. If none are given result will be null, so you filter out those student/course pairs in having clause.
I've changed > 50 condition to >= 50 because your results contradict your requirements. Also, by the data I'd say that you have omitted student table for brewity, but if you haven't, it is a must.
Live test is # Sql Fiddle.
select enrolment.student_name,
course.course_name,
course.credit_points_reqd,
sum(case when enrolment.results >= 50
then subject.credit_points
end) credit_points_obt
FROM enrolment
inner join course
on enrolment.course_code = course.course_code
inner join subject
on enrolment.subject_code = subject.subject_code
group by enrolment.student_name,
course.course_name,
course.credit_points_reqd
having sum(case when enrolment.results >= 50
then subject.credit_points
end) is not null

Related

joining 2 tables in sql which has no dependency on each other

I have 2 tables in the following way
Table 1:
e_id e_name e_salary e_age e_gender e_dept
---------------------------------------------------
1 sam 95000 45 male operations
2 bob 80000 21 male support
3 ann 125000 25 female analyst
Table 2:
d_salary d_age d_gender e_dept
----------------------------------
34000 25 male Admin
56000 41 female Tech
77000 35 female HR
I want the output something like this:
e_id e_name e_salary e_age e_gender e_dept d_salary d_age d_gender e_dept
1 sam 95000 45 male operations 34000 25 male Admin
2 bob 80000 21 male support 56000 41 female Tech
3 ann 125000 25 female analysts 77000 35 female HR
There is no dependency between the tables. No common columns. No primary or foreign key.
I tried using cross join that results in duplicate rows because it works on M X N
I am new to this SQL thing. Can someone help me, please? Thanks in advance
Though I didn't get the reason behind your desired output but you can get that with below query:
select a.e_id ,a.e_name ,a.e_salary ,a.e_age ,a.e_gender ,a.e_dept,b.d_salary ,b.d_age ,b.d_gender ,b.e_dept
from
(select e_id ,e_name ,e_salary ,e_age ,e_gender ,e_dept, row_number()over(order by e_id)rn
from table1)a
inner join
(select d_salary d_age d_gender e_dept,row_number()over(order by d_salary) rn
from table 2) b
on a.rn=b.rn
Generally you can create a row count using the row_number() window function on both tables and use this as join criterion. But this requires a certain order for both tables, which means that you have explicitly tell the query why is the Admin record ordered first and must be joined on the first record of table 1:
SELECT
*
FROM (
SELECT
*,
row_number() OVER (ORDER BY e_id) as row_count -- assuming e_id is your order criterion
FROM table1
) t1
JOIN (
SELECT
*,
row_number() OVER (ORDER BY /*whatever you expect to be ordered*/) as row_count
FROM table2
) t2
ON t1.row_count = t2.row_count

Calculate percentage of students SQL

How to calculate percentage of students with higher mark than average for each course?
Assume I have a table (avg_marks) with average marks for each course and number of students in the course:
course_id avg_mark num_students
12345 74 20
12346 70 17
12347 64 33
...
I also have a table (enrolments) with all courses, students enrolled in those courses and their mark for the course:
course_id student mark
12345 1010 63
12345 2111 75
12345 3221 85
12345 6788 40
...
12347 8989 90
...
The expected output would be the table with course id and percentage of students with higher marks than average:
course_id percentage
12345 40
12345 20
12346 50
...
I have calculated number of students who have higher mark than average, but somehow I wasn't able to calculate the percentage (perhaps because the table contains all courses?). How can I modify this query or make a new one to calculate the percentage of students with higher mark than average?
Number of students with higher than average mark:
SELECT e.course_id, COUNT(e.student)
FROM enrolments e, avg_mark av
WHERE e.mark > av.avg_mark AND e.course_id=av.course_id
Output of the above query was like the following:
course_id count
12345 5
12346 10
12347 8
...
You don't need the table avg_marks.
Use window function AVG() in enrolments to get the average mark for each course_id and then use conditional aggregation with AVG() aggregate function to get the percentage:
SELECT course_id,
ROUND(100 * AVG((mark > avg_mark)::int)) percentage
FROM (SELECT *, AVG(mark) OVER (PARTITION BY course_id) avg_mark FROM enrolments) e
GROUP BY course_id
See the demo.
select
e.course_id,
count(case when e.mark > av.avg_mark then e.student end)/count(*) * 100 as students_cnt_with_mark_above_avg_pct
from enrolments e, avg_mark av
where e.course_id = av.course_id
group by e.course_id

Avoid Null values in Coalesce

I have used coalesce to produce results but got duplicate rows with Null values.
Sample tables as below:
Table - Student
ID Student Subject id Subject Grade id
100 Peter 200 Chinese 201
101 Mary 300 English 302
102 Sam 400 Maths 403
103 John 900 Music
Chinese Table
Subject id Grade id Grade
200 201 Good
200 202 Average
200 203 Poor
English Table
Subject id Grade id Grade
300 301 Good
300 302 Average
300 303 Poor
Maths Table
Subject id Grade id Grade
400 401 Good
400 402 Average
400 403 Poor
Select Id, Name,
Coalesce (chinese.grade, english.grade, maths.grade)
from Student
Left join Chinese On student.id = Chinese.subjectId AND student.gradeId = Chinese.gradeId
Left join English On student.id = English.subjectId AND student.gradeId = Enlgish.gradeId
Left join Maths On student.id = Maths.subjectId AND student.gradeId = Maths.gradeId
Result
ID Student Subject Grade
100 Peter Chinese Good
100 Peter Chinese NULL
101 Mary English Average
101 Mary English NULL
102 Sam Maths Poor
102 Sam Maths NULL
103 John Music NULL
I am not sure where the duplicated row of Null values came from, just want to know how can I avoid giving null values by using coalesce?
Answering the "just want to know how can I avoid giving null values by using coalesce?" part of the question only, since the origin of the duplicate records requires more information than was provided:
COALESCE returns the first non-null value in its argument list, or NULL if all arguments are NULL.
So Select Id, Name, Coalesce (chinesetable.grade, englishtable.grade, mathstable.grade) ... will still give you a NULL grade if all of chinesetable.grade, englishtable.grade, and mathstable.grade are NULL. You need to decide what you want to do in that situation. You could either skip those rows:
Select * From
(Select Id, Name, Coalesce (chinesetable.grade, englishtable.grade, mathstable.grade) As Grade from Student where id = id...) As grades_with_nulls
Where Grade Is Not Null;
Or provide a default value as the last argument to Coalesce:
Select Id, Name, Coalesce (chinesetable.grade, englishtable.grade, mathstable.grade, 'None') from Student where id = id...
Use:
select s.id,s.name,s.[subject],
coalesce(coalesce(coalesce(c.grade,e.grade,c.grade),e.grade, coalesce(c.grade,e.grade,c.grade)),m.grade,coalesce(coalesce(c.grade,e.grade,c.grade),e.grade, coalesce(c.grade,e.grade,c.grade))) as grade
from student s
left join chinese c on s.subjectid = c.subjectid and c.gradeid = s.gradeid
left join english e on e.subjectid = s.subjectid and e.gradeid = s.gradeid
left join maths m on m.subjectid = s.subjectid and m.gradeid = s.gradeid
result
id--name----subject--grade
100 Peter Chinese Good
101 Mary English Average
102 Sam Maths Poor
103 John Music NULL

Using IN operator on a correlated subquery

Consider the following query on the two following Tables:
SELECT sid FROM Salesman SM WHERE sid IN(SELECT sid FROM Sale S WHERE s.sid<>sm.sid)
Sale Table
SALEID SID SLDATE
1001 1 01-Jan-14
1006 1 01-Jun-15
1003 4 01-Feb-14
1002 5 02-Jan-14
1005 2 01-Feb-14
1004 1 01-Mar-14
Salesman Table
SID SNAME LOCATION
1 Peter London
2 Michael Paris
3 John Mumbai
5 Kevin London
4 Harry Chicago
6 Alex Chicago
Why it doesn't fetch any results?
For example: on the first iteration of outer query, inner query should return 2,4,5. But it seems not. Why?
PS: Please ignore the practical use of such a query. This is for academic purpose.
Because it is impossible to fetch anything with this condition, I don't understand what you were trying to do..
As I see it you either meant to select all the sailsmans that didn't make any sales, and in that case this is the right query:
select sid from Salesman SM where sid not in (select sid from Sale S)
or you want only the salesmans that did make a sale, and in that case this is the right query:
select sid from Salesman SM where sid in (select sid from Sale S)

How to select students who got above average?

How to list all students who got above average grade of their group in SQL table? We have 6 group_ids so there six different average grades.
group_id student grade
1 James 85
1 Adam 96
2 Tom 56
2 Jane 89
2 Anny 90
Result:
group_id student grade
1 Adam 96
2 Jane 89
2 Anny 90
ashkufaraz's answer is closer but not quite right
select group_id,student,grade from students one where grade >
(select avg(grade) from students two where two.group_id = one.group_id)
The question is just tagged SQL, so this is an answer using standard SQL:
One option is to use a window function:
select group_id,student,grade
from (
select group_id,student,grade,
avg(grade) over (partition by group_id) as group_avg
from studends
) t
where grade > group_avg;
This has the additional benefit that you can also display the group average along with the result with no additional join or sub-select.