Subquery in mySQL , referring to a book by it's isbn rather than title
I need to , using a subquery approach, find the number of times the book with the title ‘Room’ was part of an order.
relevant schema tables
orderDetails(oNo, bookISBN, quantity)
book(isbn, title, authorID, genre, pubYear, publisher,
rrPrice, avgRating)
It appears in two orders and bookISBN and isbn are the primary and foreign key
I have tried
SELECT Count(*)
FROM orderDetails
WHERE 'Room' in (SELECT title
FROM book)
Which returns 25, even though there are only two orders with the book Room. I can see why it is wrong but I can't figure out how I would call the book by it's ISBN (without just typing 323 as that would be a shortcut)
mysql sql subquery union
add a comment |
I need to , using a subquery approach, find the number of times the book with the title ‘Room’ was part of an order.
relevant schema tables
orderDetails(oNo, bookISBN, quantity)
book(isbn, title, authorID, genre, pubYear, publisher,
rrPrice, avgRating)
It appears in two orders and bookISBN and isbn are the primary and foreign key
I have tried
SELECT Count(*)
FROM orderDetails
WHERE 'Room' in (SELECT title
FROM book)
Which returns 25, even though there are only two orders with the book Room. I can see why it is wrong but I can't figure out how I would call the book by it's ISBN (without just typing 323 as that would be a shortcut)
mysql sql subquery union
Please provide sample data.
– Gordon Linoff
Nov 12 at 11:40
Is using subquery necessary ? Is it some kind of homework ?
– Madhur Bhaiya
Nov 12 at 11:41
I know that there's debate about this kind of thing, but personally, I wouldn't use an ISBN as a primary key. My view is that the PK should have no meaning beyond the scope of the database - and, after all, a book sometimes has more than one ISBN!
– Strawberry
Nov 12 at 11:51
Please clarify whether you want to consider only those books wheretitle
is exactly'Room'
or contains'Room'
in it.
– Madhur Bhaiya
Nov 12 at 12:01
add a comment |
I need to , using a subquery approach, find the number of times the book with the title ‘Room’ was part of an order.
relevant schema tables
orderDetails(oNo, bookISBN, quantity)
book(isbn, title, authorID, genre, pubYear, publisher,
rrPrice, avgRating)
It appears in two orders and bookISBN and isbn are the primary and foreign key
I have tried
SELECT Count(*)
FROM orderDetails
WHERE 'Room' in (SELECT title
FROM book)
Which returns 25, even though there are only two orders with the book Room. I can see why it is wrong but I can't figure out how I would call the book by it's ISBN (without just typing 323 as that would be a shortcut)
mysql sql subquery union
I need to , using a subquery approach, find the number of times the book with the title ‘Room’ was part of an order.
relevant schema tables
orderDetails(oNo, bookISBN, quantity)
book(isbn, title, authorID, genre, pubYear, publisher,
rrPrice, avgRating)
It appears in two orders and bookISBN and isbn are the primary and foreign key
I have tried
SELECT Count(*)
FROM orderDetails
WHERE 'Room' in (SELECT title
FROM book)
Which returns 25, even though there are only two orders with the book Room. I can see why it is wrong but I can't figure out how I would call the book by it's ISBN (without just typing 323 as that would be a shortcut)
mysql sql subquery union
mysql sql subquery union
asked Nov 12 at 11:39
Jade
145
145
Please provide sample data.
– Gordon Linoff
Nov 12 at 11:40
Is using subquery necessary ? Is it some kind of homework ?
– Madhur Bhaiya
Nov 12 at 11:41
I know that there's debate about this kind of thing, but personally, I wouldn't use an ISBN as a primary key. My view is that the PK should have no meaning beyond the scope of the database - and, after all, a book sometimes has more than one ISBN!
– Strawberry
Nov 12 at 11:51
Please clarify whether you want to consider only those books wheretitle
is exactly'Room'
or contains'Room'
in it.
– Madhur Bhaiya
Nov 12 at 12:01
add a comment |
Please provide sample data.
– Gordon Linoff
Nov 12 at 11:40
Is using subquery necessary ? Is it some kind of homework ?
– Madhur Bhaiya
Nov 12 at 11:41
I know that there's debate about this kind of thing, but personally, I wouldn't use an ISBN as a primary key. My view is that the PK should have no meaning beyond the scope of the database - and, after all, a book sometimes has more than one ISBN!
– Strawberry
Nov 12 at 11:51
Please clarify whether you want to consider only those books wheretitle
is exactly'Room'
or contains'Room'
in it.
– Madhur Bhaiya
Nov 12 at 12:01
Please provide sample data.
– Gordon Linoff
Nov 12 at 11:40
Please provide sample data.
– Gordon Linoff
Nov 12 at 11:40
Is using subquery necessary ? Is it some kind of homework ?
– Madhur Bhaiya
Nov 12 at 11:41
Is using subquery necessary ? Is it some kind of homework ?
– Madhur Bhaiya
Nov 12 at 11:41
I know that there's debate about this kind of thing, but personally, I wouldn't use an ISBN as a primary key. My view is that the PK should have no meaning beyond the scope of the database - and, after all, a book sometimes has more than one ISBN!
– Strawberry
Nov 12 at 11:51
I know that there's debate about this kind of thing, but personally, I wouldn't use an ISBN as a primary key. My view is that the PK should have no meaning beyond the scope of the database - and, after all, a book sometimes has more than one ISBN!
– Strawberry
Nov 12 at 11:51
Please clarify whether you want to consider only those books where
title
is exactly 'Room'
or contains 'Room'
in it.– Madhur Bhaiya
Nov 12 at 12:01
Please clarify whether you want to consider only those books where
title
is exactly 'Room'
or contains 'Room'
in it.– Madhur Bhaiya
Nov 12 at 12:01
add a comment |
4 Answers
4
active
oldest
votes
Use this query
SELECT Count(*)
FROM orderDetails
WHERE bookISBN in (SELECT isbn
FROM book where title = 'Room')
You can use the Like
operator in subquery as well %Room%
for title
add a comment |
Your where clause either evaluates to true or false. It is not correlated to the outer query, so:
- "true" means that all rows are returned (and counted)
- "false" means that no rows are returned (and counted)
You can fix this by using a correlation clause:
SELECT Count(*)
FROM orderDetails od
WHERE 'Room' IN (SELECT b.title FROM book b WHERE od.bookISBN = b.ISBN);
add a comment |
Normal Query, without using subquery:
SELECT
COUNT(1)
FROM
orderDetails od
INNER JOIN book b ON b.isbn = od.bookISBN
WHERE
b.title LIKE '%ROOM%'
Abnormal query, with inner query, I don't recommend this as this is unnecessary, but here we go:
SELECT
COUNT(1)
FROM
orderDetails od
INNER JOIN (
SELECT isbn, title FROM book
) b ON b.isbn = od.bookISBN
WHERE
b.title LIKE '%ROOM%
Even more abnormal query with subquery:
SELECT
COUNT(1)
FROM
orderDetails od
WHERE
od.bookISBN IN (SELECT isbn FROM book WHERE title like '%ROOM%')
TIPS:
1. I have used count(1)
, since, it doesn't make sense to count all the columns, so, instead, you can just count the successful entries to the condition and it will be faster.
2. Using a subquery limits the capability of the compiler, hence, I advise against it.
add a comment |
In my opinion Join Would be better:
SELECT Count(*)
FROM orderDetails LEFT JOIN book ON isbn=bookISBN
WHERE title LIKE N'%Room%'
but if you must use sub query:
SELECT Count(*)
FROM orderDetails
WHERE bookISBN in (SELECT isbn
FROM book
WHERE title LIKE N'%Room%')
no matter which way we use we always must use common field to get
1
These queries cannot decide whether they are INNER JOINs or LEFT JOINs
– Strawberry
Nov 12 at 11:56
left join would be better while it need only number of orders
– sanaz amini
Nov 12 at 11:59
@Strawberry is right the first query will run as aINNER JOIN
instead.. The filter condition with aLEFT JOIN
should be in theON
clause instead.. it should beLEFT JOIN book ON isbn=bookISBN AND title LIKE N'%Room%'
...
– Raymond Nijland
Nov 12 at 12:00
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%2f53261390%2fsubquery-in-mysql-referring-to-a-book-by-its-isbn-rather-than-title%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use this query
SELECT Count(*)
FROM orderDetails
WHERE bookISBN in (SELECT isbn
FROM book where title = 'Room')
You can use the Like
operator in subquery as well %Room%
for title
add a comment |
Use this query
SELECT Count(*)
FROM orderDetails
WHERE bookISBN in (SELECT isbn
FROM book where title = 'Room')
You can use the Like
operator in subquery as well %Room%
for title
add a comment |
Use this query
SELECT Count(*)
FROM orderDetails
WHERE bookISBN in (SELECT isbn
FROM book where title = 'Room')
You can use the Like
operator in subquery as well %Room%
for title
Use this query
SELECT Count(*)
FROM orderDetails
WHERE bookISBN in (SELECT isbn
FROM book where title = 'Room')
You can use the Like
operator in subquery as well %Room%
for title
edited Nov 12 at 11:58
Nick
22.5k81535
22.5k81535
answered Nov 12 at 11:46
Yash Fatnani
1015
1015
add a comment |
add a comment |
Your where clause either evaluates to true or false. It is not correlated to the outer query, so:
- "true" means that all rows are returned (and counted)
- "false" means that no rows are returned (and counted)
You can fix this by using a correlation clause:
SELECT Count(*)
FROM orderDetails od
WHERE 'Room' IN (SELECT b.title FROM book b WHERE od.bookISBN = b.ISBN);
add a comment |
Your where clause either evaluates to true or false. It is not correlated to the outer query, so:
- "true" means that all rows are returned (and counted)
- "false" means that no rows are returned (and counted)
You can fix this by using a correlation clause:
SELECT Count(*)
FROM orderDetails od
WHERE 'Room' IN (SELECT b.title FROM book b WHERE od.bookISBN = b.ISBN);
add a comment |
Your where clause either evaluates to true or false. It is not correlated to the outer query, so:
- "true" means that all rows are returned (and counted)
- "false" means that no rows are returned (and counted)
You can fix this by using a correlation clause:
SELECT Count(*)
FROM orderDetails od
WHERE 'Room' IN (SELECT b.title FROM book b WHERE od.bookISBN = b.ISBN);
Your where clause either evaluates to true or false. It is not correlated to the outer query, so:
- "true" means that all rows are returned (and counted)
- "false" means that no rows are returned (and counted)
You can fix this by using a correlation clause:
SELECT Count(*)
FROM orderDetails od
WHERE 'Room' IN (SELECT b.title FROM book b WHERE od.bookISBN = b.ISBN);
edited Nov 12 at 11:54
Strawberry
25.8k83149
25.8k83149
answered Nov 12 at 11:42
Gordon Linoff
755k35290397
755k35290397
add a comment |
add a comment |
Normal Query, without using subquery:
SELECT
COUNT(1)
FROM
orderDetails od
INNER JOIN book b ON b.isbn = od.bookISBN
WHERE
b.title LIKE '%ROOM%'
Abnormal query, with inner query, I don't recommend this as this is unnecessary, but here we go:
SELECT
COUNT(1)
FROM
orderDetails od
INNER JOIN (
SELECT isbn, title FROM book
) b ON b.isbn = od.bookISBN
WHERE
b.title LIKE '%ROOM%
Even more abnormal query with subquery:
SELECT
COUNT(1)
FROM
orderDetails od
WHERE
od.bookISBN IN (SELECT isbn FROM book WHERE title like '%ROOM%')
TIPS:
1. I have used count(1)
, since, it doesn't make sense to count all the columns, so, instead, you can just count the successful entries to the condition and it will be faster.
2. Using a subquery limits the capability of the compiler, hence, I advise against it.
add a comment |
Normal Query, without using subquery:
SELECT
COUNT(1)
FROM
orderDetails od
INNER JOIN book b ON b.isbn = od.bookISBN
WHERE
b.title LIKE '%ROOM%'
Abnormal query, with inner query, I don't recommend this as this is unnecessary, but here we go:
SELECT
COUNT(1)
FROM
orderDetails od
INNER JOIN (
SELECT isbn, title FROM book
) b ON b.isbn = od.bookISBN
WHERE
b.title LIKE '%ROOM%
Even more abnormal query with subquery:
SELECT
COUNT(1)
FROM
orderDetails od
WHERE
od.bookISBN IN (SELECT isbn FROM book WHERE title like '%ROOM%')
TIPS:
1. I have used count(1)
, since, it doesn't make sense to count all the columns, so, instead, you can just count the successful entries to the condition and it will be faster.
2. Using a subquery limits the capability of the compiler, hence, I advise against it.
add a comment |
Normal Query, without using subquery:
SELECT
COUNT(1)
FROM
orderDetails od
INNER JOIN book b ON b.isbn = od.bookISBN
WHERE
b.title LIKE '%ROOM%'
Abnormal query, with inner query, I don't recommend this as this is unnecessary, but here we go:
SELECT
COUNT(1)
FROM
orderDetails od
INNER JOIN (
SELECT isbn, title FROM book
) b ON b.isbn = od.bookISBN
WHERE
b.title LIKE '%ROOM%
Even more abnormal query with subquery:
SELECT
COUNT(1)
FROM
orderDetails od
WHERE
od.bookISBN IN (SELECT isbn FROM book WHERE title like '%ROOM%')
TIPS:
1. I have used count(1)
, since, it doesn't make sense to count all the columns, so, instead, you can just count the successful entries to the condition and it will be faster.
2. Using a subquery limits the capability of the compiler, hence, I advise against it.
Normal Query, without using subquery:
SELECT
COUNT(1)
FROM
orderDetails od
INNER JOIN book b ON b.isbn = od.bookISBN
WHERE
b.title LIKE '%ROOM%'
Abnormal query, with inner query, I don't recommend this as this is unnecessary, but here we go:
SELECT
COUNT(1)
FROM
orderDetails od
INNER JOIN (
SELECT isbn, title FROM book
) b ON b.isbn = od.bookISBN
WHERE
b.title LIKE '%ROOM%
Even more abnormal query with subquery:
SELECT
COUNT(1)
FROM
orderDetails od
WHERE
od.bookISBN IN (SELECT isbn FROM book WHERE title like '%ROOM%')
TIPS:
1. I have used count(1)
, since, it doesn't make sense to count all the columns, so, instead, you can just count the successful entries to the condition and it will be faster.
2. Using a subquery limits the capability of the compiler, hence, I advise against it.
edited Nov 12 at 12:07
answered Nov 12 at 11:52
VisheshNayak
184
184
add a comment |
add a comment |
In my opinion Join Would be better:
SELECT Count(*)
FROM orderDetails LEFT JOIN book ON isbn=bookISBN
WHERE title LIKE N'%Room%'
but if you must use sub query:
SELECT Count(*)
FROM orderDetails
WHERE bookISBN in (SELECT isbn
FROM book
WHERE title LIKE N'%Room%')
no matter which way we use we always must use common field to get
1
These queries cannot decide whether they are INNER JOINs or LEFT JOINs
– Strawberry
Nov 12 at 11:56
left join would be better while it need only number of orders
– sanaz amini
Nov 12 at 11:59
@Strawberry is right the first query will run as aINNER JOIN
instead.. The filter condition with aLEFT JOIN
should be in theON
clause instead.. it should beLEFT JOIN book ON isbn=bookISBN AND title LIKE N'%Room%'
...
– Raymond Nijland
Nov 12 at 12:00
add a comment |
In my opinion Join Would be better:
SELECT Count(*)
FROM orderDetails LEFT JOIN book ON isbn=bookISBN
WHERE title LIKE N'%Room%'
but if you must use sub query:
SELECT Count(*)
FROM orderDetails
WHERE bookISBN in (SELECT isbn
FROM book
WHERE title LIKE N'%Room%')
no matter which way we use we always must use common field to get
1
These queries cannot decide whether they are INNER JOINs or LEFT JOINs
– Strawberry
Nov 12 at 11:56
left join would be better while it need only number of orders
– sanaz amini
Nov 12 at 11:59
@Strawberry is right the first query will run as aINNER JOIN
instead.. The filter condition with aLEFT JOIN
should be in theON
clause instead.. it should beLEFT JOIN book ON isbn=bookISBN AND title LIKE N'%Room%'
...
– Raymond Nijland
Nov 12 at 12:00
add a comment |
In my opinion Join Would be better:
SELECT Count(*)
FROM orderDetails LEFT JOIN book ON isbn=bookISBN
WHERE title LIKE N'%Room%'
but if you must use sub query:
SELECT Count(*)
FROM orderDetails
WHERE bookISBN in (SELECT isbn
FROM book
WHERE title LIKE N'%Room%')
no matter which way we use we always must use common field to get
In my opinion Join Would be better:
SELECT Count(*)
FROM orderDetails LEFT JOIN book ON isbn=bookISBN
WHERE title LIKE N'%Room%'
but if you must use sub query:
SELECT Count(*)
FROM orderDetails
WHERE bookISBN in (SELECT isbn
FROM book
WHERE title LIKE N'%Room%')
no matter which way we use we always must use common field to get
edited Nov 12 at 11:56
answered Nov 12 at 11:54
sanaz amini
11
11
1
These queries cannot decide whether they are INNER JOINs or LEFT JOINs
– Strawberry
Nov 12 at 11:56
left join would be better while it need only number of orders
– sanaz amini
Nov 12 at 11:59
@Strawberry is right the first query will run as aINNER JOIN
instead.. The filter condition with aLEFT JOIN
should be in theON
clause instead.. it should beLEFT JOIN book ON isbn=bookISBN AND title LIKE N'%Room%'
...
– Raymond Nijland
Nov 12 at 12:00
add a comment |
1
These queries cannot decide whether they are INNER JOINs or LEFT JOINs
– Strawberry
Nov 12 at 11:56
left join would be better while it need only number of orders
– sanaz amini
Nov 12 at 11:59
@Strawberry is right the first query will run as aINNER JOIN
instead.. The filter condition with aLEFT JOIN
should be in theON
clause instead.. it should beLEFT JOIN book ON isbn=bookISBN AND title LIKE N'%Room%'
...
– Raymond Nijland
Nov 12 at 12:00
1
1
These queries cannot decide whether they are INNER JOINs or LEFT JOINs
– Strawberry
Nov 12 at 11:56
These queries cannot decide whether they are INNER JOINs or LEFT JOINs
– Strawberry
Nov 12 at 11:56
left join would be better while it need only number of orders
– sanaz amini
Nov 12 at 11:59
left join would be better while it need only number of orders
– sanaz amini
Nov 12 at 11:59
@Strawberry is right the first query will run as a
INNER JOIN
instead.. The filter condition with a LEFT JOIN
should be in the ON
clause instead.. it should be LEFT JOIN book ON isbn=bookISBN AND title LIKE N'%Room%'
...– Raymond Nijland
Nov 12 at 12:00
@Strawberry is right the first query will run as a
INNER JOIN
instead.. The filter condition with a LEFT JOIN
should be in the ON
clause instead.. it should be LEFT JOIN book ON isbn=bookISBN AND title LIKE N'%Room%'
...– Raymond Nijland
Nov 12 at 12:00
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53261390%2fsubquery-in-mysql-referring-to-a-book-by-its-isbn-rather-than-title%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
Please provide sample data.
– Gordon Linoff
Nov 12 at 11:40
Is using subquery necessary ? Is it some kind of homework ?
– Madhur Bhaiya
Nov 12 at 11:41
I know that there's debate about this kind of thing, but personally, I wouldn't use an ISBN as a primary key. My view is that the PK should have no meaning beyond the scope of the database - and, after all, a book sometimes has more than one ISBN!
– Strawberry
Nov 12 at 11:51
Please clarify whether you want to consider only those books where
title
is exactly'Room'
or contains'Room'
in it.– Madhur Bhaiya
Nov 12 at 12:01