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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:10:31+00:00 2026-06-16T18:10:31+00:00

I am not to savvy on SQL queries but I am trying to build

  • 0

I am not to savvy on SQL queries but I am trying to build a query to remove any data (reported comments) based on a join to another table (user) using an ID to match, here is a sample schema below:

create table tbl_reported_comment(id int, commentId int, reported_by_userid int);

insert tbl_reported_comment values
  (1, 1, 101),
  (2, 2, 131),
  (3, 3, 101),
  (4, 4, 101),
  (5, 5, 24),
  (6, 6, 201),
  (7, 7, 1),
  (8, 8, 24),
  (9, 9, 23),
  (10, 10, 16),
  (11, 11, 31);

Create table tbl_user(userId int, Username varchar(50));

insert tbl_user values
  (1, 'admin'),
  (101, 'test1'),
  (131, 'test2'),
  (24, 'test3'),
  (201, 'test4');

What I am trying to achieve in this instance is the following:

Remove any data in the tbl_reported_comment table where the
[reported_by_userid] column doesn’t exist as an [userId] in the user
table

Here is a link to the SQLFIDDLE with this sample schema: SQLFiDDLE. I am using SQL Server as the database.

Many thanks,

  • 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-16T18:10:33+00:00Added an answer on June 16, 2026 at 6:10 pm

    How about this?

      DELETE FROM dbo.tbl_reported_comment
      OUTPUT DELETED.id, DELETED.commentId, DELETED.reported_by_userid
      WHERE reported_by_userid NOT IN 
                 (SELECT UserID FROM dbo.tbl_user)
    

    This will delete those rows and it will output those rows it deleted to the screen (in SQL Server Management Studio) so you can see what was deleted.

    And Ben is right – once you’ve done this, you should establish a foreign key relationship between those two tables to avoid zombie data like this in the future!

    -- make your "UserID" column NOT NULL
    ALTER TABLE dbo.tbl_user
    ALTER COLUMN UserID INT NOT NULL
    
    -- so that we can use it as the PRIMARY KEY for that table!
    ALTER TABLE dbo.tbl_user
    ADD CONSTRAINT PK_user PRIMARY KEY CLUSTERED(UserID)
    
    -- so that we can then establish a FK relationship between those two tables....
    ALTER TABLE dbo.tbl_reported_comment
    ADD CONSTRAINT FK_reported_comment_user
    FOREIGN KEY (reported_by_userid) REFERENCES dbo.tbl_user(UserID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not math savvy, but this Mathematica plot caught my eye and I was
I am not too savvy with JSON so any help would be great... I
I'm not a developer, but savvy enough to look at HTML, CSS and PHP
I'm not so savvy with MYSQL, so my apologies in advance is this is
Not ever sure if it can be done, but is it possible to use
Not quite sure how to go about describing what it is I'm trying to
Not to sound like a koan, but just wondering if there are definite rules
Not sure if im just being stupid or something but here goes i work
Not sure how to ask this, but i'll give it a try: I have
Some of our end users are not IT savvy and may not know how

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.