Question

I'm working with the AdventureWorks example DB - we're running SQL Server 2008R2, so I assume that's the edition of AdventureWorks (I have read-only access). I'm trying to get a list of sales managers so that I can then determine a couple employee/manager relationships. I'm getting two sets of three differently named people, with the same job title, with their CurrentFlag set to 1 (active) with slightly different queries. I do notice that one result group has the same contactID and employeeID, but I'm not sure what this may indicate.

So the question is: Why am I getting completely different results with these two queires? I would think I'd get six results for each - the queries are matching employee table Titles.

  • SQL Query 1:

    select 
       c.FirstName, 
       c.LastName, 
       c.ContactID, 
       e.EmployeeID, 
       e.Title, 
       c.Title, 
       e.CurrentFlag
    from Person.Contact c 
    inner join HumanResources.Employee e
       on c.ContactID = e.ContactID
    where 
       e.Title like '%Sales Manager%'
    
  • SQL Query 2:

    SELECT 
       e.EmployeeID, 
       (c.FirstName + ' ' + c.LastName) as 'First Name and Last Name', 
       e.Title
    FROM HumanResources.Employee e 
    INNER JOIN Person.Contact c
        ON e.EmployeeID = c.ContactID
    Where 
        e.Title LIKE '%Manager%'
        AND
        e.Title LIKE '%Sales%'
    ORDER BY e.EmployeeID;
    

UPDATE: These are my results:

  • SQL Query 1:

    -------  -------  ----  ---  ----------------------------  ----  --
    Stephen  Jiang    1011  268  North American Sales Manager  NULL  1
    Amy      Alberts  1013  284  European Sales Manager        NULL  1
    Syed     Abbas    1012  288  Pacific Sales Manager         Mr.   1
    
  • SQL Query 2:

    ---  ---  -----------  ----------------------------  ---  --
    268  268  Gary Drury   North American Sales Manager  Mr.  1
    284  284  John Emory   European Sales Manager        Mr.  1
    288  288  Julie Estes  Pacific Sales Manager         Ms.  1
    
Was it helpful?

Solution

The only diffrents i can see is this:

where 
   e.Title like '%Sales Manager%'

And this:

Where 
 e.Title LIKE '%Manager%'
    AND
  e.Title LIKE '%Sales%'

The first query says that bring me all titles that has '%Sales Manager%' you can have for ex this output:

Account Sales Manager
some Sales Manager
Sales Manager something else

The second question says bring me all the titles that has '%Manager%' and '%Sales%' so you can for ex have:

Sales Account Manager
some Sales some Manager some 
Sales Manager some else thing
Manager Sales

And this join can not be corrent

INNER JOIN Person.Contact c
    ON e.EmployeeID = c.ContactID

Don't you mean:

INNER JOIN Person.Contact c
        ON e.ContactID= c.ContactID

OTHER TIPS

The first query will match the rows where substring "Sales Manager" is present. But second one can match rows like "Managers of Sales Dep" as well. I mean the second doesn't care about positions of the words in the srting.

I believe that the results of first query is a subset of the results of second one.

UPDATE You use different columns in JOIN clause, so it's normal that you got different results.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top