Solved

MS SQL query

Posted on 2004-04-01
8
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

738 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