Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show correct result with SQL Joins

Tags:

sql

sql-server

Firstly, I am newbie to SQL (T-SQL), I would appreciate guidance with this. I have 3 tables with values created as below.

CREATE Table StudentProject
(ID int identity (1,1) PRIMARY KEY,
ProjectName Varchar (30),
DueDate Date)


CREATE Table StudentName 
(ID int identity (1,1) PRIMARY KEY,
StudentName Varchar (30))

CREATE Table StudentWork
(ID int identity (1,1) PRIMARY KEY,
ProjectID int,
StudentID int)

Insert Into StudentProject values 
('Omega','1/2/2005'),('KingOmega','1/3/2000'),('Beast','1/6/2007'), 
('DeltaMovie','3/7/2008')

Insert into StudentName values
('Roger'),('John'),('James'),('Juliet'),('William')

Insert into StudentWork values
(1,1),(1,2),(2,2),(2,3),(3,3),(3,4),(1,3)

The goal is to produce the below outcome but seems that i cant or i'm sure i'm doing something wrong.

SQL_Outcom

SQL_Outcome

Please help.

like image 610
Jon_McClusky Avatar asked Mar 27 '18 00:03

Jon_McClusky


People also ask

How do I display SQL results?

You have the option of displaying your query results on the Run SQL window, as opposed to Data Display windows. To do this, go to View > Data Grid (Ctrl+G). Once you have selected this option, a panel will appear at the bottom of the window - your query results will be displayed there.

When using a right join what will be the result in the output?

The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records from the left table (table1). The result is 0 records from the left side, if there is no match.

Which join return rows that have matching values?

What type of join is needed when you wish to return rows that do have matching values? Explanation: Outer join returns the row having matching as well as non matching values.

What does (+) mean in JOINs in SQL?

The plus sign is Oracle syntax for an outer join. There isn't a minus operator for joins. An outer join means return all rows from one table. Also return the rows from the outer joined where there's a match on the join key. If there's no matching row, return null.


3 Answers

SELECT StudentProject.ProjectName, StudentName.StudentName
FROM StudentWork 
INNER JOIN StudentProject ON StudentProject.ID = StudentWork.ProjectID
INNER JOIN StudentName ON StudentName.ID = StudentWork.StudentID

You have 3 tables, try to recognize the "master table", then join them to other tables, after joining them you will have access to their columns.

Hello World :)

UPDATE:

In order to confirm Roger always with id 1 and all the other students in StudentName table, you need to use SET IDENTITY_INSERT which guarantee you the ordering of rows.

Instead of :

Insert into StudentName values
('Roger'),('John'),('James'),('Juliet'),('William')

Do this:

SET IDENTITY_INSERT StudentName ON

Insert into StudentName values
(1,'Roger'),(2,'John'),(3,'James'),(4,'Juliet'),(5,'William')

SET IDENTITY_INSERT StudentName OFF
like image 94
SuicideSheep Avatar answered Sep 22 '22 07:09

SuicideSheep


You need to use inner join and in order to match the records related to each others:

Select  p.ProjectName, s.StudentName  from StudentName  s
    Inner join Studentwork sw on sw.studentid = s.Id
    inner join StudentProject p on p.ID= sw.ProjectId
    Order by P.ProjectName desc
like image 35
Hussein Salman Avatar answered Sep 19 '22 07:09

Hussein Salman


You need inner join

Try this :

select s.StudentName,p.ProjectName from StudentName s
inner join StudentProject sp on sp.id= sw.ProjectId
inner join StudentWork sw on sw.studentid = s.id;
like image 37
stackFan Avatar answered Sep 22 '22 07:09

stackFan