user2690891
user2690891

Reputation: 1

Can Some one please help me solve this subquery in mysql?

***Rules: --- Do not use a join of any kind and do not use a correlated subquery. --- Do not use a comma join. --- If you use a join, you will get no points for that task. A From clause will reference only one table. --- DO not use a variable.

***Here is my problem: Display the book id and title for any books which someone has ordered and the book is both an SQL book and a atabase book. Use the Topic_id to filter for DB and SQL. Sort by the book_id.

***My answwer:

 select book_id, title
  From a_bkinfo.books
  Where book_id IN
    (Select book_id
     From a_bkinfo.book_topics
     Where topic_id = 'DB')
 And book_id in
    (Select book_id
     From a_bkinfo.book_topics
     Where topic_id = 'SQL')
     order by book_id; 

I received -2 on this problem. My teachers response: -2 * did not check that this book has been ordered.

***Here are the relevant tables.* Tables:

 ---- create order_details ------
 create  table   a_bkorders.order_details (
 order_id          integer          not null 
, order_line        integer          not null 
, book_id           integer          not null 
, quantity          integer          not null 
, order_price       numeric(6,2)     not null   
, constraint bk_orderline_pk         primary key (order_id, order_line)
, constraint bk_orderline_order_fk   foreign key (order_id) 
             references a_bkorders.order_headers(order_id) on delete cascade
, constraint bk_orderline_book_fk    foreign key (book_id )  
           references a_bkinfo.books(book_id)
, constraint bk_quantity_ck          check (quantity > 0) 
, constraint bk_ordprice_ck          check (order_price >= 0) 

)engine = INNODB;

---- create book_topics ----
 create  table a_bkinfo.book_topics (
 book_id           integer          not null
, topic_id          varchar(5)       not null  
, constraint bk_book_topics_pk        primary key (book_id, topic_id)
, constraint bk_books_topics_fk      foreign key(topic_id) 
           references a_bkinfo.topics(topic_id)
, constraint bk_books_id_fk          foreign key(book_id) 
           references a_bkinfo.books(book_id)

)engine = INNODB;

------ create books ------
create table a_bkinfo.books (
book_id           integer          not null
, title             varchar(75)      not null 
, publ_id           integer          null
, year_publd        integer          not null
, isbn              varchar(17)      null
, page_count        integer          null  
, list_price        numeric(6,2)     null  
, constraint bk_books_pk             primary key (book_id)
, constraint bk_books_publ_fk        foreign key(publ_id) 
           references a_bkinfo.publishers  (publ_id)
, constraint book_id_range           check (book_id > 1000)
, constraint bk_page_count_ck        check (page_count >= 0)
, constraint bk_price_ck             check (list_price >= 0)             
, constraint bk_books_year_ck        check (year_publd >= 1850)
)engine = INNODB;

Upvotes: 0

Views: 116

Answers (1)

spencer7593
spencer7593

Reputation: 108400

and book_id in 
    ( select d.book_id 
        from a_bkorders.order_details d
       where d.quantity > 0 
    )

Display the ID and title of the book(s) with the largest number of sales; include ties. For this query, use the total quantity sold. when determining the sales of a book

Without any JOIN operations or correlated subqueries, this will return the specified resultset:

SELECT b.book_id
     , b.title
  FROM a_bkinfo.books b
 WHERE b.book_id IN
       ( 
         SELECT d.book_id
           FROM a_bkorders.order_details d
          GROUP BY d.book_id
         HAVING SUM(d.quantity) =
                (
                  SELECT MAX(t.tot_quantity)
                    FROM (  
                           SELECT s.book_id
                                , SUM(s.quantity) AS tot_quantity
                             FROM a_bkorders.order_details s
                            GROUP BY s.book_id
                         ) t
                )
       )

N.B. MySQL does not enforce CHECK constraints.

Upvotes: 1

Related Questions