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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:46:27+00:00 2026-06-17T04:46:27+00:00

My table is: CREATE TABLE Rating ( rid INTEGER GENERATED BY DEFAULT AS IDENTITY

  • 0

My table is:

CREATE TABLE Rating
(
    rid INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    mid INTEGER FOREIGN KEY REFERENCES Movie(movieId) ON DELETE CASCADE, 
    uid INTEGER FOREIGN KEY REFERENCES User(id) ON DELETE CASCADE,
    rating INTEGER NOT NULL, 
);

I want to select the mid with most average rating:

select avg(r.rating) from rating r

witch returns the average. I want to return the mid`s with the most average rating. Any ideas how to do that?

> UPDATE

the other two tables:

CREATE TABLE User(
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    passwordhash VARCHAR(100) NOT NULL,
    fullname VARCHAR(50) NOT NULL,
    birthday DATE NOT NULL,
    joindate DATE NOT NULL,
    email VARCHAR(50) NOT NULL,
    picturepath VARCHAR(256) NOT NULL,
    favouritemovie VARCHAR(50) NOT NULL,
    favouritecategory INTEGER REFERENCES category(id),
    isDeleted BOOLEAN NOT NULL
);

CREATE TABLE Movie
(
    movieId INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    moviePath VARCHAR(500) NOT NULL
);
  • 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-17T04:46:28+00:00Added an answer on June 17, 2026 at 4:46 am

    From your comments:

    calculate average rating for each mid (with a GROUP BY mid), then choose maximum and return the mid

    So first step, calculate average for each mid:

    select mid, 
           avg(rating) as avg_rating
    from rating
    group by mid;
    

    Now choose the maximum:

    select max(avg_rating)
    from (
      select avg(rating) as avg_rating
      from rating
      group by mid
    ) as mar
    

    Now combine these:

    select ar.mid, mar.max_avg
    from (
        select mid, 
               avg(rating) as avg_rating
        from rating
        group by mid
      ) as ar
      join (
        select max(avg_rating) as max_avg
        from (
          select avg(rating) as avg_rating
          from rating
          group by mid
        ) as t
      ) as mar
      on ar.avg_rating = mar.max_avg;
    

    SQLFiddle example (using Postgres, but works with HSQLDB as well): http://sqlfiddle.com/#!12/e208a/8

    It’s not the most simple solution but quering on grouped data never is. Using the TOP construct as shown by Luther is going to be much faster. The only drawback with the TOP 1 is that you won’t notice if two movies have the same average rating.

    Edit: Just to expand a little bit beyond HSQLDB. In a database that supports window functions (PostgreSQL, Oracle and many others), this type of question is very easy:

    select *
    from (
      select mid, 
             avg(rating) as avg_rating,
             dense_rank() over (order by avg(rating) desc) as rnk
      from rating
      group by mid
    ) t
    where rnk = 1;
    

    It is especially easy to find the second highest, third highest and so on (where rnk = 2, where rnk = 3) which is really complicated using those nested queries – but a lit bit easier when using the TOP/LIMIT aproach.

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

Sidebar

Related Questions

I have three tables: CREATE TABLE Movie ( movieId INTEGER GENERATED BY DEFAULT AS
Here is my database: create table Movie ( mID smallint auto_increment primary key, title
Having a following SQL table: create table users_posts_ratings_map ( postId integer not null references
I have a three SQL tables: create table users ( id serial primary key,
I have a table CREATE TABLE `messages` ( `uid` BIGINT NOT NULL , `mid`
I have two tables called Reviews and Levels. CREATE TABLE [dbo].[Reviews]( [ReviewID] [int] IDENTITY(1,1)
Table: CREATE TABLE `test` ( `uid` int(11) unsigned NOT NULL AUTO_INCREMENT, `rating` smallint(5) unsigned
Using Linq to SQL and the autogeneration capabilities of DBML, foreign key relationships create
I have these relationships: User(uid:integer,uname:varchar) , key is uid Recipe(rid:integer,content:text) , key is rid
create database Exer4 use Exer4 create table customer ( cus_code int, constraint PK_customer primary

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.