Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7495759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:18:21+00:00 2026-05-29T18:18:21+00:00

I have an error in my query sub query. I been trying to work

  • 0

I have an error in my query sub query. I been trying to work this out for hours until I recently given up. Anyone have expertise in oracle DBA?

How can I write this query? I need a sub query with use of aggregates max() and sum().

select at.au_id, 
    ((price*sales)*royalty_rate), 
    max(royalty_share) 
from royalties, titles, author_titles at 
group by royalty_rate 
having  max(royalty_share) = (select max((price*sales)*royalty_rate) 
    from author_titles, titles, royalties 
    group by rollup (royalty_rate, royalty_share, au_id));

Here is what i have, I need to use group by rollup or group by cube

Here’s the question:
[1] List the author(s) that has/have received the greatest compensation for writing a book,
and compensation received. If more than 1 author is involved, list the total compensation
and compensation for each author. Hint: A publisher assigns an advance and a royalty
rate to a book. This information is available in the Royalties table. The royalty rate is
applied to the book’s revenue to determine the royalty payment. The amount of the
advance and royalty payment made to each author is determined by the royalty_share
field in the author_titles table. This solution involves a subquery to find the maximum
royalty payment.

You can copy and paste tables right into what ever you like to use. Thanks.
My tables are:

DROP TABLE authors;
CREATE TABLE authors
 (
 au_id    CHAR(3)     NOT NULL,
 fname VARCHAR(15) NOT NULL,
 lname VARCHAR(15) NOT NULL,
 phone    VARCHAR(12)         ,
 address  VARCHAR(20)         ,
 city     VARCHAR(15)         ,
 state    CHAR(2)             ,
 zip      CHAR(5)             ,
CONSTRAINT authors_pk PRIMARY KEY (au_id)
);
INSERT INTO authors VALUES('A01','Sarah','Buchman','718-496-7223','75 West 205 St','Bronx','NY','10468');
INSERT INTO authors VALUES('A02','Wendy','Heydemark','303-986-7020','2922 Baseline Rd','Boulder','CO','80303');
INSERT INTO authors VALUES('A03','Hallie','Hull','415-549-4278','3800 Waldo Ave, #14F','San Francisco','CA','94123');
INSERT INTO authors VALUES('A04','Klee','Hull','415-549-4278','3800 Waldo Ave, #14F','San Francisco','CA','94123');
INSERT INTO authors VALUES('A05','Christian','Kells','212-771-4680','114 Horatio St','New York','NY','10014');
INSERT INTO authors VALUES('A06',' ','Kellsey','650-836-7128','390 Serra Mall','Palo Alto','CA','94305');
INSERT INTO authors VALUES('A07','Paddy','O''Furniture','941-925-0752','1442 Main St','Sarasota','FL','34236');



DROP TABLE titles;
CREATE TABLE titles
  (
  title_id   CHAR(3)      NOT NULL,
  title VARCHAR(40)  NOT NULL,
  genre       VARCHAR(10)          ,
  pub_id     CHAR(3)      NOT NULL,
  pages      INTEGER              ,
  price      DECIMAL(5,2)         ,
  sales      INTEGER              ,
  pubdate    DATE                 ,
  contract   SMALLINT     NOT NULL,
  CONSTRAINT titles_pk PRIMARY KEY (title_id), 
  CONSTRAINT Titles_pub_fk FOREIGN KEY (Pub_id)
    REFERENCES Publishers (Pub_id)
  );

INSERT INTO titles VALUES('T01','1977!','history','P01',107,21.99,566,DATE '2000-08-01',1);
INSERT INTO titles VALUES('T02','200 Years of German Humor','history','P03',14,19.95,9566,DATE '1998-04-01',1);
INSERT INTO titles VALUES('T03','Ask Your System Administrator','computer','P02',1226,39.95,25667,DATE '2000-09-01',1);
INSERT INTO titles VALUES('T04','But I Did It Unconsciously','psychology','P04',510,12.99,13001,DATE '1999-05-31',1);
INSERT INTO titles VALUES('T05','Exchange of Platitudes','psychology','P04',201,6.95,201440,DATE '2001-01-01',1);
INSERT INTO titles VALUES('T06','How About Never?','biography','P01',473,19.95,11320,DATE '2000-07-31',1);
INSERT INTO titles VALUES('T07','I Blame My Mother','biography','P03',333,23.95,1500200,DATE '1999-10-01',1);
INSERT INTO titles VALUES('T08','Just Wait Until After School','children','P04',86,10.00,4095,DATE '2001-06-01',1);
INSERT INTO titles VALUES('T09','Kiss My Boo-Boo','children','P04',22,13.95,5000,DATE '2002-05-31',1);
INSERT INTO titles VALUES('T10','Not Without My Faberge Egg','biography','P01',NULL,NULL,NULL,NULL,0);
INSERT INTO titles VALUES('T11','Perhaps It''s a Glandular Problem','psychology','P04',826,7.99,94123,DATE '2000-11-30',1);
INSERT INTO titles VALUES('T12','Spontaneous, Not Annoying','biography','P01',507,12.99,100001,DATE '2000-08-31',1);
INSERT INTO titles VALUES('T13','What Are The Civilian Applications?','history','P03',802,29.99,10467,DATE '1999-05-31',1);


