Hi Friends,
I know that we have a left and right outer join that retains the rows of the table based on how we chose to write the query. For example, depending on whether the table is to the left of the join or to the right of the join. My question is, in what situations, or queries do we want to retain the rows of the table with no matching records? Thanks !
When you're doing an "Upsert". That is, you're checking to see if rows already exist or not to know whether you have to do an INSERT or an UPDATE.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 15, 2019 at 9:12 pm
An example is below, say you want to query how many orders your customers have, but include in the output customers who don't have any orders at all:
SELECT c.cust_name, COUNT(o.order_id) AS order_count
FROM dbo.Cusomter c
LEFT OUTER JOIN dbo.Order o ON c.cust_id = o.cust_id;
GROUP BY c.cust_name
If you did an INNER JOIN instead of OUTER join, you would only get the customers who had at least 1 order.
October 15, 2019 at 9:39 pm
Thanks for your help, but I was wondering if you could maybe structure your answer in a real world example. I think that's why I am struggling with this stuff is because I haven't had an opportunity to see how it works in the real world. I understand it excellent in a text book or theoretical context, but again I get lost when trying to read these long queries or stored procedures someone wrote where I work and they have ZERO comments.
October 16, 2019 at 7:32 pm
CREATE TABLE #Customer (
cust_id int identity(1,1) primary key not null,
cust_name varchar(40));
INSERT INTO #Customer
VALUES ('Fred'),('Wilma'),('Barney'),('Betty');
CREATE TABLE #Order (
order_id int identity(1,1) primary key not null,
cust_id int not null,
order_date date);
INSERT INTO #Order
VALUES (1, '2018-05-22'), (1, '2018-08-04'), (1, '2019-04-12'),
(2, '2019-01-29'),
(3, '2018-02-10'), (3, '2018-06-01'), (3, '2018-11-15'), (3, '2019-02-02'), (3, '2019-05-11'), (3, '2019-08-21'),
(4, '2018-07-10');
--query 1
SELECT c.cust_name, COUNT(o.order_id) AS order_count
FROM #Customer c
LEFT OUTER JOIN #Order o ON c.cust_id = o.cust_id AND o.order_date >= '2019-01-01'
GROUP BY c.cust_name;
--query 2
SELECT c.cust_name, COUNT(o.order_id) AS order_count
FROM #Customer c
INNER JOIN #Order o ON c.cust_id = o.cust_id AND o.order_date >= '2019-01-01'
GROUP BY c.cust_name;
Okay I put some data here for you to see. In query 1 with the LEFT OUTER JOIN, then Betty shows up with order_count of 0 since she doesn't have any orders this year. If you use INNER JOIN like in query 2, then Betty doesn't show up in the results at all even though she is still in the Customer table.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply