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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:18:32+00:00 2026-05-27T09:18:32+00:00

I have the next table: code, year, day I want to find the code

  • 0

I have the next table: code, year, day

I want to find the code of the courses that was taken in 2000 in Sunday and Monday.

So I wrote the next query:

Select DISTINCT dbo.Course.Code
From dbo.Course
Where dbo.Course.CourseYear = 2000 AND 
EXISTS (Select * From dbo.course Where (dbo.course.day = 'Sunday' and dbo.Course.CourseYear = 2000))and 
EXISTS (Select * From dbo.course Where (dbo.course.day = 'Monday' and dbo.Course.CourseYear = 2000))

It’s return me the courses that were given in 2000, in Sunday or Monday. Why?

  • 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-27T09:18:33+00:00Added an answer on May 27, 2026 at 9:18 am

    You need to enforce that the same course record is being checked in both exists statement.
    Your exists statement only ensure that there exists any course that was offered in sunday and any course that was offered on monday

    Revising your query:

    Select DISTINCT dbo.Course.Code
    From dbo.Course c
    Where dbo.Course.CourseYear = 2000 AND 
    EXISTS (Select * From dbo.course Where (dbo.course.day = 'Sunday' and dbo.Course.CourseYear = 2000 and dbo.course.Code = c.Code))and 
    EXISTS (Select * From dbo.course Where (dbo.course.day = 'Monday' and dbo.Course.CourseYear = 2000 and dbo.course.Code = c.Code))
    

    A better written version

    select distinct c.code
    from
    dbo.course c
    inner join dbo.course c2 on c.code = c2.code
    where
    c.CourseYear = 2000
    and c2.CourseYear = 2000
    and c.Day = 'Monday'
    and c2.Day = 'Sunday'
    

    Also note that your schema is not normalized. You should have two tables:

    1. Course (CourseId, Code)
    2. CourseInstance (CourseId, Day, Year)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the next code: b.Text = myDataContext.purchases.Count().ToString(); the b is label that i
I have a table that is storing a list of rules. In my code,
I have the next code to display my countries table in a select box:
I have a code that loads data from Excel into SQLServer table. It works
I have a table code_prices that looks something like this: CODE | DATE |
I have two tables next to each other and problem is that I want
I have the following function. $(function() { $(.sectionHeader:gt(0)).click(function() { $(this).next(.fieldset).slideToggle(fast); }); $(img[alt='minimize']).click(function(e) { $(this).closest(table).next(.fieldset).slideUp(fast);
I have a table story_category in my database with corrupt entries. The next query
I'm going to start of by noting that I have next to no python
I have the next query that I run via SQLCMD.EXE use [AxDWH_Central_Reporting] GO EXEC

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.