Solved

MS SQL query

Posted on 2004-04-01
8
301 Views
Last Modified: 2010-05-18
Hi all,
I want to perform an intersection or minus operation in Ms SQL.
I was trying the following:

(select language.language from language)
intersect
(select language.language
from language,user,user_language
where
language.id = user_language.user_id
and
user.id = user_language.lang_id)

The table structures are:
user:
         id          varchar      primary key
         name     varchar
language:
         id           varchar     primary key
         language varchar
user_language:
        user_id    varchar
        lang_id    varchar


It's working fine if I give replace intersect with union. But for intersect it's giving the following error:

Server: Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'intersect'.

But I need to do the intersect operation. Does MS SQL not support intersect or my query is wrong.
Pls help
Thanks in advance
0
Comment
Question by:kajalg
  • 4
  • 2
8 Comments
 
LVL 26

Expert Comment

by:Hilaire
ID: 10731235
intersect doesn't exist in SQL Server

You should get what you want with

select distinct language.language
from language l inner user_language ul on l.id = ul.language_id
inner join user u on u.id = ul.user_id
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10731243
you don't need the INTERSECT in the above query, because the inner joins checks for existence of the language in the language table

HTH

Hilaire
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10731246
select distinct l.language
from language l inner user_language ul on l.id = ul.language_id
inner join user u on u.id = ul.user_id
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Comment

by:kajalg
ID: 10738508
There was an error in the query I gave. The where clause should be
"........
language.id = user_language.lang_id
and
user.id = user_language.user_id....
"
I think you pointed out the mistake.
However I run your query.

select distinct l.language
from language l inner user_language ul on l.id = ul.language_id
inner join user u on u.id = ul.user_id

It's giving the following error

Server: Msg 155, Level 15, State 1, Line 2
'user_language' is not a recognized join option.

 
0
 

Author Comment

by:kajalg
ID: 10738761
Actually what rowset I need is the language name some user doesn't have.
0
 
LVL 26

Accepted Solution

by:
Hilaire earned 50 total points
ID: 10739399
-- get languages that nobody has
select l.language
from language l
where not exists (
select 1
from user_language ul
inner join user u on u.id = ul.user_id
where ul.language_id = l.id
)

-- get languages that user 'John Doe' doesn't have
select l.language
from language l
where not exists (
select 1
from user_language ul
inner join user u on u.id = ul.user_id
where ul.language_id = l.id
and u.name = 'John Doe'
)


Sorry for the delay
Hilaire

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now