Full outer join not including results from the second table? - sql

I have 2 employee tables and trying to look up a user from both of them. For example:
Employees1
username full_name
fbar Foo Bar
Employees2
email full_name
sname#test.com Some Name
I'm trying to do something like this:
DECLARE #testuser VARCHAR(100)
SET #testuser = 'sname'
SELECT isnull(a.full_name, b.full_name) as full_name
FROM Employees1 as a
FULL OUTER JOIN Employees2 as b ON b.email LIKE #testuser + '%'
WHERE a.username = #testuser
If I run this then it comes out blank. But if I run the following then it shows the correct name:
SELECT full_name FROM Employees2 WHERE email LIKE #testuser + '%'
Is there a reason the full outer join isn't showing a result but the second select statement is?

If you have limiting conditions on individual tables that are separate from the outer join condition, you will need to put them in a subselect for them to work as expected.
I believe what you want is something like:
DECLARE #testuser VARCHAR(100) = 'sname'
SELECT isnull(a.full_name, b.full_name) as full_name
FROM (
SELECT *
FROM Employees1
WHERE username = #testuser
) as a
FULL OUTER JOIN (
SELECT *
FROM Employees2
WHERE email LIKE #testuser + '%'
) as b
ON b.full_name = a.full_name
Or perhaps you may just need a UNION:
SELECT full_name
FROM Employees1
WHERE username = #testuser
UNION
SELECT full_name
FROM Employees2
WHERE email LIKE #testuser + '%'
I added the full_name join condition, as I believe that is what you intended.
See this db<>fiddle.

Related

T-SQL inner query like join based off IN results

I have a T-SQL query that has a subquery listing 2 names:
Coop
Bro
Code:
Select LastName
From Managers
Where Type = prefix;
I need to have the outer query use this above sub-query in something like an 'IN' statement, but it's not an exact match, but rather a BEGINS WITH. Something like the following with + '%':
Select *
From Employees
Where LastName In (Select LastName + '%'
From Managers
Where Type = prefix)
Desired query would return back outer query results such as:
Cooper
Coopersmith
Coopmoth
Brown
Brownly
Bronnan
...but, this is not working. I get 0 results.
You can use exists. Presumably, you intend something like this:
Select e.*
from Employees e
where exists (select 1
from managers m
where type = prefix and
e.LastName like m.LastName + '%'
)
Got it with similar to:
select * from employees e
inner join Managers m on e.lastname like m.lastname + '%' and m.type = prefix

If null in one table select from Other

