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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:35:04+00:00 2026-05-20T05:35:04+00:00

My problem is I want to delete records from two tables, deleting the records

  • 0

My problem is I want to delete records from two tables, deleting the records that are a result of the following query:

SELECT AVG(pr.rating) AS rating_average
FROM products p 
INNER JOIN product_ratings pr 
ON pr.product_id = p.product_id  
GROUP BY p.product_id 
HAVING AVG(pr.rating) < 3

The above query shows average ratings of a product that are less than 3, I want to delete products and their associated ratings of all the results from the above query.

I looked at DELETE FROM product_ratings, products WHERE EXISTS (//above query), but this didn’t work, I’ve been trying various DELETE statements to no avail.

I have read the following, and still cannot find a solution: SQL: DELETE Statement & SQL: EXISTS Condition.

The tables are products and product_ratings, with the following structure:

products
--------
product_id [PK] | link | ...

product_ratings
---------------
rating_id [PK] | rating | product_id

Appreciate any help, as well as links to reference material to better understand how it’s done.

EDIT: Apologies for not stating what RDBMS I’m using, It’s MySQL

EDIT2: A bit confused now, @Martin’s example doesn’t use a temp table like the other answers, I assume this is because of my vague question not stating with RDBMS I was using?

  • 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-20T05:35:05+00:00Added an answer on May 20, 2026 at 5:35 am

    You have now added the MySQL tag. In that case this might do the job for you.

    DELETE products,
           product_ratings
    FROM   products,
           product_ratings
    WHERE  product_ratings.product_id = products.product_id
           AND product_ratings.product_id IN 
     (SELECT product_id
            FROM   (SELECT p.product_id
                    FROM   products p
                           LEFT JOIN product_ratings pr
                             ON pr.product_id = p.product_id
                    GROUP  BY p.product_id
                    HAVING COALESCE(AVG(pr.rating), 0) < 3) T) 
    

    MySQL does support a multiple table DELETE syntax. The derived table is to get around the issue where it doesn’t allow mutating (update or delete target) tables to be referenced in a sub query (it materializes the result into a temporary table).

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

Sidebar

Related Questions

i m new here, i want to update and delete the records from the
I'm having a problem in mysql. I want to delete 20 rows from a
Problem: I want to list n number of games from each genre (order not
Problem I want to create an application that can be extended somehow by programmers.
Following problem: I want to render a news stream of short messages based on
I have two tables called TableA and TableB. TableA has the following fields: TableA_ID
I have a strange problem when deleteting records using linq, my suspicion is that
I have a table in my database which has duplicate records that I want
I have two records that are the same in a table (entered by mistake).
This isn’t trully a problem, I just want to understand. Considering the following code:

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.