Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS SQL query

Posted on 2004-04-01
8
Medium Priority
?
308 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 200 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

705 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