how to write this SQL? specifics inside - sql

Assume we have a table which records grades of all students of a class. There are two columns among others in the table: 1) student_id, 2) grades, The value of grades is a single letter which can be "A", "B", "C" or "F". How to write a SQL listing all student ids (one student per line) who has never got a "B" grade? Thanks.
PS: Assume we are using MySQL.

You would need a subquery to accomplish this. You'll return records from the table where the student_id is not in the list of students who have received a B.
select student_id, grades
from table_name
where student_id not in (select student_id from table_name where grade = 'B')

Use this and sorry, you need to define that student_id can be repeated in table
select student_id from table_name where grades in ('A','B','C')
and student_id not in(
select student_id from table_name where grades = 'B'
)

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;

Inserting dynamic amount of rows based off Amount of IDS found from search

I am trying to insert into the table TAKES(ID, COURSEID, SEC_ID, SEMESTER, YEAR , GRADE) off all the students who have not taken a certain course.
I correctly get the IDS needed from the table with the call
select ID from student
where dept_name = 'Computer Science'
minus
select ID from takes
where course_id = 'CS-347';
then I go to actually insert it with these IDS that I have retrieved and all the other fields for insert are static.
insert into TAKES
SELECT ID,'CS-347' as COURSE_ID,1 as SEC_ID,'Spring' as SEMESTER,2021 as YEAR,NULL as GRADE
from student
where dept_name = 'Computer Science'
minus
select ID from takes
where course_id = 'CS-347';
I then get the an error:
Incorrect number of result columns.
I know that I am only pulling from the Student column, but I'm not sure how to work around this as in I have tried selecting the IDS individually and that didn't work either.
you can use this query instead :
insert into TAKES (column names)
SELECT
ID,
'CS-347' as COURSE_ID,
1 as SEC_ID,
'Spring' as SEMESTER,
2021 as YEAR,
NULL as GRADE
from
student
where
dept_name = 'Computer Science'
and ID NOT IN (select
ID
from
takes
where
course_id = 'CS-347');
when you use minus both side of operation need of return them same number of columns . also make sure you are inserting the right columns , better to mention column names

How to insert new row without duplicating existing data

I want to insert rows in my table like so:
my columns are student,subject,class,teacher,level. Primary key is (student,subject). The table contains all the students, but the Math subject is missing for some of them, so I want to add it without duplicating the ones that already have it.
I've tried this but it gives me unique constraint violated:
insert into table (student,subject,class,teacher,level)
select a.student, 'math', null, null, null
from table a
where a.student in (select distinct student from table where subject not in 'math')
and (a.student,a.subject) not in (select student,subject from table);
I think you basically need select distinct:
insert into table (student, subject)
select distinct a.student, 'math'
from table a
where not exists (select 1
from table a2
where a2.student = a.student and
a2.subject = 'math'
);
One approach would be to use minus:
insert into course_students (student, subject)
select student, 'Math' from course_students
minus
select student, subject from course_students;
This would would need extending a little if you wanted to include other columns in the insert:
insert into course_students (student, subject, class, teacher, course_level)
select student, subject, '101', 'Naomi', 1
from ( select student, 'Math' as subject from course_students
minus
select student, subject from course_students );

How can I select distinct number of values from a column in SQL?

The database contains a REGISTRATION table and STUDENT table.
The columns of REGISTRATION table are :
CourseID, StudentID, CourseCode, Score, Year
The CourseCode column contains the codes of courses like CS-101, MS-202 (each student ID is registered to many courses). I need to find the names and ID's of students taking more than 3 courses.
I have tried:
Select distinct
CourseRegistrations.S_ID, Students.FirstName
from
Students, CourseRegistrations
where
Students.StudentID = CourseRegistrations.StudentID
group by
CourseRegistrations.S_ID, Students.FirstName
having
count(distinct CourseRegistrations.CourseCode) > 3
but this is showing all records of file.
You should try:
Select CourseRegistrations.StudentID, count(CourseRegistrations.CourseCode)
from Students, CourseRegistrations
where Students.StudentID=CourseRegistrations.StudentID
GROUP BY CourseRegistrations.StudentID
having count(CourseRegistrations.CourseCode)>3
Distinct is not need if you used GROUP BY
Select CusReg.S_ID
from Students stud join CourseRegistrations CusReg
ON stud.StudentID=CusReg.StudentID
GROUP BY CourseRegistrations.S_ID
having count(CusReg.CourseCode)>3
Dont Use OLD Style Joins
SOURCE

Is this sql query correct? If incorrect how can I fix it?

Schema:
Student(studentid,name,age)
Course(coursename,dept)
enroll(studentid,course,grade)
I need to find , for students in each age group find their average grade for courses they have taken for Political Science and History, and return the names of student with max average grade for each age group
My attempt so far is :
select max(grade), age, name
from (
select name, age, grade
from student s, (
select avg(grade) as grade, e.studentid
from enroll e
where dname in ('Political Sciences', 'History')
group by studentid
) as temp
where s.studentid = temp.studentid
) temp1
group by temp1.age;
I want to know if logically it is correct, and not syntactically.
Here's a few tips regarding your query:
Be careful with your table aliases. Make sure that you carry them over to your SELECT
You can only include columns in your SELECT that are being used in your aggregate (GROUP BY). Therefore, you can't GROUP BY temp1.age and SELECT age, name
The logic behind your SQL looks solid to me, so long as "Age" correlates to "Age Group", and does not refer to the individual student's age.