How to show two columns of names? - sql

SQL question
I have a table with Students (ID and Name)
I have another Table with Friends (ID1 and ID2)
create table Students(ID int, name text);
create table Friends(ID1 int, ID2 int);
If I enter:
SELECT Friends.ID1, Friends.ID2 FROM Friends
I get:
http://image.prntscr.com/image/d6e599ee5bd74c13822339afeb4c05ab.png
I think I need to use:
SELECT Strudets.Name
from Friends, Strudents
Where Strudents.ID=Friends.ID
And I want to show two columns of names
I need to look for the Friends.ID1 in the Students Table and get the Strudents.name, then do the same for Friends.ID2 and then show both names
EDIT with more info
When I enter this:
SELECT id1, name
FROM friends
INNER JOIN students
ON friends.id1 = students.id
I get this:
Lista de una sola columna
And I need to show two columns of names
ANSWER FROM #kbbal
SELECT s1.NAME, s2.NAME
FROM FRIENDS
INNER JOIN STUDENT s1 ON FRIENDS.ID1 = s1.ID
INNER JOIN STUDENT s2 ON FRIENDS.ID2 = s2.ID
I love this website!! <3

You seem new so I will lay out an approach.
Join the friends table to the students table. You should do this for the first friend first, so join the tables on the common ids. This would be the id column in the students table and id1 in the friends table.
Join the students table again. This time perform the join on id2 of the friends table
Specify that you want id1, name, id2, name in the SELECT statement
Your final answer should give you the ids of both friends and the names corresponding to their ids in the students table.
Edit based on comments:
Here is how you would do the joins:
SELECT friends.id1, s1.name, friends.id2, s2.name
FROM friends
INNER JOIN students s1
ON friends.id1 = s1.id
INNER JOIN students s2
ON friends id2 = s2.id
The trick is to actually join the students table twice, once for each friend. And then you just have to create aliases (s1 and s2) so you can distinguish between the two instances.

Related

Create a additional row from join sql?

I have 2 tables: Person and House with 1-n relation.
I want to the result return as picture below:
Row always have a Person column with a null House column.
Thanks.
you can use unionall to join the result set with person table something like
select p.name,h.name as housename from person p join house h on p.id=h.personid
union all (select name,null from person)
order by name,housename

Native Query to Select records to only contain specific ids

I am in the process of implementing an advanced search in my application. I'm sort of new with Oracle and JPA. The database structure has a many-to-many relationship (ERD) with an intermediate table that contains StudentID and CourseID.
I'm trying to return rows of students that have the list of classes/courses.
Basically, I want the results of
SELECT DISTINCT s.* FROM STUDENT s
INNER JOIN STUDENT_COURSE sc ON s.StudentID = sc.StudentID
INNER JOIN COURSE c ON c.CourseID = sc.CourseID
WHERE ( c.CourseID IN (SELECT DISTINCT CourseID FROM STUDENT_COURSE WHERE CourseID = 'A01') AND c.CourseID IN (SELECT DISTINCT CourseID FROM STUDENT_COURSE WHERE CourseID = 'A02'));
which returns the records of students that have both courses 'A01' and 'A02'.
ID
Age
Grade
1
...
...
4
...
....
9
...
....
with courses
Courses
A01,A02
A01,A02,A03,X02
A01, A02, A03
The goal is to get a similar result using Spring Data JPA. And make it more general to select any number of Course Ids.
I've tried
#Query(value="SELECT DISTINCT s.* FROM STUDENT s "
+"INNER JOIN STUDENT_COURSE sc ON s.StudentID = sc.StudentID "
+"INNER JOIN COURSE c ON c.CourseID = sc.CourseID "
+"WHERE ( s.STUDENTID IN (SELECT DISTINCT STUDENTID FROM STUDENT_COURSE WHERE CourseID =:courseIds)",nativeQuery=true))
public List<Student> advancedSearch(#Param("courseIds") String courseIds);
One example, The courseIds field contain "A01,A02". The result would be empty.
I've looked at examples where people use IN. When I tried it, it would return records of the student have courses AO1 OR A02 OR Both.
DON'T WANT THIS
ID
Age
Grade
2
...
...
3
...
....
8
...
....
With Courses
CourseID
A01,A03
A01, X01
A02
I want records of students that have both A01 AND A02 as shown in the other table.
My guess is that the query you want is
select s.name /* note this column doesn't appear in your ERD */
,s.age
,s.grade
from student s
join student_course sc
on s.studentID = sc.studentID
where sc.courseID IN ('A01', 'A02')
group by s.name /* again, this column isn't in your ERD */
,s.age
,s.grade
having count(sc.courseID) = 2
That doesn't produce the output table you say you want. But neither does the query you posted. This will return all the students that have taken both courses (you could add a distinct to the having clause if the data model were to change to allow a student to take a course multiple times). It won't give you the list of all courses that student has taken as a comma-separated list. But the query you posted doesn't return any course information either.
If this isn't what you are looking for, it would be really helpful to update your question with
The DDL for your schema in text form (so we can run it) rather than as an image
The DML to insert whatever sample data you want
The actual results you'd want the query to return (since there is a conflict between what the query you posted returns, the table of data you say you want, and the descriptive text of what you are trying to accomplish, it is confusing to try to figure out which is right).

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

SQL query - list of items in one table not in another

I need some help with a SQL query. I have a table of courses and a table that contains user id and course id, denoting courses that user has taken (might not have taken any; no entry in that table for that user id).
I need a query to return the list of courses not taken.
Course Category table
CategoryID
Caegory
Courses table
CourseID
CategoryID
CourseName
...
UserCourse table
UserID
CourseID
you can use not exists
Select *
From Courses c
Where Not Exists (Select 1 From UserCourse uc Where uc.CourseID = c.CourseID)
This will just list the course
select *
from Courses C
Left join CourseCategory cc on
cc.CategoryID = c.CategoryID
where CourseID not in (Select CourseID from UserCourse where UserID = 14)
what i need is for a given user id and course category, what courses within that category have not been taken by this user
(This should have been in the request by the way.)
So:
Select from courses.
Limit to the desired category.
Limit to courses not in the set of courses taken by the user.
The query:
select *
from courses
where categoryid = 123
and courseid not in (select courseid from usercourse where userid = 456);
Another way of writing same query, which will perform faster.
select C.CourseID,C.CategoryID
from Courses C
Left join CourseCategory cc on
cc.CategoryID = c.CategoryID
left join UserCourse uc
on C.CourseID=uc.CourseID
where uc.CourseID is null

SQL: Join two different rows from the same table to a relation

Let's say I've got a table called relation with these fields:
id, type, person1.id, person2.id
Then there's the table with the people called person with this field:
id, name
Now I want to create a list that contains the following information:
relation.id, relation.type, person1.id, person1.name, person2.id, person2.name
How can I achieve this with an SQL statement? I know that this is probably a pretty basic question. :-(
You need to use the person table twice:
select rel.id, rel.type, p1.id, p1.name, p2.id, p2.name
from relation rel
, person p1
, person p2
where rel.person1 = p1.id
and rel.person2 = p2.id
Take a look at this
select relation.type,a.Person1_name,b.person2.name from relation inner join
(select relation.type, person1.name Person1_name from relation inner join person
on relation.person1=person.name
where person1 is not null)a
on relation.type=a.type
inner join
(
select relation.type, person2.name,from relation inner join person
on relation.person2=person.name
where person2 is not null)b
on relation.type=a.type
pls try this code