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 8710335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:36:12+00:00 2026-06-13T04:36:12+00:00

I need to create a function that will give the average of the book

  • 0

I need to create a function that will give the average of the book prices based on books’ subjects. The rules for this function are:

a) If the argument is null, return a null

b) If the argument does not match any topic id that we have in the topics table, return a value of -2

c) If the argument matches a topic id that we have in the topics table, but we do not have any books with
that topic, return a value of -1

create function AvgPriceByTopic(
p_subject varchar(20))
RETURNS decimal(8,2)
begin
declare v_avgPrice decimal(8,2);
declare v_avgListPrice decimal(8,2);

if p_subject is null then
    set v_avgPrice := null;
elseif exists (
        select avg(list_price) into v_avgListPrice
        from books 
        where topic_id = p_subject
        group by book_id
        limit 1 ) then
    set v_avgPrice := v_avgListPrice;
else 
    set v_avgPrice := -2;
end if;

return v_avgPrice;
end;
#

I’m getting an error that states:

 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
 corresponds to your MySQL server version for the right syntax to use near 
  'into v_avgListPrice from books' at line 11

Any suggestions to get rid of this error? Sometimes I have trouble with the syntax… Thanks ahead of time.

  • 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-06-13T04:36:13+00:00Added an answer on June 13, 2026 at 4:36 am

    First you need to move the query that calculates the average outside the EXISTS like @dan suggested.

    But I see a problem on that query: it’s grouping by book_id and limiting and then tries to limit it to the first row. First of all, that would not result in the average price by topic, because it’s not grouping by topic, it would just result each book’s price. Try the following:

    delimiter $$
    
    create function AvgPriceByTopic(
        p_subject varchar(20))
    RETURNS decimal(8,2)
    begin
    declare v_avgPrice decimal(8,2);
    declare v_avgListPrice decimal(8,2);
    
    if p_subject is null then
        set v_avgPrice := null;
    elseif not exists (select * from topics where topic_id = p_subject) then
        set v_avgPrice := -2;
    elseif not exists (select * from books where topic_id = p_subject) then
        set v_avgPrice := -1;
    else
        select avg(list_price) into v_avgListPrice
        from books
        where topic_id = p_subject
        group by topic_id;
    end if;
    
    return v_avgPrice;
    end$$
    
    delimiter ;
    

    Remember to change the delimiter so that MySQL does not interpret the semi-colons within the function as the end of the create function statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a function that uses a loop. This function will open
I need to create a function in JavaScript, that will write some text (which
I need to create a function that, among other things, spawns a child process.
I need to create a function that takes month and year as parameters and
I need to create a function that receives a string and checks if the
I need to create a javascript function that checks if it has been a
i am trying to create sql procedure or function that need to find duplicate
I need to create a function which rounds decimal numbers like this: Round($32.95, 0)
I'm trying to create a function that will check to make sure all the
I'm trying to create a function that will run through an array and collect

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.