Linking of field data to field name between two tables - sql

I have one table name Class_sub where I have subjects name according to the classes, I have weekly_test table where the same subject's marks have to be entered.
My problem is to show subject name as field name in weekly_test table reference to class_sub table data fields.

If you have the following tables:
CLASS
id
name
SUBJECT
id
name
CLASS_SUBJECT
id
class_id
subject_id
WEEKLY_MARKS
id
class_subject_id
test_date
mark
The following query will work:
SELECT s.name as subject_name,
c.name as class_name,
wm.test_date,
wm.mark
FROM weekly_marks wm
INNER JOIN class_subject cs ON wm.class_subject_id = cs.id
INNER JOIN class c ON cs.class_id = c.id
INNER JOIN subject s ON cs.subject_id = s.id
A bit of a guess here, but assuming the class_subject table you're talking about is joining of subjects (students?) to the classes they are enrolled in.

Related

Find missing records in ONE-TO-MANY relationship query

I have 3 tables:
Class
Id
ClassName
guid
1A
guid
2A
Subject
Id
SubjectName
guid
Math
guid
Biography
SubjectOfEachClass
Id
ClassId
SubjectId
TeacherName
guid
guid
guid
James Bond
The way I wanted these tables to work is:
There will be 10 classes in table Class.
There will be 10 subjects in table Subject.
Each class will has 10 subjects and for 10 classes there will be 100 records.
I ran into some problems, I queried the SubjectOfEachClass table and there are only 95 records.
The query command I use to find the missing subjects is:
SELECT *
FROM Subject s
JOIN (
SELECT *
FROM SubjectOfEachClass
WHERE ClassId = 'guid'
) AS sc ON s.Id = sc.SubjectId
I replaced the ClassId several times until I found the class that misses some of the subjects.
I reckon this way of querying is not efficient at all. If I have 100 subjects and 100 classes, there will no chance that I will find the missing subjects.
to find every missing subject in all classes:
select c.id, c.classname , s.id , s.SubjectName
from class c
cross apply Subject s
where not exists (
select 1 from SubjectOfEachClass sc
where sc.classid = c.id and sc.subjectid = s.id
)
Try this:
SELECT c.id AS classId,
count(sc.id) AS countOfSubjects
FROM SubjectOfEachClass AS sc
INNER JOIN Classes AS c ON c.id = sc.classId
GROUP BY c.id
ORDER BY countOfSubjects
The abnormal values will be floated.
Your primary table should be SubjectOfEachClass, then those foreign tables Subject and Class will join your primary table.
select *
from SubjectOfEachClass sc
inner join Subject s on s.guid=sc.guid
inner join Class c on c.guid=sc.guid
where sc.ClassId = 'guid'

Getting data from unrelated tables

I've got this homework for database 'SCHOOL'
Table Student (StudentID, Name, Surname, etc..)\
Table Application (ApplicationID, StudentID, ClassID)\
Table Class (ClassID, ClassName, TeacherID)\
Table Teacher (TeacherID, Name, Surname, etc..)
There are some other tables and columns in the database but I don't think they're important to this query.
I need to get name of the teachers for whose classes no student signed up. Let's say there is no one signed up for Math class, and I need to get the name of the teacher for that Math class.
SELECT Teacher.Name, Teacher.Surname, Class.ClassName
FROM Teacher
INNER JOIN Class ON Class.TeacherID = Teacher.TeacherID
INNER JOIN Application ON Application.ClassID = Class.ClassID
INNER JOIN Student ON Student.StudentID = Application.StudentID
WHERE Application.PredmetID IS NULL
Help would be appreciated.
I would recommend NOT EXISTS:
select t.name
from class c join
teacher t
on t.teacherid = c.teacherid
where not exists (select 1
from application a
where a.classid = c.classid
);
I need to get name of the teachers for whose classes no student signed up
Select T.name
From Teacher T inner join [Class] C on T.TeacherId = C.TeacherId
Where C.ClassId not in (Select Distinct ClassId from Application)

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

Mapping table join query

I have three tables as follows,
student
category
student_category
Student table has the following columns:
studentid, student name, studenttype
Category table has the following tables:
categoryid, categoryname, ..
student_category table has the following columns:
id, studentid, categoryid
Now I have 2 input parameters categoryid and studenttype so now I need to get the all the student details which are associated with respective categoryid and whose student type is studenttype
I tried as follows which is not giving correct result,
SELECT
s.*
FROM
student s
JOIN
student_category sc ON sc.categoryid = 1;
Also I also need to filter student whose studenttype is 'someinput'
I am working on PostgreSQL. Any suggestions please
You should add a where clause and also use the appropiate join condition.
SELECT s.*
FROM student s
JOIN student_category sc ON sc.studentid = s.studentid
where s.studenttype = 'studenttype' --or your desired value
and sc.categoryid = 1

sql select based on column in another table

Ok I have two tables.
One table is called Persons and just has columns Pname and Age. (A person's name and their age).
Another table is called Giving and has donor, receiver, and giftname. (donor and receiver have foreign key constraints referencing persons.pname).
I need to find the names of all people who donated a gift to someone with a different age.
SELECT
Giving.donor
FROM Giving
INNER JOIN Persons AS donor ON Giving.donor=donor.Pname
INNER JOIN Persons AS receiver ON Giving.receiver=receiver.Pname
WHERE donor.Age<>receiver.Age
If you mean the donor age has to be different then the receiver age, then try this:
SELECT pd.pname
FROM Persons pd
INNER JOIN giving g
ON pd.pname = g.donor
INNER JOIN persons pr
ON pr.pname = g.receiver AND pr.age != pd.age