SQL finding course name from course code - sql

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 ....

Related

Student and Subjects in a query

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

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'

SQL Creating an Alias Column For a Nested Select?

Say I have a Person table and a Courses table. In the Person table I have the column PersonName. In the Courses table, let's say I have CourseTitle,PersonName and CourseDifficulty. CourseDifficulty is 1-4 (4 being the hardest). How do I return a list of people from Person and for each person have a column that shows the most difficult class they're taking by CourseTitle.
As far as I know, I'd get the CourseTitle of the most difficult class Brett is taking by doing the following:
SELECT CourseTitle
FROM Courses
WHERE PersonName = 'Brett'
AND CourseDifficulty = (SELECT MAX(CourseDifficulty)
FROM Courses
WHERE PersonName='Brett')
But how do I run that for each person in the Person table? I want the results to be something like
Brett-SQL For Dummies 4
Tim-Quantum Mechanics
Jane-Thermodynamics 2
Sorry for the noobness. Thanks in advance for the help!
you can use the following
SELECT p.name ,p.address, c.courseTitle ,c.courseDifficulty FROM (
SELECT personName, courseTitle, MAX(courseDifficulty) AS courseDifficulty
FROM course
GROUP BY personName
) AS c RIGHT JOIN person AS p ON p.name = c.personName
here am assuming personName is a unique. Otherwise you can use unique id over here instead on person name and add this field in select statement.
Try this:
SELECT c.CourseTitle, c.PersonName, c.CourseDifficulty
FROM Courses c
WHERE c.CourseDifficulty=(SELECT MAX(c2.CourseDifficulty) FROM Courses c2 WHERE c2.PersonName=c.PersonName)

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

SQL question: Show me students who have NOT taken a certain course?

So I have these tables:
STUDENTS:
Student ID - First name - Last name - Email
COURSES:
Catalog ID - Course Name - Description
TERMS:
Term ID - Start Date - End Date
COURSEINSTANCES:
CourseInstance ID - Catalog ID - Term ID
STUDENTCOURSES:
StudentCourse ID - CourseInstance ID - Student ID - Date added to database
This makes it easy to see which students have taken which courses. I'm not sure how to go about finding out which students have NOT taken a particular course.
Doing something like this:
WHERE ((CourseInstances.CatalogLookup)<>504)
will just give me a list of courses taken by students that do not equal catalog number 504 like this:
Tara - 501
Tara - 502
Tara - 505
John - 503
So for example I've taken 504. Therefore I do not want me to show up on this list. The SQL above will just show all of my courses that are not 504, but it will not exclude me from the list.
Any ideas? Is this possible?
I prefer this syntax over outer joins, IMO it's easier to read:
select *
from STUDENTS
where StudentID not in
(
select StudentID
from STUDENTCOURSES s
inner join COURSEINSTANCES c on s.CourseInstanceID = c.CourseInstanceID
where c.CatalogID = 504
)
In the nested query, you select the StudentIDs of all students who HAVE taken course 504.
Then, you select all the students whose StudentIDs are not included in the nested query.
EDIT:
As ChrisJ already said, the c and the s are aliases for the table names.
Without them, the query would look like this:
select *
from STUDENTS
where StudentID not in
(
select StudentID
from STUDENTCOURSES
inner join COURSEINSTANCES on STUDENTCOURSES.CourseInstanceID = COURSEINSTANCES.CourseInstanceID
where CatalogID = 504
)
I always use aliases because:
a) I'm too lazy to type the table names more often than necessary.
b) In my opinion it's easier to read, especially when you join tables with long names.
Try something like this:
SELECT *
FROM Users
WHERE UserID NOT IN
( SELECT UserID
FROM
Users
INNER JOIN
ClassesTaken ON Users.UserID = ClassesTaken.UserID AND ClassesTaken.ClassNumber = 504)
Another way occurred to me the other day:
SELECT *
FROM
Users
LEFT OUTER JOIN ClassesTaken ON Users.UserID = ClassesTaken.UserID AND ClassesTaken.ClassNumber = 504
WHERE ClassesTaken.UserID IS NULL
You should read about outer joins.
SELECT * FROM students
WHERE studentId not in
(SELECT distinct studentID FROM studentCourses WHERE courseInstanceID = 504)
Three main ways in Access
NOT IN (Be careful to exclude any NULLs if there is any possibility of them appearing in the sub query)
OUTER JOIN and filter on NULL (may need DISTINCT added)
NOT EXISTS
Other RDBMSs also have EXCEPT or MINUS
homework? use set operators.
select all students MINUS select any student who has taken this course...