DROP TABLE author_titles;
CREATE TABLE author_titles
  (
  au_id         CHAR(3)      NOT NULL,  
  title_id      CHAR(3)      NOT NULL,
  au_order      SMALLINT     NOT NULL,
  royalty_share DECIMAL(5,2) NOT NULL,
  CONSTRAINT authors_titles_pk PRIMARY KEY (au_id, title_id),
  CONSTRAINT author_titles_au_fk FOREIGN KEY (au_id) 
    REFERENCES Authors (au_id),
  CONSTRAINT author_titles_titles_pk FOREIGN KEY (title_id) 
    REFERENCES Titles (title_id)
  );

INSERT INTO author_titles VALUES('A01','T01',1,1.0);
INSERT INTO author_titles VALUES('A01','T02',1,1.0);
INSERT INTO author_titles VALUES('A05','T03',1,1.0);
INSERT INTO author_titles VALUES('A03','T04',1,0.6);
INSERT INTO author_titles VALUES('A04','T04',2,0.4);
INSERT INTO author_titles VALUES('A04','T05',1,1.0);
INSERT INTO author_titles VALUES('A02','T06',1,1.0);
INSERT INTO author_titles VALUES('A02','T07',1,0.5);
INSERT INTO author_titles VALUES('A04','T07',2,0.5);
INSERT INTO author_titles VALUES('A06','T08',1,1.0);
INSERT INTO author_titles VALUES('A06','T09',1,1.0);
INSERT INTO author_titles VALUES('A02','T10',1,1.0);
INSERT INTO author_titles VALUES('A03','T11',2,0.3);
INSERT INTO author_titles VALUES('A04','T11',3,0.3);
INSERT INTO author_titles VALUES('A06','T11',1,0.4);
INSERT INTO author_titles VALUES('A02','T12',1,1.0);
INSERT INTO author_titles VALUES('A01','T13',1,1.0);




DROP TABLE royalties;
CREATE TABLE royalties
  (
  title_id     CHAR(3)      NOT NULL,
  advance      DECIMAL(9,2)         ,
  royalty_rate DECIMAL(5,2)         ,
  CONSTRAINT royalties_pk PRIMARY KEY (title_id),
  constraint roy_Titles_fk FOREIGN KEY (title_id) REFERENCES Titles (title_id)
  );

Alter table royalties disable constraint roy_titles_fk ;

INSERT INTO royalties VALUES('T01',10000,0.05);
INSERT INTO royalties VALUES('T02',1000,0.06);
INSERT INTO royalties VALUES('T03',15000,0.07);
INSERT INTO royalties VALUES('T04',20000,0.08);
INSERT INTO royalties VALUES('T05',100000,0.09);
INSERT INTO royalties VALUES('T06',20000,0.08);
INSERT INTO royalties VALUES('T07',1000000,0.11);
INSERT INTO royalties VALUES('T08',0,0.04);
INSERT INTO royalties VALUES('T09',0,0.05);
INSERT INTO royalties VALUES('T10',NULL,NULL);
INSERT INTO royalties VALUES('T11',100000,0.07);
INSERT INTO royalties VALUES('T12',50000,0.09);
INSERT INTO royalties VALUES('T13',20000,0.06);

Alter table royalties enable constraint roy_titles_fk ;

commit;
DROP TABLE publishers;
CREATE TABLE publishers
  (
  pub_id   CHAR(3)     NOT NULL,
  pname  VARCHAR(20) NOT NULL,
  city     VARCHAR(15) NOT NULL,
  state    CHAR(2)             ,
  country  VARCHAR(15) NOT NULL,
  CONSTRAINT publishers_pk PRIMARY KEY (pub_id)
  );
INSERT INTO publishers VALUES('P01','Abatis Publishers','New York','NY','USA');
INSERT INTO publishers VALUES('P02','Core Dump Books','San Francisco','CA','USA');
INSERT INTO publishers VALUES('P03','Schadenfreude Press','Hamburg',NULL,'Germany');
INSERT INTO publishers VALUES('P04','Tenterhooks Press','Berkeley','CA','USA');

