Note: This question is part of a series of questions that use the same or similar answer choices. An
answer choice may be correct for more than one question in the series. Each question is independent
of the other questions in this series. Information and details provided in a question apply only to that
question.
You have a database that contains tables named Customer_CRMSystem and Customer_HRSystem. Both
tables use the following structure:
The tables include the following records:
Customer_CRMSystem
Customer_HRSystem
Records that contain null values for CustomerCode can be uniquely identified by CustomerName.
You need to display customers who appear in both tables and have a proper CustomerCode.
Which Transact-SQL statement should you run?
Explanation:
When there are null values in the columns of the tables being joined, the null values do not match each other.
The presence of null values in a column from one of the tables being joined can be returned only by using an
outer join (unless the WHERE clause excludes null values).
https://technet.microsoft.com/en-us/library/ms190409(v=sql.105).aspx
A
25
2
Wrong answer, you cannot compare NULL in a join: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/19175715-a005-48dd-a1ac-a09cb8bee463/joining-2-tables-on-a-column-which-is-having-null-values-and-expecting-that-nullnull-has-to-be?forum=transactsql
To test:
create table blaA (customerID int not null, customercode char(4) null, customername varchar(50) not null)
create table blaB (customerID int not null, customercode char(4) null, customername varchar(50) not null)
insert into blaA (customerid, customername) values (4, ‘jane’)
insert into blaB(customerid, customername) values (4, ‘jane’)
insert into blaA (customerid, customercode, customername) values (1, ‘cus1’, ‘do’)
insert into blaB(customerid, customercode, customername) values (1, ‘cus1’, ‘do’)
select a.customerid, b.customerid, a.customercode, b.customercode, a.customername, b.customername
from blaA A
inner join blaB b on a.customercode = b.customercode and a.customername = b.customername
This will do the trick:
select a.customerid, b.customerid, a.customercode, b.customercode, a.customername, b.customername
from blaA A
inner join blaB b on a.customername = b.customername
where (a.customercode is not null) or (a.customercode is not null)
3
15
When comparing NULLs in joins (excluding OUTER JOIN), yes, there will be no match and the rows will be discarded. However, this happens to fulfill the requirement for returning customers that ‘have a proper CustomerCode’ because the customers with NULL customercodes on both sides will be excluded in the INNER JOIN.
2
0
B is the correct answer. Try this:
DROP TABLE IF EXISTS dbo.CustomerCrm, dbo.CustomerHr
go
create table CustomerCrm
(
CustomerId int not null,
CustomerCode char(4) null,
CustomerName varchar(50) not null
)
create table CustomerHr
(
CustomerId int not null,
CustomerCode char(4) null,
CustomerName varchar(50) not null
)
insert into CustomerCrm VALUES (1,’CUS1′, ‘Roya’)
insert into CustomerCrm VALUES (2,’CUS9′, ‘Almudena’)
insert into CustomerCrm VALUES (3,’CUS4′, ‘Jack’)
insert into CustomerCrm VALUES (4,null, ‘Jane’)
insert into CustomerCrm VALUES (5,null, ‘Francisco’)
insert into CustomerHr VALUES (1,’CUS1′, ‘Roya’)
insert into CustomerHr VALUES (2,’CUS2′, ‘Jose’)
insert into CustomerHr VALUES (3,’CUS9′, ‘Almudena’)
insert into CustomerHr VALUES (4,null, ‘Jane’)
— A (Wrong answer)
SELECT c.CustomerCode, c.CustomerName, h.CustomerCode, h.CustomerName
FROM CustomerCrm c
INNER JOIN CustomerHr h
ON c.CustomerCode = h.CustomerCode AND c.CustomerName = h.CustomerName
— B
SELECT CustomerCode, CustomerName
FROM CustomerCrm
INTERSECT
SELECT CustomerCode, CustomerName
FROM CustomerHr
/*
select * from CustomerCrm
select * from CustomerHr
*/
1
8
Sorry, I confirm that A is the correct answer. Proper CustomerCode means that is is not null.
6
0
A y B tienen el mismo resultado
select c.customercode,c.customername, h.customercode,h.customername from Customer_CRMSystem
as c inner join Customer_HRSystem as h
on c.customercode=h.customercode and c.customername=h.customername
customercode customername customercode customername
1 Roya 1 Roya
4 Jane 4 Jane
select customercode, customername from [dbo].[Customer_CRMSystem]
intersect
select customercode, customername from [dbo].[Customer_HRSystem]
customercode customername
1 Roya
4 Jane
0
0