I want to select all sections which do not have any students enrolled
These are the three tables:
ENROLLMENTS
student_id, section_id
SECTIONS
course_id, section_id
COURSES
course_id, description
The output table should look like this:
course_id | description | section_id
I'm not shure which join to use.
So you want
SELECT
course_id
, description
, section_id
FROM
COURSES
INNER JOIN
SECTIONS
ON
COURSES.course_id = SECTIONS.course_id
LEFT JOIN
ENROLLMENTS
ON
ENROLLMENTS.section_id = SECTIONS.section_id
WHERE
ENROLLMENTS.student_id IS NULL;
This will take all of the information from COURSES and SECTIONS, then join it to ENROLLMENTS, keeping all of the information for COURSES and SECTIONS where there an no enrollments. Then by selecting where the ENROLLMENTS student_id is NULL we find all the entries that didn't have section_ids in SECTIONS.
Why not using a subquery?
SELECT
sections.course_id,
courses.description,
sections.section_id
FROM
courses INNER JOIN sections ON courses.course_id = sections.course_id
WHERE
courses.section_id NOT IN (SELECT section_id FROM enrollments)
Related
I have three tables: exams, subjects and students.
Students table has foreign key subjectId and Subjects table has foreign key examId.
I need to retrieve all exams for desired studentId. So I need sql select that will pick up all subjects for that studentId, and pick up all exams for all these subjects.
So pseudo code is something like:
SELECT * FROM exams
WHERE id IN (SELECT examId FROM subjects
WHERE id IN (SELECT subjectId from students
where id === desiredId))
Why are you using subqueries and just not inner join?
Try with this:
Select X.* From exams X
Inner join subjects S on S.examId = X.id
Inner join students T on T.subjectId = s.Id
Where T.id =#yourStudentId
For example, I have 3 tables: teachers, students and persons.
Teachers with teacher_id and person_id,
Students with student_id and person_id,
Persons with person_id and some information about him.
How should I use constraint to avoid situation, when the same person is teacher and student. I know how to use constraint in simple ways, like limiting dates or other integer values. But here I have no idea how to connect and constraint them.
If you want to SELECT all persons, but the students who are teachers, you can put that in your WHERE clause:
SELECT *
FROM persons p
INNER JOIN student_id s
ON s.person_id = p.person_id
INNER JOIN teacher_id t
ON t.person_id = p.person_id
WHERE student_id != teacher_id
Update
Use the WHERE clause in a CASE statement while creating your index:
CREATE UNIQUE INDEX index_person ON index_table(
CASE WHEN student_id != teacher_id
THEN person_id
ELSE NULL
END
);
I have a database that has the following tables
Courses: CourseID*, CourseName, TeacherID
Teachers: TeacherID*, TeacherName
Students: StudentID*, StudentName
StudentCourses: CourseID*, StudentID*
Legend: * is the primary key.
How can I write to query that produces produces the number of students taught by each teacher?
For example
TeacherName, Count
Bob 15
Sarah 5
Zubair 1
Edit
select "TeacherName", count(*)
from courses inner join teachers on courses."TeacherID" = teachers."TeacherI"
join sutdentcourses on sutdentcourses."CourseID" = courses."CourseID"
group by "TeacherName"
order by "TeacherName";
You need make JOIN between the tables Teachers, Courses and StudentCourses, Group your records by TeacherName and Count the StudentID.
SELECT TeacherName, COUNT(StudentID)
FROM Teachers JOIN Courses USING(TeacherID)
JOIN StudentCourses USING(CourseID)
GROUP BY TeacherName
Here is my code that I created for each of the tables, not sure how to join the tables to calculated the gpa's??
CREATE TABLE Student(SSN NUMBER(4) NOT NULL,
SName VARCHAR(20) NOT NULL,
Major VARCHAR(4),
Because grade and SSN are in the grades table, you dont need to join and can get the average grade like this:
This gives the average grade per student, ordered by the grade:
SELECT AVG(g.Grade), g.SSN
FROM Grade g
group by g.SSN
order by AVG(g.Grade)
If you wanted average grade per course it would look like this:
SELECT AVG(g.Grade), g.cno
FROM Grade g
group by g.cno
order by AVG(g.Grade)
However, if you need more information from student, you'd need to join to the student table:
select *
from (
SELECT AVG(g.Grade) as average_grade, g.SSN
FROM Grade g
group by g.SSN) a
inner join Student s on a.ssn = s.ssn
I have 3 table, named as
students (id, name)
subjects (id, name, is_published)
student_subjects (id, student_id, subject_id)
The subjects taken by a student goes in student_subjects. But there are cases where student_subjects contains NULL subject_id for a student
Here's the data for Students table
1;"John"
2;"Ahmeah"
3;"Dina"
4;"Leo"
5;"Lenon"
Subjects Table
1;"Computer Sci";1
2;"Physics";1
3;"Bio";1
4;"Maths";0
Student_subjects
1;1;1
2;1;2
3;1;4
4;2;1
5;2;3
6;2;4
7;3;2
8;4;1
9;5;NULL
Currently to fetch all the students with their subjects and also display the name of the student who has no subject attached to him, I am using the query as follows
SELECT
students.*,
inners.name
FROM
students
LEFT JOIN ( SELECT
student_id,
subjects.name
FROM
student_subjects
JOIN subjects ON ( student_subjects.subject_id = subjects.id AND subjects.is_published = 1)
) AS inners ON (inners.student_id = students.id )
Is there a better way to do the same http://sqlfiddle.com/#!12/9cf93/12
SELECT s.*, su.name AS SubjName
FROM students AS s
LEFT JOIN student_subjects AS ss ON ss.student_id = s.id
LEFT JOIN subjects AS su ON su.id = ss.subject_id
Does the same thing as the one you listed. Is that what you are after?