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

The Archive Base Latest Questions

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

I have three mysql tables that I would like to extract some information from,

  • 0

I have three mysql tables that I would like to extract some information from, the tables are:

  • Videos – represents a video with a score.
  • Tags – contains a global list of tags.
  • VideoTags creates an association between a Video and a Tag.

What I want to do is find the videos with the highest points for each tag. There are many videos with the same tag, but my result set will have the
same number of rows as there are tags. The end goal is to have a list of the best video (by points) for each unique tag (tags being a topic prefixed with a hash).

My SQL noob attempt at achieving this is as follows:

  SELECT video.id AS video_id, video.owner_id, MAX(video.points), tag.id AS tag_id
    FROM Videos video, VideoTags videotag, Tags tag
   WHERE video.id = videotag.video_id
     AND videotag.tag_id = tag.id
     AND tag.content LIKE '#%'
GROUP BY tag.id

Here’s the schema and sample data:

DROP TABLE IF EXISTS `Video`;
CREATE TABLE `Video` (
  `id` varchar(24) NOT NULL default '',
  `owner_id` varchar(24) NOT NULL default '',
  `points` DOUBLE NOT NULL default 0
);

DROP TABLE IF EXISTS `Tags`;
CREATE TABLE `Tags` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content` varchar(32) NOT NULL default ''
PRIMARY KEY (id)
);

DROP TABLE IF EXISTS `VideoTags`;
CREATE TABLE `VideoTags` (
  `video_id` varchar(24) NOT NULL default '',
  `tag_id` int(11) NOT NULL
);

INSERT INTO Videos (id,owner_id,points) VALUES ('owner-x-video-a','owner-x', 20);
INSERT INTO Videos (id,owner_id,points) VALUES ('owner-x-video-b','owner-x', 15);
INSERT INTO Videos (id,owner_id,points) VALUES ('owner-y-video-k','owner-y', 12);
INSERT INTO Videos (id,owner_id,points) VALUES ('owner-y-video-l','owner-y', 17);
INSERT INTO Videos (id,owner_id,points) VALUES ('owner-y-video-m','owner-y', 44);

INSERT INTO Tags (id, content) VALUES (111, '#topic-1');
INSERT INTO Tags (id, content) VALUES (222, '#topic-2');

INSERT INTO VideoTags (video_id,tag_id) VALUES ('owner-x-video-a',111);
INSERT INTO VideoTags (video_id,tag_id) VALUES ('owner-x-video-b',111);
INSERT INTO VideoTags (video_id,tag_id) VALUES ('owner-y-video-k',111);
INSERT INTO VideoTags (video_id,tag_id) VALUES ('owner-y-video-l',222);
INSERT INTO VideoTags (video_id,tag_id) VALUES ('owner-y-video-m',222);

What I expect to see is:

video_id          owner_id    MAX(video.points)  tag_id
owner-x-video-a   owner-x     20                 111
owner-y-video-m   owner-y     44                 222

but what I get is:

video_id          owner_id    MAX(video.points)  tag_id
owner-x-video-a   owner-x     20                 111
owner-y-video-l   owner-y     44                 222

Unfortunately the video_id for the second row is not what I expected, as owner-y-video-l
does not have 44 points, rather it has 17 so would not be the highest scoring video for
the tag with id 222.

Any Masters of the SQL Universe out there that can help me out? Thanks a million 🙂

  • 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-12T11:53:14+00:00Added an answer on June 12, 2026 at 11:53 am

    You want the groupwise maximum:

    SELECT * FROM Video JOIN (
    
      SELECT   VideoTags.tag_id, MAX(points) points
      FROM     Video JOIN VideoTags ON Video.id = VideoTags.video_id
      GROUP BY VideoTags.tag_id
    
    ) t USING (points) JOIN Tags ON t.tag_id = Tags.id
    

    See it on sqlfiddle.

    Note that this query returns all videos having the maximum number of points within each tag, so more than one record will be returned for tied tags. If you wish to return only one record in such situations, please specify how to determine the video that should be returned.

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

Sidebar

Related Questions

I have a MySQL query that: gets data from three tables linked by unique
I have three MySQL InnoDB tables: Debtors Companies Private individuals Now I would like
I have three tables I would like to join to pull off a MySQL
Hoping someone can provide some mysql advice... I have 2 tables that look like
I have three mysql table from same database Db1. Three tables have following columns.
i have three tables i would like to link in this one query. The
I have two MySQL tables and I would like to know if there is
I have three tables in MySQL that are related, yet not technically linked to
I have three tables... Given the following tables I would like to know how
I have three different tables in my MySQL database. table users: (id, score, name)

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.