Solved

MS SQL query

Posted on 2004-04-01
8
303 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
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.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Accessing SQL Server Instance from outside the network 29 102
SqlDataBase 7 46
get column names from table in vb.net 8 26
Run SQL Server Proc from Access 11 28
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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

806 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