PostgreSQL. how to make a query that shows pairs from the same column? - sql

I have 3 tables
Students{sId, name}
Class{cId, cName} and
Enrollment{sId, cId, semester}
A student can enroll in multiple classes. I need to produce a query that displays all the pairs of students (their names and ids )who have completed one or more of the same classes in the same semester. I'm not really sure what I am to SELECT to display because I need to display in a format that shows the pairs
I started by doing the joins and came up with this
FROM Students s
INNER JOIN Enrollment e
ON s.sId = e.sId
INNER JOIN Class c
ON c.cId = e.sId
Then I am not sure what to do.. If someone could point me in the right direction it would be much appreciated. I am very new to SQL and am even struggling to find some keywords to google.
Thanks for any help

Related

List the names of the course(s) student Altvater took in semester I-2008

Im stuck on a question "List the names of the course(s) student Altvater took in semester I-2008" If anyone can guide me to the right direction I would appreciate it. I assume i would use some sort of join. Please refer to this diagram http://www.csc.villanova.edu/~mdamian/Past/databasefa13/notes/ch07-inclassex.pdf
I understand that I will below parts in my query
Where StudentName = 'Altvater' and
Where Semester = ‘I-2008’
Column names are consistent throughout the schema, allowing natural joins:
SELECT DISTINCT CourseName
FROM STUDENT
NATURAL JOIN REGISTRATION
NATURAL JOIN SECTION
NATURAL JOIN COURSE
WHERE StudentName = 'Altvater'
AND Semester = 'I-2008';
Try following SQL:
SELECT COURSENAME
FROM STUDENT S
INNER JOIN REGISTRATION R
ON S.STUDENTID=R.STUDENTID
INNER JOIN SECTION SE
ON R.SECTOINNO=SE.SECTOINNO
AND R.SEMESTER=SE.SEMESTER
INNER JOIN COURSE C
ON SE.COURSEID=C.COURSEID
WHERE S.STUDENTID=54907/* Altvater */
AND R.SEMESTER='I-2008'
You just need to understand basic design of tables to write SQL. SQL is very easy language. Let me know if you have any doubt in this query.

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;

I am unable to return all values using joins in my sql query, what am I doing wrong?

I have three tables - Assignment, Grades, Student and I am trying to make this query work so that it returns all assignments even if there is no grade entered for a student yet.
The tables are setup like this
Assignment
AssignmentId, AssignmentName, PointsPossible
Grades (Junction table)
StudentId, AssignmentId, PointsReceived
Student
StudentId, StudentName
My query:
select
s.StudentName, a.AssignmentName, g.PointsReceived, a.PointsPossible
from
student s
cross join
assignment a
left outer join
grades g on s.StudentId = g.StudentId and g.AssignmentId = a.AssignmentId
order by
s.StudentName;
When I run the query I get all the names I need, but I don't get all the assignments back. I should be getting all the names, all the assignments, and if the assignment hasn't been graded yet, there should be a null value returned.
I need a little direction, maybe my tables are setup incorrectly.
You need to get all assignments even if there isn't a grade? The obvious question is: without a junction table, how do you know which assignments to provide for each student?
So, let me guess that you want to get a cross product of all students and all assignments, along with grades (if any). If so, you want to structure your query like this:
select s.StudentName, a.AssignmentName, a.PointsPossible, g.PointsReceived
from students s cross join
assignments a left outer join
grades g
on g.StudentId = a.StudentId and g.AssignmentId = a.AssignmentId;
order by s.StudentName;

Multiple Table SQL Statement

I'm struggling with how to structure an SQL statement involving multiple tables and was wondering if anyone could help point me in the right direction.
I assume multiple JOINS will be needed, but I'm not entirely sure of how exactly they work.
Basically the system I need to create will allow teaching staff to view an HTML table of the assignment submissions with relevant details. Below is the ER diagram representing the database.
Each row in the HTML table will relate to a particular assignment submission, and will include the following information - Forename of the student, Surname of the student, Module the assignment relates to, Assignment Name, a link to the feedback PDF document, the time the feedback was collected and the number of times the feedback was commented on.
So basically I need an SQL statement that would return the information mentioned above.
I probably haven't explained this too well, but hopefully the diagram can help fill in the parts I have missed.
SELECT
Forename,
Surname,
ModuleName,
sa.StudentID as StudentID,
sa.AssignmentID as AssignmentID,
FeedbackURL,
FirstViewed,
LastViewed,
ViewCount,
a.Name AS AssignmentName,
FROM assignment AS a
INNER JOIN student_assignment AS sa ON a.AssignmentID=sa.AssignmentID
INNER JOIN student AS s ON sa.StudentID=s.StudentID
INNER JOIN course AS c ON s.CourseID=c.CourseID
INNER JOIN course_module AS cm ON c.CourseID=cm.CourseID
INNER JOIN module AS m ON cm.CRN=m.CRN

Query database not using stored procedure

i have a db diagram as shown above.
What I need to do is:
Select a table which is as follows:
PatientId, Emergency.Name or Doctor Names, Patient.Address.
Explanation; I need A query which will return patient details and, if Patient has an emergency also add an emergency, if not select all doctor.Name into one row.
So, example:
So, the first row was built because EmergencyId in table patient was null, while the second row had an emergency Id.
I need the query to simulate this. Using SSRS
Thanks a lot!
Thanks guys, can you at least explain
me how to return this data in separate
rows, so I can union later on?
I believe this will return the data you want broken out into separate rows, returning Emergency if it exists and the Drs if it does not. Good luck!
SELECT Distinct Coalesce(E.Name, D.Name) as VariableColumn,
P.PatientId,
P.Address
FROM Patient P
LEFT JOIN Emergency E
ON P.EmergencyId=E.EmergencyId
LEFT JOIN PatientDoctor PD
ON P.PatientID=PD.PatientId
LEFT JOIN Doctor D
ON PD.DoctorId=D.DoctorId
I can't really debug or work with your tables to check this, but here is a rough stab at something that should get you going:
SELECT P.PatientId, E.Name as 'EmergencyName', D.Name as 'DoctorName', P.Address
FROM Patient P
LEFT JOIN Emergency E ON P.EmergencyId=E.EmergencyId
LEFT JOIN PatientDoctor PD ON P.PatientID=PD.PatientId
LEFT JOIN Doctor D ON PD.DoctorId=D.DoctorId
ORDER BY P.PatientId, E.Name, D.Name, P.Address
Also, I'm no SQL Wizard. This should get the ball rolling, though.
Can you test this query against your data?
Does it return the desired results?
EDIT:
I just saw your comment about needing this layout with multiple values in one cell aligned vertically. That would require sub queries, and would be too much for me to write out.