SQL Query Update ID with a number - sql

I have 3 tables
Student (studentID, TeacherID)
Teacher (TeacherID, Number)
a temp table StudentUpdate (StudentID, TeacherNumber)
How do I update the student.TeacherID with values from studentupdate.teacherNumber?
Please see the difference between teacherID and teacher Number. One is the PK and one is just a nvarchar column. Thanks in advance.

You can do that using below query. Assuming you have only one teacher for each student
update st
set st.TeacherID = t.TeacherID
from Student st
inner join StudentUpdate su on su.StudentID = st.StudentID
inner join Teacher t on t.Number = su.TeacherNumber

Related

SQL query with more than 2 tables

I'm doing an exercise on ORACLE SQL.
Currently I got 3 tables.
Student values = "student_id ,name"
Subjects values = "subject_id, name"
Scores values = "score, student_id, subject_id"
I'm trying to retrieve the following information from my database.
Name of student, Name and id of the subject and finally the score that has the student_id "34560".
SELECT scores.score,
scores.subject_id,
student.name,
subject.subject_id,
subject.name
FROM scores
INNER JOIN students
ON scores.student_id = '34560'
INNER JOIN subject
ON /* and here's where i'm lost*/
Is there a way to put all together from the first part of the query where I call the list of students with student_id = "34560" and then query that list to see if it matches with the subject_id?
Use in operator for list of student id
SELECT sc.score, sc.subject_id,
st.name, sb.subject_id, sb.name
FROM scores sc
INNER JOIN students st
ON sc.student_id = st.student_id
INNER JOIN subject sb
ON sc.subject_id=sb.subject_id
where sc.student_id in ('34560','add_anotherstudentid','add_anotherstudentid') //you can add multiple student id

Recursive sql query n to n in SQLite

I have a database table like that
Table student
_______________
id_student int (PK)
student_name VARCHAR
and I have a recursive loop:
A student can oversee many students, and a student can be overseen by many students
so a new table:
Table oversee
________________
id_student pk, fk
id_overseen pk, fk
date date
the problem is that I want to get the list that I have
I made an sql query:
with
sr1 as ( select s.student_name as over from student s, oversee o where o.id_student = s.id_student),
sr2 as (select s.student_name as overseen from student s, oversee o where o.id_overseen = s.id_student)
select distinct * from sr1, sr2;
the problem is that's the query returns the wrong answers
I mean if we have two lines in the table, it will return 4 lines.
I want to get every student with his overseen:
Student | overseen.
Someone has any idea please?
Thanks.
I want to get a table with student | overseen | date
SELECT s.student_name AS student
, s2.student_name AS overseen
, oversee.date
FROM student s
JOIN oversee ON oversee.id_student = s.id_student
JOIN student s2 ON s2.id_student = oversee.id_overseen

SQL Server: update column with select result

I have table Student with a column called IdStudent.
The value of IdStudent is 0
Table Student also have a column called UID
I need to update IdStudent in table Student with IdCandidate in table Candidate.
Table Candidate also have UID column containing the same UID of table Student.
So we can do this to have IdCandidate:
select C.IdCandidate from Candidate as C inner join Student as S
on C.UID = S.UID
How can I update IdStudent in table Student with this IdCandidate obtained in this select?
Thanks!
Use JOIN in update
update s set s.IdStudent = C.IdCandidate
from Candidate as C
inner join Student as S on C.UID = S.UID
You can do it using the following query :
update S set S.IdStudent = C.IdCandidate
from Student S
inner join Candidate C on S.UID=C.UID

How to query for many to many relationship in Sql Server

Here is my tables
My question is How to get CourseNames for a specific student id
I tried this but didn't work
select Course.CourseName from Course
where Course.CourseId in (
select Student.studentname ,StudentCourse.CourseId from Student inner join StudentCourse
on Student.StudentId = StudentCourse.StudentId
where Student.StudentId = 1)
You can forget my query because i am new in SQL Server just tell me what exactly developers do in SQL Server real-world to get course names of a specific student
As you said you want to know the approach this is just basic viewpoint
1) We want to look at CourseName's.
SELECT CourseName FROM Course
2) One Student may have more than one Courses.
3) So we have one more table which is StudentCourse to achieve this.
4) We have to look CourseName's ID'S in this table
SELECT CourseID FROM StudentCourse
5) to find which students(X is a number you seach for) takes those courses.
WHERE StudentID = X
6) If we look them together, we now have all CourseName's via step 1. But we don't want all CourseName's and we have all CourseID's which X numbered student takes. So if we get them together, now we will just select CourseName's which X takes.
WHERE CourseID IN
7) So our final result is
SELECT CourseName FROM Course WHERE CourseID IN
(SELECT CourseID FROM StudentCourse WHERE StudentID = X)
Check this or this one to see how it works.
I'm using left joins just in case your student doesn't have any courses assigned, otherwise, if you use inner joins, you'll get no results;
SELECT
s.StudentID
,s.StudentNam
,sc.CourseID
,c.CourseName
FROM Student s
LEFT JOIN StudentCourse sc
ON s.StudentID = sc.StudentID
LEFT JOIN Course c
ON sc.CourseID = c.CourseID
WHERE s.StudentID = 1
try this:
select CourseName from Course
where CourseId in
(select CourseId from StudentCourse
where StudentId = 1)

Multiple joins onto same table

i have the following tables:
TABLE: teachers:
teacherID
teacherName
TABLE: students:
studentID
studentName
teacherID
advisorID
so, usually, i know i can get a single row per student, with their teachers name using an INNER JOIN.
but in this case - the advisor and tacher - are from the same teachers table. so how can i join onto the teachers table twice - once getting the teacher name, and then again to get the advisor name?
hope this is clear
thanks!
This lists students with the names of their teachers and advisors if any, in alpha order of student, without either (a) the teacher or (b) the advisor having to exist. If you want only where those names exist, change the respective join to an INNER join.
SELECT s.studentname as [Student], t.teachername as [Teacher], a.teachername as [Advisor]
FROM Students s
LEFT JOIN Teachers t ON s.TeacherID = t.TeacherID
LEFT JOIN Teachers a ON s.AdvisorID = a.TeacherID
ORDER BY 1, 2
You can join to the same table more than once, just give it a different alias for each join, and name your fields in a descriptive enough way. Use a left join if there might not be a link, but if a student always has both a teacher and an advisor, a straight join should be fine.
Something like this:
select s.studentname student
, t.teachername teacher
, a.teachername advisor
from students s
join teacher t
on t.teacherID = s.teacherID
join teacher a
on a.teacherID = s.teacherID
Why not try something like the following. Its been a while since I've done SQL so this may not work.
SELECT s.studentName AS Student, t.teacherName AS Teacher, a.teacherName AS Advisor
FROM teachers t, teachers a, students s
WHERE t.teacherID = s.teacherID AND a.teacherID = s.advisorID