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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:04:37+00:00 2026-06-03T12:04:37+00:00

Here is the most relevant part of my database schema: create table TEST (

  • 0

Here is the most relevant part of my database schema:

create table TEST (
    ID integer not null,
    NAME text not null,
    constraint PK_TEST primary key (ID),
    constraint UNQ_TEST_NAME unique (NAME)
);

create table SESSION (
    ID integer not null,
    constraint PK_SESSION primary key (ID)
);

create table SESSION_TEST (
    SESSION_ID integer not null,
    TEST_ID integer not null,
    ORDINAL integer not null,
    constraint PK_SESSION_TEST primary key (SESSION_ID, TEST_ID),
    constraint FK_SESSION_TEST_SESSION_ID foreign key (SESSION_ID) references SESSION (ID) on delete cascade,
    constraint FK_SESSION_TEST_TEST_ID foreign key (TEST_ID) references TEST (ID) on delete cascade,
    constraint UNQ_SESSION_TEST_SESSION_ID_ORDINAL unique (SESSION_ID, ORDINAL)
);

There are SESSIONS that consist of multiple TESTS. TESTs in SESSIONS have ORDINALs (are ordered). SESSION_TEST is a link table for a many-to-many relationship: one test can be a part of multiple sessions, and one session consists of multiple tests (but one test can be in a session only once, which is the PK).

I am having problems writing an SQL statement that would return true (or 1 actually, as I am using SQLite) for a test with a given ID, if there is a session that has that test and only that test (in other words, the session consists of only one test, the one I am looking for).
For example:

TEST:
ID|NAME
1|aaa
2|bbb
3|ccc

SESSION:
ID
1
2
3
4

SESSION_TEST:
SESSION_ID|TEST_ID|ORDINAL
1|1|1
1|2|2
2|1|1
3|3|1

SESSION with ID = 1 has two TESTs, and SESSIONs 2 and 3 have one test each. I would need to have a select that would return 1/true for inputs 1 and 3, but 0 for 2 (as this TEST is only in SESSION 1, but it’s not the only one).

(Sorry about the title, I really didn’t know how to put it in a few sentences and make it clear!).

  • 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-03T12:04:38+00:00Added an answer on June 3, 2026 at 12:04 pm
    RETURN EXISTS SELECT SESSION_ID FROM 
    SESSION_TEST
    WHERE TEST_ID = @id
    AND ORDINAL = 1
    AND SESSION_ID NOT IN (SELECT SESSION_ID FROM SESSION_TEST WHERE ORDINAL > 1);
    

    The RETURN EXISTS syntax might be TSQL only, but you get the picture. If the query returns rows, return true, if not return false.

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

Sidebar

Related Questions

This is most likely not an easy one but here is the situation: I
EDIT Sorry I forgot the most important part here. Each key can have more
The most relevant question here on StackOverflow was this , which still doesn't answer
Ok, i've read most relevant questions asked here previously on this but I still
I'm trying to parse values with ANTLR. Here's the relevant part of my grammar:
This is probably a trivial question for most but here goes... I am using
We have decided to work with the Insert Framework Here We'll most likely update
Here's a piece of code that takes most time in my program, according to
Here is a quick overview of the controllers functionality in most of the application:
Question about logics here: What's the most elegant way to make the menu appear/disappear

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.