I have 2 tables inquiry and Application table I want to build a view that contains the user information based on data from these two tables.
Example
I Have Table Inquiry
FirstName, LastName, Address, email
I have table Application
FirstName, LastName, Address, email
Am querying the tables using the email field, This is what i want,
SELECT FirstName From InquiryTBL where email = #Email
If Null Select FirstName From ApplictionTBL where email = #email
This is kinda what I have been trying
SELECT
CASE
WHEN a.Email = null
THEN (SELECT FirstName from dbo.Inquiry_Tbl where email = #Email)
ELSE a.FirstName
END As [FirstName],
If email is in both tables, you can JOIN on that field and then use COALESCE to pull non-null data:
SELECT
Email = COALESCE( i.Email, a.Email)
, FirstName = COALESCE(i.FirstName, a.FirstName)
, LastName = COALESCE(i.LastName, a.LastNamej)
FROM InquiryTBL i
LEFT JOIN ApplicationTBL a
ON i.Email = a.Email
If the email is in both tables or in only one table, use a FULL OUTER JOIN on email inside a sub-select. Then you can filter that sub-select by the email address you need.
DECLARE #email varchar(20) = 'ted#excellent.com' ;
SELECT s1.FirsTName, s1.LastName, s1.Email
FROM (
SELECT COALESCE(i.FirstName, a.FirstName) AS FirstName
, COALESCE(i.LastName, a.LastName) AS LastName
, COALESCE(i.email, a.email) AS email
FROM InquiryTBL i
FULL OUTER JOIN ApplicationTBL a ON i.email = a.email
) s1
WHERE s1.Email = #email
;
https://dbfiddle.uk/?rdbms=sqlserver_2012&fiddle=62d0f0a1026150de6b4fd9be3d058dbb
NOTE: To pick the first non-null value, you can use either coalesce() or isnull(). coalesce() is ANSI-compliant, can take more than 2 arguments and can be used in most databases. isnull() is primarily a Microsoft T-SQL function and takes only 2 arguments. It can be quicker in some cases than coalesce(), but it's not as portable. Both will also pick the first non-null value in your selected order, so you can change which value you want first.

LIKE operator instead of IN operator when checking tables

I am working on an SQL script to add the ID of a customer if the phone number is found inside a table.
Below is a snippet of my code:
CREATE TABLE #Customers ( AccountNumber nvarchar(20))
CREATE TABLE #Phones ( Number nvarchar(30))
INSERT INTO #Customers (AccountNumber)
SELECT AccountNumber
FROM CustomerTable
WHERE
(CustomerTable.PhoneNumber IN (SELECT DISTINCT Number FROM #Phones))
OR
(CustomerTable.PhoneNumber2 IN (SELECT DISTINCT Number FROM #Phones))
GO
However, using those statement I am only able to check if the exact record is met within the table. For example:
If Phones.Number has a record '123456' and CustomerTables.PhoneNumber has a record '123456 7', this record record will not be obtained, even if it contains the number.
Are their any modifications I can do to my current snippet in order to apply similar logic to the LIKE operator?
This can't be done directly with the in operator, but using a query inside the exists operator should do the trick:
INSERT INTO #Customers (AccountNumber)
SELECT AccountNumber
FROM CustomerTable ct
WHERE EXISTS (SELECT *
FROM #Phones p
WHERE ct.PhoneNumber LIKE '%' + p.Number + '%' OR
ct.PhoneNumber2 LIKE '%' + p.Number + '%')
SELECT AccountNumber
FROM CustomerTable c
join #Phones p
on c.PhoneNumber like p.Number + '%' OR c.PhoneNumber2 like p.Number + '%'
I think this will also work, but i have not tested.
WITH Customers AS
(
SELECT AccountNumber , Len(PhoneNumber) as PhoneSize
FROM
)
INSERT INTO #Customers (AccountNumber)
SELECT AccountNumber
FROM CustomerTable a
Join Customers b on a.AccountNumber = b.AccountNumber
WHERE
(CustomerTable.PhoneNumber IN (SELECT DISTINCT LEft(Number,b.PhoneSize) FROM #Phones))
OR
(CustomerTable.PhoneNumber2 IN (SELECT DISTINCT LEft(Number,b.PhoneSize) FROM #Phones))

Search 'AS' alias in SQL Server

I have a sql query that pulls the customers full name from the customers table based on the customer ID in the sales table and binds the info to an asp.net listview. I now want to search the records by the customers full name. I used the query below but it keeps telling me that the column "Fullname" is an invalid column name. How do I go about modifying this query to work?
SELECT
tbl_Sales.SaleID, tbl_Sales.CustomerID,
(SELECT Firstname + ' ' + Lastname
FROM tbl_Customers
WHERE CustomerID = tbl_Sales.CustomerID) AS CustomerName,
tbl_Sales.Price
FROM
tbl_Sales
WHERE
CustomerName LIKE '%John%'
Thanks in advance.
You cannot use the Alias name in the same select statement because where will be processed before the select.
Make the original query as sub select and use the alias name in where clause.Try this.
SELECT *
FROM (SELECT tbl_Sales.SaleID,
tbl_Sales.CustomerID,
(SELECT Firstname + ' ' + Lastname
FROM tbl_Customers
WHERE CustomerID = tbl_Sales.CustomerID) AS CustomerName,
tbl_Sales.Price
FROM tbl_Sales) A
WHERE CustomerName LIKE '%John%'
or use Cross apply
SELECT tbl_Sales.SaleID,
tbl_Sales.CustomerID,
t.CustomerName,
tbl_Sales.Price
FROM tbl_Sales
CROSS apply (SELECT Firstname + ' ' + Lastname AS CustomerName
FROM tbl_Customers
WHERE CustomerID = tbl_Sales.CustomerID) t
WHERE t.CustomerName LIKE '%John%'
You are not allowed to pass ALIAS name in WHERE clause instead of that you should use JOIN
Try this:
SELECT S.SaleID, S.CustomerID, (C.Firstname + ' ' + C.Lastname) AS CustomerName, S.Price
FROM tbl_Sales S
INNER JOIN tbl_Customers C ON S.CustomerID = C.CustomerID
WHERE (C.Firstname + ' ' + C.Lastname) LIKE '%John%'
Instead of checking the combined value, you could check each column like this. Combining the columns and comparing is not SARGable, and will result in bad performance:
SELECT
tbl_Sales.SaleID,
tbl_Sales.CustomerID,
cus.Firstname + ' ' + cus.Lastname CustomerName,
tbl_Sales.Price
FROM
tbl_Sales
JOIN
tbl_Customers cus
ON
cus.CustomerID = tbl_Sales.CustomerID
WHERE
(cus.Firstname LIKE '%John%' or
cus.Lastname LIKE '%John%')

Get ID from database matching multiple names

I have database with columns
ID FirstName LastName. I would like get the id of various people. Hence I am writing queries like the below.
Select ID from Database where FirstName='X' and LastName='x1'
Select ID from Database where FirstName='Y' and LastName='y1'
Select ID from Database where FirstName='Z' and LastName='z1'
I there any way to optimize this query.
You can simply put multiple condition in single where clause so just use them for simplicity.
Select ID from Database
where (FirstName='X' and LastName='x1') OR
(FirstName='Y' and LastName='y1') OR
(FirstName='Z' and LastName='z1')
with NULL Entries- You can add one more condition
OR (FirstName is NULL AND LastName is NULL)
If you want to get row even for those name which are not exists in your Database table (don't use this name for your table, BTW):
select a.FirstName, a.LastName, d.id
from (
select 'X', 'X1' union all
select 'Y', 'Y1' union all
select 'Z', 'Z1'
) as a(FirstName, LastName)
left outer join Database as d on d.FirstName = a.FirstName and d.LastName = a.LastName