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;








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



enter image description here










share|improve this question





















  • 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

















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



enter image description here










share|improve this question





















  • 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













0












0








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



enter image description here










share|improve this question
















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



enter image description here







sql sql-server subquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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







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












2 Answers
2






active

oldest

votes


















1
















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






share|improve this answer



























  • This seems to have worked. Thank you.

    – Nolan Schue
    Mar 28 at 12:56


















0
















;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





share|improve this answer

























  • 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














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
);



);













draft saved

draft discarded


















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









1
















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






share|improve this answer



























  • This seems to have worked. Thank you.

    – Nolan Schue
    Mar 28 at 12:56















1
















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






share|improve this answer



























  • This seems to have worked. Thank you.

    – Nolan Schue
    Mar 28 at 12:56













1














1










1









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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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













0
















;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





share|improve this answer

























  • 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
















0
















;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





share|improve this answer

























  • 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














0














0










0









;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





share|improve this answer













;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






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript