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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:38:18+00:00 2026-06-18T01:38:18+00:00

I have 2 tables: CREATE TABLE plans (id int, benefit varchar(5), clip_state int); INSERT

  • 0

I have 2 tables:

CREATE TABLE plans
    (id int, benefit varchar(5), clip_state int);

INSERT INTO plans
    (id, benefit, clip_state)
VALUES
    (1, 'A', 1),
    (2, 'A', 0),
    (3, 'B', 0),
    (4, 'C', 0);

CREATE TABLE clip_states
    (state varchar(2), clip_state int);

INSERT INTO clip_states
    (state, clip_state)
VALUES
    ('LA', 1),
    ('FL', 0);

Please noted that the clip_state is either 0 or 1 and the data model allows to query for one or no plan in plans table from 2 information: benefit & state. First, using benefit condition, we can filter the plans table to 2 rows at most, one with clip_state = 0, and one = 1. Then by using state and join with the clip_states table, we can reduce the result to one row (or zero) by checking:

  • If the state is in clip_states table, make sure the clip_state is matched between 2 tables. If no match, no result returned.
  • If the state doesn’t exist in clip_states, it only matches with the row in plans table that has clip_state = 0.

Here is my query that does the trick:

SELECT id, p.clip_state, benefit
FROM plans p
LEFT JOIN clip_states cs ON STATE IN ('LA')
WHERE benefit = 'A' AND
(p.clip_state = cs.clip_state OR (p.clip_state = 0 AND cs.clip_state IS NULL));

As you can see, the left join is quite strange because it doesn’t join on the relationship between the 2 tables. So, my question is:

  • Is it normal to have such kind of join?
  • Is there any better solution (clean & performance)?

You can check my solution at: http://sqlfiddle.com/#!1/8912d/53

Updated 1: I have updated the text of the question above.

Updated 2: More information

  1. If a state isn’t in clip_states table, its clip_state implicitly equals to 0. Otherwise, its clip_state is in the table.
  2. From the two given info: benefit and state, find a row in plans table where plans.clip_state = clip_state of that given state. Of course if it doesn’t match, no row returned.
  • 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-18T01:38:19+00:00Added an answer on June 18, 2026 at 1:38 am

    I guess I have to use the following query as the solution. I have checked the query plan and see it one step better than my query in the question. The idea is to narrow down plan first, then join with the clip_states table.

    SELECT *
    FROM (
        SELECT DISTINCT id, clip_state, benefit
        FROM plans
        WHERE benefit = 'A') p
    LEFT JOIN clip_states cs ON state IN ('LA')
    WHERE (p.clip_state = cs.clip_state OR (p.clip_state = 0 AND cs.clip_state IS NULL));
    

    Thanks all.

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

Sidebar

Related Questions

Strange situation. If I have these tables: CREATE TABLE t1 (id INT, title VARCHAR(20),
Here's the scenario: I have 2 tables: CREATE TABLE dbo.API_User ( id int NOT
I have the following tables CREATE TABLE `files` ( `fileid` int(11) NOT NULL AUTO_INCREMENT,
I have the following tables: CREATE TABLE title ( booktitle VARCHAR( 60 ), title_id
I have the following tables: create table TableA ( Id int primary key identity,
For example I have the following tables resulting from: CREATE TABLE A (Id int,
I have 3 tables with the following schema: CREATE TABLE `devices` ( `device_id` int(11)
Suppose I have the following tables: CREATE TABLE Game ( GameID INT UNSIGNED NOT
i have 2 tables CREATE TABLE if not exists tag_name( + tagid INTEGER PRIMARY
I have 2 tables: CREATE TABLE `product_det` ( `id` bigint(12) unsigned NOT NULL AUTO_INCREMENT,

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.