This is very hard for me 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T18:18:22+00:00Added an answer on May 29, 2026 at 6:18 pm

    Let’s do this in steps. It is frequently easier to tackle a task in discrete steps. Based on what you tried, it seems clear that you are struggling with some of the basic concepts. I hope that this helps you to learn the concepts

    -- 1) First you know you are looking for authors, so select all the authors
    SELECT a.au_id
      FROM authors a
    
    -- 2) Build up all of your table joins since we know we will need data from all these tables
    -- the author_titles maps authors to titles, and titles map to royalties.  Use the foreign keys provided
    SELECT a.au_id
      FROM authors a
     INNER JOIN author_titles at ON a.au_id = at.au_id
     INNER JOIN titles t         ON at.title_id = t.title_id
     INNER JOIN royalties r      ON t.title_id = r.title_id
    
    -- 3) Figure out the formula for an author's compensation
    -- 3a) Notice that the royalties table has NULLs for advance and royalty_rate.  Convert these to 0
    -- 3b) Notice that the titles table has NULLs for price and sales.  Convert these to 0
    SELECT a.au_id
           ,total_compensation = (at.royalty_share * NVL(r.advance, 0)) + (at.royalty_share * NVL(r.royalty_rate,0) * NVL((t.price * t.sales),0))
      FROM authors a
     INNER JOIN author_titles at ON a.au_id = at.au_id
     INNER JOIN titles t         ON at.title_id = t.title_id
     INNER JOIN royalties r      ON t.title_id = r.title_id
    
    -- 4) Summarize the compensation for each author by adding the group by author id
    SELECT a.au_id
           ,total_compensation = SUM((at.royalty_share * NVL(r.advance, 0)) + (at.royalty_share * NVL(r.royalty_rate,0) * NVL((t.price * t.sales),0)))
      FROM authors a
     INNER JOIN author_titles at ON a.au_id = at.au_id
     INNER JOIN titles t         ON at.title_id = t.title_id
     INNER JOIN royalties r      ON t.title_id = r.title_id
    GROUP BY a.au_id
    
    -- 5) Find the the highest compensation.  It is the MAX of the previous query
    SELECT highest_compensation = MAX(s.total_compensation)
      FROM
        (SELECT a.au_id
               ,total_compensation = SUM((at.royalty_share * NVL(r.advance, 0)) + (at.royalty_share * NVL(r.royalty_rate,0) * NVL((t.price * t.sales),0)))
          FROM authors a
         INNER JOIN author_titles at ON a.au_id = at.au_id
         INNER JOIN titles t         ON at.title_id = t.title_id
         INNER JOIN royalties r      ON t.title_id = r.title_id
         GROUP BY a.au_id) s
    
    -- 6) Find the author(s) that match the highest compensation by combining 4) and 5)
    SELECT t1.au_id
      FROM
        (SELECT a.au_id
               ,total_compensation = SUM((at.royalty_share * NVL(r.advance, 0)) + (at.royalty_share * NVL(r.royalty_rate,0) * NVL((t.price * t.sales),0)))
          FROM authors a
         INNER JOIN author_titles at ON a.au_id = at.au_id
         INNER JOIN titles t         ON at.title_id = t.title_id
         INNER JOIN royalties r      ON t.title_id = r.title_id
         GROUP BY a.au_id) t1
    WHERE t1.total_compensation =
        (SELECT highest_compensation = MAX(s.total_compensation)
          FROM
            (SELECT a.au_id
                   ,total_compensation = SUM((at.royalty_share * NVL(r.advance, 0)) + (at.royalty_share * NVL(r.royalty_rate,0) * NVL((t.price * t.sales),0)))
              FROM authors a
             INNER JOIN author_titles at ON a.au_id = at.au_id
             INNER JOIN titles t         ON at.title_id = t.title_id
             INNER JOIN royalties r      ON t.title_id = r.title_id
             GROUP BY a.au_id) s)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query and I have an error: images = Image.find_by_sql('PREPARE stmt FROM
I have this query and I get error Operand should contain 1 column(s), whats
Please help me to find out error in my SQL query. I have created
I get the following error in the query below: #1064 - You have an
I have this error message: Msg 8134, Level 16, State 1, Line 1 Divide
I have this error that is keeping me from moving forward. I basically have
I wrote the following query, but it appears to have an error, and I
I have the following SQL Query I'm executing and I'm trying to find why
I have a query with a sub query in the select that needs to
I have written the following query: Private Sub Size_Sqft_BeforeUpdate(Cancel As Integer) Me!Size_Sqft = Nz(Me!Size_Sqft,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.