Solved

MS SQL query

Posted on 2004-04-01
8
300 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

708 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

12 Experts available now in Live!

Get 1:1 Help Now