Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL correct way of joining if the other parameter is null

Tags:

sql

sql-server

I have this code and its temporary tables so you can run it.

create table #student (     id int identity(1,1),     firstname varchar(50),     lastname varchar(50) )  create table #quiz (     id int identity(1,1),     quiz_name varchar(50) )  create table #quiz_details (     id int identity(1,1),     quiz_id int,     student_id int )  insert into #student(firstname, lastname) values ('LeBron', 'James'), ('Stephen', 'Curry')  insert into #quiz(quiz_name) values('NBA 50 Greatest Player Quiz'), ('NBA Top 10 3 point shooters')  insert into #quiz_details(quiz_id, student_id) values (1, 2), (2, 1)   drop table #student drop table #quiz drop table #quiz_details 

So as you can see lebron james takes the quiz nba top 10 3 point shooters quiz and stephen curry takes the nba 50 greatest player quiz.

All I want is to get the thing that they didn't take yet for example LeBron hasn't taken the 50 greatest player quiz so what I want is like this.

id   quiz_name                    firstname  lastname ---------------------------------------------------- 1    NBA 50 Greatest Player Quiz  NULL       NULL  

I want 2 parameters, the id of lebron and the id of the quiz so that I will know that lebron or stephen hasn't taken it yet, but how would I do that if the value of the student_id is still null?

My attempt:

select     QD.id,     Q.quiz_name,     S.firstname,     S.lastname from      #quiz_details QD inner join      #quiz Q on Q.id = QD.quiz_id inner join      #student S on S.id = QD.student_id 
like image 621
SCrub Avatar asked Jan 10 '18 14:01

SCrub


People also ask

How do you handle NULL values in SQL joins?

Because null values represent unknown or inapplicable values, Transact-SQL has no basis to match one unknown value to another. You can detect the presence of null values in a column from one of the tables being joined only by using an outer join.

How do you inner join with NULL values?

A join that displays only the rows that have a match in both joined tables. Columns containing NULL do not match any values when you are creating an inner join and are therefore excluded from the result set. Null values do not match other null values.

Does join work on NULL values?

As we have seen from the above examples joining NULL values does not work. Even though you have two NULL values SQL Server does not treat these as the same value. Internally a value of NULL is an unknown value and therefore SQL Server does not equate an unknown value being equal to another unknown value.

Which join includes NULL?

In SQL Full Outer Join, all rows from both the tables are included. If there are any unmatched rows, it shows NULL values for them.


2 Answers

This should get you started:

-- filter out the student and quiz you want DECLARE @qid INT = 1 DECLARE @sid INT = 1  SELECT *  FROM #student AS s INNER JOIN #quiz AS q  -- you want the quiz  ON 1=1 LEFT OUTER JOIN #quiz_details AS qd  -- left join here to get result where rows not found  ON qd.id = q.id   AND qd.student_id=s.id WHERE s.id = @sid  AND q.id = @qid  AND qd.id IS NULL -- only return quizes not taken 
like image 66
SAS Avatar answered Sep 24 '22 14:09

SAS


Pretty sure you want something along these lines. This will give you the quiz values and return NULL for the student and quiz_details when there is no matching data.

select * from #quiz q left join #quiz_details qd on q.id = qd.quiz_id left join #student s on s.id = qd.student_id 
like image 41
Sean Lange Avatar answered Sep 24 '22 14:09

Sean Lange