using not with sub-queriesUsing max function with Multiple tables in a sub querySQL Server: Query fast, but slow from procedureInserting multiple rows in a single SQL query?Does a foreign key automatically create an index?SQL update query using joinsSET NOCOUNT ON usagesql query to return differences between two tablesJoin vs. sub-queryHow to query MongoDB with “like”?sql sub query difficultiesUsing max function with Multiple tables in a sub query
How to find better food in airports
The little bee buzzes around the flower garden
slowest crash on the Moon?
co-son-in-law or co-brother
Main differences between 5th edition Druid and 3.5 edition Druid
Everyone for non livings
Are kids with daycare background antisocial?
Finder/Terminal: Find files that contain less than 21 lines of text
Is there any difference between these two sentences? (Adverbs)
How to generate all 3×3 matrices with a,a,a,a,b,b,b,c,c?
Count rook moves 1D
IEEE Registration Authority mac prefix
Which is the best password hashing algorithm in .NET Core?
Can a country avoid prosecution for crimes against humanity by denying it happened?
When is it legal to castle moving the rook first?
What exactly is a softlock?
Planet that’s 90% water or more?
Did the US Climate Reference Network Show No New Warming Since 2005 in the US?
Adding transparency to ink drawing
What does "se jouer" mean here?
Do I need to get a noble in order to win Splendor?
Can there be plants on the dark side of a tidally locked world?
To which airspace does the border of two adjacent airspaces belong to?
What's the difference between a share and a stock?
using not with sub-queries
Using max function with Multiple tables in a sub querySQL Server: Query fast, but slow from procedureInserting multiple rows in a single SQL query?Does a foreign key automatically create an index?SQL update query using joinsSET NOCOUNT ON usagesql query to return differences between two tablesJoin vs. sub-queryHow to query MongoDB with “like”?sql sub query difficultiesUsing max function with Multiple tables in a sub query
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have been asked to display results in my sql server database for the following question
What software packages are not installed on any HP computers?
I have tried the following but I am still getting results for the PACKNAME Manta but that package is installed on an HP computer. What am I missing?
select * from package where PACK in
( select PACK from software where TAGNUM in
( select tagnum from PC where comp NOT in
( select comp from computer where MFRNAME = 'HP')))
I have attached an image of the data for your reference below
sql sql-server subquery
add a comment |
I have been asked to display results in my sql server database for the following question
What software packages are not installed on any HP computers?
I have tried the following but I am still getting results for the PACKNAME Manta but that package is installed on an HP computer. What am I missing?
select * from package where PACK in
( select PACK from software where TAGNUM in
( select tagnum from PC where comp NOT in
( select comp from computer where MFRNAME = 'HP')))
I have attached an image of the data for your reference below
sql sql-server subquery
1
this looks like some sort ofhomework
. Before we attempt to help you with the query, Is there any particular method of query that you must use or cannot use ?
– Squirrel
Mar 28 at 4:24
I have been told to only use sub-queries.
– Nolan Schue
Mar 28 at 4:37
Any query that answers a question "where something doesnt exist" is generally best answered with a NOT EXISTS(subquery) pattern. Makes it really easy to understand and generally efficient to run. I would research that.
– TomC
Mar 28 at 4:37
2
mysql or sql-server? They aren't the same thing.
– Dale Burrell
Mar 28 at 5:06
This is for Microsoft SQL server management studio 17
– Nolan Schue
Mar 28 at 12:53
add a comment |
I have been asked to display results in my sql server database for the following question
What software packages are not installed on any HP computers?
I have tried the following but I am still getting results for the PACKNAME Manta but that package is installed on an HP computer. What am I missing?
select * from package where PACK in
( select PACK from software where TAGNUM in
( select tagnum from PC where comp NOT in
( select comp from computer where MFRNAME = 'HP')))
I have attached an image of the data for your reference below
sql sql-server subquery
I have been asked to display results in my sql server database for the following question
What software packages are not installed on any HP computers?
I have tried the following but I am still getting results for the PACKNAME Manta but that package is installed on an HP computer. What am I missing?
select * from package where PACK in
( select PACK from software where TAGNUM in
( select tagnum from PC where comp NOT in
( select comp from computer where MFRNAME = 'HP')))
I have attached an image of the data for your reference below
sql sql-server subquery
sql sql-server subquery
edited Mar 28 at 12:56
Nolan Schue
asked Mar 28 at 3:22
Nolan SchueNolan Schue
175 bronze badges
175 bronze badges
1
this looks like some sort ofhomework
. Before we attempt to help you with the query, Is there any particular method of query that you must use or cannot use ?
– Squirrel
Mar 28 at 4:24
I have been told to only use sub-queries.
– Nolan Schue
Mar 28 at 4:37
Any query that answers a question "where something doesnt exist" is generally best answered with a NOT EXISTS(subquery) pattern. Makes it really easy to understand and generally efficient to run. I would research that.
– TomC
Mar 28 at 4:37
2
mysql or sql-server? They aren't the same thing.
– Dale Burrell
Mar 28 at 5:06
This is for Microsoft SQL server management studio 17
– Nolan Schue
Mar 28 at 12:53
add a comment |
1
this looks like some sort ofhomework
. Before we attempt to help you with the query, Is there any particular method of query that you must use or cannot use ?
– Squirrel
Mar 28 at 4:24
I have been told to only use sub-queries.
– Nolan Schue
Mar 28 at 4:37
Any query that answers a question "where something doesnt exist" is generally best answered with a NOT EXISTS(subquery) pattern. Makes it really easy to understand and generally efficient to run. I would research that.
– TomC
Mar 28 at 4:37
2
mysql or sql-server? They aren't the same thing.
– Dale Burrell
Mar 28 at 5:06
This is for Microsoft SQL server management studio 17
– Nolan Schue
Mar 28 at 12:53
1
1
this looks like some sort of
homework
. Before we attempt to help you with the query, Is there any particular method of query that you must use or cannot use ?– Squirrel
Mar 28 at 4:24
this looks like some sort of
homework
. Before we attempt to help you with the query, Is there any particular method of query that you must use or cannot use ?– Squirrel
Mar 28 at 4:24
I have been told to only use sub-queries.
– Nolan Schue
Mar 28 at 4:37
I have been told to only use sub-queries.
– Nolan Schue
Mar 28 at 4:37
Any query that answers a question "where something doesnt exist" is generally best answered with a NOT EXISTS(subquery) pattern. Makes it really easy to understand and generally efficient to run. I would research that.
– TomC
Mar 28 at 4:37
Any query that answers a question "where something doesnt exist" is generally best answered with a NOT EXISTS(subquery) pattern. Makes it really easy to understand and generally efficient to run. I would research that.
– TomC
Mar 28 at 4:37
2
2
mysql or sql-server? They aren't the same thing.
– Dale Burrell
Mar 28 at 5:06
mysql or sql-server? They aren't the same thing.
– Dale Burrell
Mar 28 at 5:06
This is for Microsoft SQL server management studio 17
– Nolan Schue
Mar 28 at 12:53
This is for Microsoft SQL server management studio 17
– Nolan Schue
Mar 28 at 12:53
add a comment |
2 Answers
2
active
oldest
votes
You can use NOT EXISTS
with a correlated subquery that joins the other tables together, and contains your filter condition:
select
pk.pack,
pk.packname,
pk.packv,
pk.packtype,
pk.packcost
from package pk
where not exists (
select 1
from software s
inner join pc on pc.tagnum = s.tagnum
inner join computer c on c.comp = pc.comp
where s.pack = pk.pack
and c.mfrname = 'HP'
)
order by pk.pack;
Result
| pack | packname | packv | packtype | packcost |
|------|------------------|-------|-----------------|----------|
| AC11 | Quick Accounting | 4.1 | Accounting | 754.95 |
| AC12 | Accounting MIS | 4.0 | Accounting | 2000 |
| AC13 | Quickbook | 2005 | Accounting | 300 |
| DB11 | Manta | 1.5 | Database | 380 |
| DB13 | SQL Server | 2005 | Database | 500 |
| DB14 | My SQL | 2005 | Database | 300 |
| SS11 | Easycal | 5.5 | Spreadsheet | 225.15 |
| WP04 | Word Power | 2 | Word Processing | 118 |
| WP07 | Good Word | 3.2 | Word Processing | 35 |
| WP14 | GOOGLE | 2 | Word Processing | 118 |
SQL Fiddle example
This seems to have worked. Thank you.
– Nolan Schue
Mar 28 at 12:56
add a comment |
;with installed as
( select PACK from software where TAGNUM in ( select tagnum from PC where comp in ( select comp from computer where MFRNAME = 'HP')))
select * from Package p left join installed i on p.pack = i.pack
where i.pack is null
Thanks for the prompt reply but I am still receiving a result with PACKNAME MANTA in the list.
– Nolan Schue
Mar 28 at 3:35
It must be in the list then. have you checked?
– Programnik
Mar 28 at 3:39
Yes. Manta is installed on the HP computer associated with the PACK DB22 but the question is phrased in a way that it is looking for all packages that are not installed on any HP computers
– Nolan Schue
Mar 28 at 3:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389680%2fusing-not-with-sub-queries%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use NOT EXISTS
with a correlated subquery that joins the other tables together, and contains your filter condition:
select
pk.pack,
pk.packname,
pk.packv,
pk.packtype,
pk.packcost
from package pk
where not exists (
select 1
from software s
inner join pc on pc.tagnum = s.tagnum
inner join computer c on c.comp = pc.comp
where s.pack = pk.pack
and c.mfrname = 'HP'
)
order by pk.pack;
Result
| pack | packname | packv | packtype | packcost |
|------|------------------|-------|-----------------|----------|
| AC11 | Quick Accounting | 4.1 | Accounting | 754.95 |
| AC12 | Accounting MIS | 4.0 | Accounting | 2000 |
| AC13 | Quickbook | 2005 | Accounting | 300 |
| DB11 | Manta | 1.5 | Database | 380 |
| DB13 | SQL Server | 2005 | Database | 500 |
| DB14 | My SQL | 2005 | Database | 300 |
| SS11 | Easycal | 5.5 | Spreadsheet | 225.15 |
| WP04 | Word Power | 2 | Word Processing | 118 |
| WP07 | Good Word | 3.2 | Word Processing | 35 |
| WP14 | GOOGLE | 2 | Word Processing | 118 |
SQL Fiddle example
This seems to have worked. Thank you.
– Nolan Schue
Mar 28 at 12:56
add a comment |
You can use NOT EXISTS
with a correlated subquery that joins the other tables together, and contains your filter condition:
select
pk.pack,
pk.packname,
pk.packv,
pk.packtype,
pk.packcost
from package pk
where not exists (
select 1
from software s
inner join pc on pc.tagnum = s.tagnum
inner join computer c on c.comp = pc.comp
where s.pack = pk.pack
and c.mfrname = 'HP'
)
order by pk.pack;
Result
| pack | packname | packv | packtype | packcost |
|------|------------------|-------|-----------------|----------|
| AC11 | Quick Accounting | 4.1 | Accounting | 754.95 |
| AC12 | Accounting MIS | 4.0 | Accounting | 2000 |
| AC13 | Quickbook | 2005 | Accounting | 300 |
| DB11 | Manta | 1.5 | Database | 380 |
| DB13 | SQL Server | 2005 | Database | 500 |
| DB14 | My SQL | 2005 | Database | 300 |
| SS11 | Easycal | 5.5 | Spreadsheet | 225.15 |
| WP04 | Word Power | 2 | Word Processing | 118 |
| WP07 | Good Word | 3.2 | Word Processing | 35 |
| WP14 | GOOGLE | 2 | Word Processing | 118 |
SQL Fiddle example
This seems to have worked. Thank you.
– Nolan Schue
Mar 28 at 12:56
add a comment |
You can use NOT EXISTS
with a correlated subquery that joins the other tables together, and contains your filter condition:
select
pk.pack,
pk.packname,
pk.packv,
pk.packtype,
pk.packcost
from package pk
where not exists (
select 1
from software s
inner join pc on pc.tagnum = s.tagnum
inner join computer c on c.comp = pc.comp
where s.pack = pk.pack
and c.mfrname = 'HP'
)
order by pk.pack;
Result
| pack | packname | packv | packtype | packcost |
|------|------------------|-------|-----------------|----------|
| AC11 | Quick Accounting | 4.1 | Accounting | 754.95 |
| AC12 | Accounting MIS | 4.0 | Accounting | 2000 |
| AC13 | Quickbook | 2005 | Accounting | 300 |
| DB11 | Manta | 1.5 | Database | 380 |
| DB13 | SQL Server | 2005 | Database | 500 |
| DB14 | My SQL | 2005 | Database | 300 |
| SS11 | Easycal | 5.5 | Spreadsheet | 225.15 |
| WP04 | Word Power | 2 | Word Processing | 118 |
| WP07 | Good Word | 3.2 | Word Processing | 35 |
| WP14 | GOOGLE | 2 | Word Processing | 118 |
SQL Fiddle example
You can use NOT EXISTS
with a correlated subquery that joins the other tables together, and contains your filter condition:
select
pk.pack,
pk.packname,
pk.packv,
pk.packtype,
pk.packcost
from package pk
where not exists (
select 1
from software s
inner join pc on pc.tagnum = s.tagnum
inner join computer c on c.comp = pc.comp
where s.pack = pk.pack
and c.mfrname = 'HP'
)
order by pk.pack;
Result
| pack | packname | packv | packtype | packcost |
|------|------------------|-------|-----------------|----------|
| AC11 | Quick Accounting | 4.1 | Accounting | 754.95 |
| AC12 | Accounting MIS | 4.0 | Accounting | 2000 |
| AC13 | Quickbook | 2005 | Accounting | 300 |
| DB11 | Manta | 1.5 | Database | 380 |
| DB13 | SQL Server | 2005 | Database | 500 |
| DB14 | My SQL | 2005 | Database | 300 |
| SS11 | Easycal | 5.5 | Spreadsheet | 225.15 |
| WP04 | Word Power | 2 | Word Processing | 118 |
| WP07 | Good Word | 3.2 | Word Processing | 35 |
| WP14 | GOOGLE | 2 | Word Processing | 118 |
SQL Fiddle example
edited Mar 28 at 6:52
Strawberry
28k8 gold badges33 silver badges52 bronze badges
28k8 gold badges33 silver badges52 bronze badges
answered Mar 28 at 4:59
bruceskyausbruceskyaus
6984 silver badges12 bronze badges
6984 silver badges12 bronze badges
This seems to have worked. Thank you.
– Nolan Schue
Mar 28 at 12:56
add a comment |
This seems to have worked. Thank you.
– Nolan Schue
Mar 28 at 12:56
This seems to have worked. Thank you.
– Nolan Schue
Mar 28 at 12:56
This seems to have worked. Thank you.
– Nolan Schue
Mar 28 at 12:56
add a comment |
;with installed as
( select PACK from software where TAGNUM in ( select tagnum from PC where comp in ( select comp from computer where MFRNAME = 'HP')))
select * from Package p left join installed i on p.pack = i.pack
where i.pack is null
Thanks for the prompt reply but I am still receiving a result with PACKNAME MANTA in the list.
– Nolan Schue
Mar 28 at 3:35
It must be in the list then. have you checked?
– Programnik
Mar 28 at 3:39
Yes. Manta is installed on the HP computer associated with the PACK DB22 but the question is phrased in a way that it is looking for all packages that are not installed on any HP computers
– Nolan Schue
Mar 28 at 3:41
add a comment |
;with installed as
( select PACK from software where TAGNUM in ( select tagnum from PC where comp in ( select comp from computer where MFRNAME = 'HP')))
select * from Package p left join installed i on p.pack = i.pack
where i.pack is null
Thanks for the prompt reply but I am still receiving a result with PACKNAME MANTA in the list.
– Nolan Schue
Mar 28 at 3:35
It must be in the list then. have you checked?
– Programnik
Mar 28 at 3:39
Yes. Manta is installed on the HP computer associated with the PACK DB22 but the question is phrased in a way that it is looking for all packages that are not installed on any HP computers
– Nolan Schue
Mar 28 at 3:41
add a comment |
;with installed as
( select PACK from software where TAGNUM in ( select tagnum from PC where comp in ( select comp from computer where MFRNAME = 'HP')))
select * from Package p left join installed i on p.pack = i.pack
where i.pack is null
;with installed as
( select PACK from software where TAGNUM in ( select tagnum from PC where comp in ( select comp from computer where MFRNAME = 'HP')))
select * from Package p left join installed i on p.pack = i.pack
where i.pack is null
answered Mar 28 at 3:26
ProgramnikProgramnik
8436 silver badges11 bronze badges
8436 silver badges11 bronze badges
Thanks for the prompt reply but I am still receiving a result with PACKNAME MANTA in the list.
– Nolan Schue
Mar 28 at 3:35
It must be in the list then. have you checked?
– Programnik
Mar 28 at 3:39
Yes. Manta is installed on the HP computer associated with the PACK DB22 but the question is phrased in a way that it is looking for all packages that are not installed on any HP computers
– Nolan Schue
Mar 28 at 3:41
add a comment |
Thanks for the prompt reply but I am still receiving a result with PACKNAME MANTA in the list.
– Nolan Schue
Mar 28 at 3:35
It must be in the list then. have you checked?
– Programnik
Mar 28 at 3:39
Yes. Manta is installed on the HP computer associated with the PACK DB22 but the question is phrased in a way that it is looking for all packages that are not installed on any HP computers
– Nolan Schue
Mar 28 at 3:41
Thanks for the prompt reply but I am still receiving a result with PACKNAME MANTA in the list.
– Nolan Schue
Mar 28 at 3:35
Thanks for the prompt reply but I am still receiving a result with PACKNAME MANTA in the list.
– Nolan Schue
Mar 28 at 3:35
It must be in the list then. have you checked?
– Programnik
Mar 28 at 3:39
It must be in the list then. have you checked?
– Programnik
Mar 28 at 3:39
Yes. Manta is installed on the HP computer associated with the PACK DB22 but the question is phrased in a way that it is looking for all packages that are not installed on any HP computers
– Nolan Schue
Mar 28 at 3:41
Yes. Manta is installed on the HP computer associated with the PACK DB22 but the question is phrased in a way that it is looking for all packages that are not installed on any HP computers
– Nolan Schue
Mar 28 at 3:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389680%2fusing-not-with-sub-queries%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
this looks like some sort of
homework
. Before we attempt to help you with the query, Is there any particular method of query that you must use or cannot use ?– Squirrel
Mar 28 at 4:24
I have been told to only use sub-queries.
– Nolan Schue
Mar 28 at 4:37
Any query that answers a question "where something doesnt exist" is generally best answered with a NOT EXISTS(subquery) pattern. Makes it really easy to understand and generally efficient to run. I would research that.
– TomC
Mar 28 at 4:37
2
mysql or sql-server? They aren't the same thing.
– Dale Burrell
Mar 28 at 5:06
This is for Microsoft SQL server management studio 17
– Nolan Schue
Mar 28 at 12:53