Student and Subjects in a query - sql

Can anyone help me how to possibly do this query:
I have 2 tables namely Students and Subjects, and what I want to do is get all the students with more than one subjects in the Subject table. Also I want to show the count of subjects that each student has.
Thanks you!

use join between Students and Subjects tables ,and for count aggregate function , below query can be your solution
select st.id,st.name,count(sb.subjectid) as numberofTakenSubject
from Students st inner join Subjects sb
on st.id=sb.student_id
group by st.id,st.name
having count(*)>1
BTW your question should be more clear and specific like
Students and Subject tables structure and sample data

Try this:
select studentid,count(subjectid) from students inner join subjects
on student.id=subjects.studentid
group by studentid
having count(subjectid)>1

Related

How to display a list of departments whose teachers teach DBMS department students?

I have a database in Access. The database has 4 tables:
[DEPARTMENT], [TEACHER], [LECTURE], [SGROUP].
I need to display a list of departments whose teachers teach DBMS department students.
The question is: how to do it? I think it should be a query to join multiple times on the same table ... but I don't understand which table to join ...
I'm a noob.
First, you need to add DepNo value which 3, so the concatenation logically works
INNER JOIN displays everything you want about;
departments whose teachers teach DBMS department students
SELECT DEPARTMENT.DepNo, TEACHER.TechNo, TEACHER.name, DEPARTMENT.name, TEACHER.post
FROM DEPARTMENT
INNER JOIN TEACHER ON DEPARTMENT.DepNo=TEACHER.DepNo
ORDER BY TEACHER.name;
Image chart for What's the INNER JOIN:
Reference:
SQL Joins

Join a SQL resultset based on common email address

I have one table that contains both students and parents records. There is a common field (email address) in both parent and student records that match the records (parents and students have the same email address).
I want my query to find the common email addresses and return each
student record but it must also include a field (Mifare) from the
parents record.
So I have tried to join and where clauses but i am getting crazy results. I was also thinking of using the with clause.
Expected result is
The basic script is:
Select extid, first_name, last_name, commonemail, designation,mifare
from students
Please assist with a basic coding - no procedures etc...just simple help for a newbie!
Thank you all!
Here is the solution for your problem:
SELECT stud.ExtId,
stud.FirstName,
stud.LastName,
stud.CommonEmail,
stud.Designation,
stud.Mifare,
prnt.Mifare
FROM Students AS stud
INNER JOIN Students AS prnt
ON stud.CommonEmail = prnt.CommonEmail
AND stud.Designation = 'Student'
AND prnt.Designation = 'Parent'
You can also follow the link to the demo:
http://sqlfiddle.com/#!9/5b790b/1
Select s.*
p.mifare
from students s
left join students p on p.commonemail=s.commonemail and p.Designation='Parent'
Try using the following Sub query
Select *,
(Select Mifare from students b where b.Designation = 'Parent' and b.commonemail = a.commonemail) as ParentMifare
from students a
I am not sure you can try this way join both the tables with email select only the designation student only.
Select
st.extid,
st.first_name,
st.last_name,
st.commonemail,
st.designation,
st. mifare
pr.parentmifare
from students st inner join students pr on st.commonemail=pr.commonemail
where pr.designation='student' and st.designation='parent'

SQLite "appear the amount of courses for every student whether he is in Attends or not" sql query

I have an ER Diagram as shown below
for every student I want to appear all courses that addends.
So I use query
select studentId,course.courseCode
from student natural left outer join attends
natural left outer join course
which gives me all results in right way
now I want to appear the total amount of courses that a student attends
and I am using this query
select studentId,
(select count(attends.courseCode)
from attends natural left outer join student
)as 'amount'
from student
but I am having this result
How am I supposed to appear the real amount of courses for every student whether he is in Attends or not? That is, a 0 for studentId 6,7,8 and a 2 for studentId 17 etc.
Thank you in advance
PS1: If you want more of my tables, please let me know.
PS2: I was not sure about the title. If you find that another title fits better, please suggest
First, don't use natural join. It is entirely dependent on the data structure -- and if that changes, then the semantics of the query change too. In other words, you cannot read a query and really understand what it is doing.
Then, for this query, first generate a list of all students and courses using cross join, then bring in the attendance information:
select s.studentId, c.courseCode, count(a.CourseCode)
from student s cross join
course c left join
attends a
on s.studentId = a.studentId and s.courseCode = c.courseCode
group by s.studentId, c.courseCode;

SQL finding course name from course code

I have 2 tables, students and courses. In the students table each student has a course code and in the courses table I have course codes with their names.
How can I return the course name when running the query?
So for example if I run a basic SELECT * FROM Students it says "Jimmy | Computing" instead of "Jimmy | 12390814"
JOIN both tables. Try something like this,
SELECT a.*, b.courseName
FROM students a
INNER JOIN courses b
ON a.courseCode = b.courseCode
-- WHERE ....

SQL Server query to know if an id in a column equals to other in another column

I’ve been hours trying to build this query and I need your help so I can make it.
This is table Students (made out of inner joins):
SpecialtyChosenID StudentID Subject SubjectSpecialtyID
5ABFB416-8137 15 Math A1EBF3CB-E899
5ABFB416-8137 15 English A1EBF3CB-E899
The info in it means that a student with id no. 15 has chosen an specialty with id 5ABFB416-8137
The two subjects he has passed (Math and English) belong to a specialty with id A1EBF3CB-E899
What would be the query to know if the passed subjects belong to the specialty chosen by the student??
Counting the number of subjects with the same SubjectSpecialtyID as SpecialtyChosenID and vice versa could do.
Thanks a lot
You can do a self join. This finds the number of subjects taken by the student that match the student's chosen specialities.
SELECT l.SpecialtyChosenID, l.StudentID, Count(Distinct r.Subject) FROM Students l
LEFT JOIN Students r ON (l.StudentID=r.StudentID AND l.SpecialityChosenID=r.SubjectSpecialityID)
GROUP BY l.SpecialtyChosenID, l.StudentID
However, this is quite inefficient using the table structure given. If you have a table listing students with their specialities, and another with subjects and specialities, and a third relating students with subjects, it would be better to build this query from the base data, rather than from the derived data.
SELECT * FROM Students WHERE SpecialtyChosenID = SubjectSpecialtyID
If you only need the list of matching subjects and you have the SpecialtyChosenID you can do something like
SELECT * FROM Students
WHERE SubjectSpecialtyID = SpecialityChosenID
CASE WHEN SpecialtyChosenID = SubjectSpecialtyID THEN 1 ELSE 0 END AS specialty