July 15, 2013 at 9:52 am
here is the sample data .
create table empp(eno int,ename varchar(25),dept varchar(3))
insert into empp values(101,'raghava','hr')
insert into empp values(102,'krish','hr')
insert into empp values(103,'venkat','fin')
create table dept(dno int, dname varchar(3))
insert into dept values(1,'hr')
insert into dept values(2,'fin')
insert into dept values(3,'mkt')
all i need the deptname where there are no employees in it.
for above the example , it has to be 'mkt' as result.
Thanks for your help!!!:-):-)
July 15, 2013 at 10:10 am
select *
from dept d
left join empp e on e.dept = d.dname
where e.dept is null
Why do you the name of the department in your employee table. You should have the department number instead. Otherwise, what is the point of having a department table? 😉
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 15, 2013 at 10:14 am
thanks.. yes you were right... i need to use deptno only...
July 15, 2013 at 10:17 am
Just another way of doing it.
select *
from dept d
WHERE NOT EXISTS( SELECT 1 FROM empp e WHERE e.dept = d.dname)
July 15, 2013 at 10:21 am
Thanks Luis!!
July 15, 2013 at 10:25 am
Thanks Luis for posting the "other" option.
To the OP, which one is better? That is not as simple as it may seem. There are some considerations about the actual table structure to consider.
You might want to read Gail's article that explains the difference between these two approaches and the performance considerations around each of them.
http://sqlinthewild.co.za/index.php/2010/03/23/left-outer-join-vs-not-exists/[/url]
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 15, 2013 at 5:53 pm
Perhaps an even simpler solution:
--Sample data
IF OBJECT_ID('tempdb..#empp') IS NOT NULL
DROP TABLE #empp
CREATE TABLE #empp
(
eno INT
,ename VARCHAR(25)
,dept INT
)
INSERT INTO #empp
VALUES (101,'raghava',1)
INSERT INTO #empp
VALUES (102,'krish',1)
INSERT INTO #empp
VALUES (103,'venkat',3)
IF OBJECT_ID('tempdb..#dept') IS NOT NULL
DROP TABLE #dept
CREATE TABLE #dept
(
dno INT
,dname VARCHAR(3)
)
INSERT INTO #dept
VALUES (1,'hr')
INSERT INTO #dept
VALUES (2,'fin')
INSERT INTO #dept
VALUES (3,'mkt')
Returns only the missing dept number:
SELECT
dno
FROM
#dept AS d
EXCEPT
SELECT
dept
FROM
#empp AS e
July 15, 2013 at 9:58 pm
Steven Willis (7/15/2013)
Perhaps an even simpler solution:
That would have been my first choice for this. I believe that I'd reverse the order of the tables, though. Reason being is to look for NULLs and other anomolies.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 16, 2013 at 7:15 am
Steven Willis (7/15/2013)
Perhaps an even simpler solution:
Nice. I always forget about except, not really sure why. Thanks for reminding that is in the toolbox!!!
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 16, 2013 at 7:17 am
Thansk folks!!! i learned a lot here by posting the question...
thanks again!!!!
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply