If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Concept of this SQL query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-01-10, 13:59
mahendrakariya mahendrakariya is offline
Registered User
 
Join Date: Jan 2009
Posts: 6
Question Concept of this SQL query

My SQL query is as follows
Code:
select title
from book as B
where (select count(*)
           from book as T
           where (T.price > B.price) < 5
This query returns the 5 most expensive books. If I do >5, it is returning the 4 most expensive books. What is the concept behind this? Why is it so?
Reply With Quote
  #2 (permalink)  
Old 02-01-10, 14:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
Quote:
Originally Posted by mahendrakariya View Post
If I do >5, it is returning the 4 most expensive books.
no, it does not
Code:
CREATE TABLE book
( title VARCHAR(99)
, price INTEGER
);
INSERT INTO book VALUES
 ( 'book 1'       , 40 )
,( 'book two'     , 50 )
,( 'three'        , 30 )
,( 'book 5'       , 10 )
,( 'six     '     , 30 )
,( '7even'        , 40 )
,( 'eight'        , 15 )
,( 'book 9'       , 25 )
,( 'book 10'      , 35 )

SELECT price,title FROM book ORDER BY price DESC;

price	title
50	book two
40	book 1
40	7even
35	book 10
30	three
30	six
25	book 9
15	eight
10	book 5

select price,title
from book as B
where ( select count(*)
           from book as T
           where T.price > B.price ) < 5
order by price desc

price	title
50	book two
40	book 1
40	7even
35	book 10
30	three
30	six

select price,title
from book as B
where ( select count(*)
           from book as T
           where T.price > B.price ) > 5
order by price desc

price	title
25	book 9
15	eight
10	book 5
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-01-10, 23:54
mahendrakariya mahendrakariya is offline
Registered User
 
Join Date: Jan 2009
Posts: 6
Quote:
no, it does not
That is because some of the books have the same price. If we have different price for each book, the result is something different.

Code:
CREATE TABLE book
( title VARCHAR(99)
, price INTEGER
);

INSERT INTO book VALUES
 ( 'book 1'       , 40 )
,( 'book two'     , 50 )
,( 'three'        , 60 )
,( 'book 5'       , 70 )
,( 'six     '     , 80 )
,( '7even'        , 90 )
,( 'eight'        , 105 )
,( 'book 9'       , 115 )
,( 'book 10'      , 125 )

SELECT price,title FROM book ORDER BY price DESC;

price	title
125	book 10
115	book 9
105	eight
90	7even
80	six
70	book 5
60	three
50	book two
40	book1

select price,title
from book as B
where ( select count(*)
           from book as T
           where T.price > B.price ) < 5
order by price desc

price	title
125	book 10
115	book 9
105	eight
90	7even
80	six

select price,title
from book as B
where ( select count(*)
           from book as T
           where T.price > B.price ) > 5
order by price desc

price	title
60	three
50	book two
40	book1

select price,title
from book as B
where ( select count(*)
           from book as T
           where T.price > B.price ) < 3
order by price desc

price	title
125	book 10
115	book 9
105	eight

select price,title
from book as B
where ( select count(*)
           from book as T
           where T.price > B.price ) > 3
order by price desc

price	title
80	six
70	book 5
60	three
50	book two
40	book1
Reply With Quote
  #4 (permalink)  
Old 02-02-10, 00:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
Quote:
Originally Posted by mahendrakariya View Post
select price,title
from book as B
where ( select count(*)
from book as T
where T.price > B.price ) > 5
order by price desc

price title
60 three
50 book two
40 book1
you have proved here, yourself, that your first post was incorrect

in your first post, you said "If I do >5, it is returning the 4 most expensive books."

clearly, 60-50-40 is ~not~ the 4 most expensive

see what i mean?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On