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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:30:24+00:00 2026-06-17T08:30:24+00:00

I have a table as follows: create table table1(id integer,firstname text,lastname text); id firstname

  • 0

I have a table as follows:

create table table1(id integer,firstname text,lastname text);

id firstname lastname

====================
1 ben taylor
2 rob taylor
3 rob smith
4 rob zombie
5 peter smith
6 ben smith
7 peter taylor

Now I have another table create table table2(id integer,position integer);

id position

===========
1 5
1 9
2 6
3 7
6 2

I want to select rows with a lastname , where the lastname must be shared by ben and rob and firstnames must be ben and rob.
Now I also want the position of ben to be one lesser than that of rob, hence the result would be :

id firstname lastname position

===========================
1 ben taylor 5
2 rob taylor 6

What should the sql query be ?

  • 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-17T08:30:25+00:00Added an answer on June 17, 2026 at 8:30 am

    To get the last name:

    select lastname
    from names n join
         position p
         on n.id = p.id
    where firstname in ('ben', 'rob')
    group by lastname
    having count(distinct firstname) = 2 and
           1+max(case when firstname = 'ben' then p.position end) = max(case when firstname = 'rob' then p.position end)
    

    You can then get the original list with:

    select n.*, p.position
    from names n join
         position p
         on n.id = p.id
    where firstname in ('ben', 'rob') and
          lastname in (select lastname
                       from names n join
                            position p
                            on n.id = p.id
                       where firstname in ('ben', 'rob')
                       group by lastname
                       having count(distinct firstname) = 2 and
                              1+max(case when firstname = 'ben' then p.position end) = max(case when firstname = 'rob' then p.position end)
                      )
    

    I think the following query answers your question, with the caveat that this combines the names into one row:

    select nben.*, p.position, nrob.*, prob.position
    from names nben join
         positions p
         on nben.id = p.id and
            nben.firstname = 'ben' join
         names nrob
         on nrob.firstname = 'rob' and
            nrob.lastname = nben.lastname join
         positions prob
         on nrob.id = prob.id and
            p.position = prob.position - 1
    

    Also, this is untested.

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

Sidebar

Related Questions

i have a table as follows: create table table1(id integer,firstname text,lastname text); firstname lastname
I have a table in sql as follows: CREATE TABLE Reserves( sid INTEGER, bid
I have days table created as follows create table days( + day_id integer primary
I have a table defined as follows: CREATE TABLE [dbo].[T]( [FileID] [bigint] NOT NULL,
Let us suppose I have a table created as follows: create table `test_table` (
I have a simple table in Sybase, let's say it looks as follows: CREATE
I have a table say scope_test as follows: create table scope_test ( id int
In my database, assume we have a table defined as follows: CREATE TABLE [Chemical](
Hi I have an interesting problem. I Have an Employee Table AS Follows CREATE
I have an sqlite database structured as follows: CREATE TABLE IF NOT EXISTS Patient

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.