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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:52:57+00:00 2026-05-31T14:52:57+00:00

I have 2 tables (I am using T-SQL) Table1 ID First_Name Last_Name Phone —

  • 0

I have 2 tables (I am using T-SQL)

Table1

ID  First_Name Last_Name Phone
--  ---------- --------- --------------
1   John       Smith     (643) 434-4343 
2   Dave       Miller    (543) 344-3432
3   Tiffany    Ovally    (434) 343-6598
4   Dan        Davis     (534) 342-9876
5   Mike       Kolis     (454) 345-3434


Table2

Iden   FirstN     LastN      PhoneN
----   ------     -----      ------
J-09   Tiffany    Ovally    (434) 343-6598
K-98   Dan        Davis     (534) 342-9876
W-03   Dave       Miller    (543) 344-3432
C-34   Mike       Kolis     (454) 345-3434

I need to check that key values from Table1 DO NOT exist in table2

I am doing the following

    IF NOT EXISTS(SELECT * FROM Table2 t2
                  WHERE t2.FirstN = (Select First_Name from Table1 where ID = @ID)
                  AND t2.LastN  = (Select Last_Name from Table1 where ID = @ID)  
                  AND t2.PhoneN = (Select Phone from Table1 where ID = @ID)        
                  )
   BEGIN
    .... 
  END

Not sure if there is a more efficient way to do this as I am doing a subquery for each field..

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-05-31T14:52:59+00:00Added an answer on May 31, 2026 at 2:52 pm

    Your query is looking for records in Table2 that do not match the record in Table1 with an ID of @ID. Using an INNER JOIN here would be better than using the multiple subqueries:

    IF NOT EXISTS (SELECT *
                   FROM FROM Table1 t1
                   INNER JOIN Table2 t2 
                      ON  t1.First_Name = t2.FirstN
                      AND t1.Last_Name = t2.LastN
                      AND t1.Phone = t2.PhoneN
                  WHERE t1.ID = @ID)
    BEGIN
        ....
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge database with 100's of tables and stored procedures. Using SQL
I´m using linq to sql and I have a lot of tables with foreign
I am using VB.NET with LINQ to MS SQL. I have two following tables.
I have two tables: student Columns first_name last_name teacher_accounts Columns fname lname I have
I'm using SQL Server 2008 and I have a table with three columns: Length
[using SQL Server 2005] I have a table full of users, I want to
I am using SQL Server 2005. I have a table with a text column
If I have a table field named 'description', what would be the SQL (using
I am using Microsoft SQL Server. I have a Table which had been updated
I have a table in the database that I'm retrieving using LINQ to SQL,

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.