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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:33:03+00:00 2026-06-06T06:33:03+00:00

I have two tables that I want to join together. Table1 Year, ID, Theme,

  • 0

I have two tables that I want to join together.

Table1

Year, ID, Theme,

Table2

First, Last, WeekID, Date, Affiliation

I want to use this command

SELECT * 
FROM Table1 
CROSS JOIN Table2 
WHERE Table1.ID = 5 
    AND WHERE Table2.Date >= 1/1/2011 
    AND Table2.Date <= 12/30/2011 
ORDER BY Asc

What I wanted to happen was all the rows and Columns from Table 1 are selected where the ID column contains an int value of 5. In Table2 all of the columns and rows should be selected that are within the given date range.

I would like to know if the WHERE clause should be coming after the CROSS JOIN clause as I have above. Also Should I remove the second WHERE keyword and instead have the following command.

SELECT * 
FROM Table1 
CROSS JOIN Table2 
WHERE Table1.ID = 5 
    AND Table2.Date >= 1/1/2011 
    AND Table2.Date <= 12/30/2011 
ORDER BY Asc

My third question is the tricky one. Can 2 different WHERE clauses be used in a single command like this but be applied to separate tables? Meaning can I have WHERE Table1 (*Condition*) AND WHERE Table2 (*Condition*) when I am joining the tables?

I think I could easily resolve the entire problem by creating 2 separate SQL commands 1 for each table and just avoid the JOIN and 2 WHERE clauses. Would this be something you would recommend?

The final result would look something like this

Table3

ID, Year, Theme, WeekID, Date, First, Last, Affiliation

Then the cells of would be order in ascending order based on date.

a sample table is below

Table3

ID     Year     Theme     WeekID     Date          First      Last    Affiliation
5      2011     Stuff1    1          01/09/2011    Foo        Bar     Baz Inc
5      2011     Stuff2    2          01/14/2011    Flum       Baz     Bar Inc
5      2011     Stuff3    3          04/15/2011    Bar        Flum    Bub Inc
5      2011     Stuff4    4          05/01/2011    Bar        Foo     FlumBub Inc 
5      2011     Stuff5    5          08/16/2011    Bub        Baz     Foo Inc 
  • 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-06T06:33:05+00:00Added an answer on June 6, 2026 at 6:33 am

    Q: I would like to know if the WHERE clause should be coming after the CROSS JOIN clause as I have above.

    A: Yes, that’s the correct placement of the WHERE clause.

    Q: Also Should I remove the second WHERE keyword and instead have the following command.

    A: Yes, the WHERE clause can appear only once in a simple SELECT statement. Each subquery can have its own WHERE clause, but that’s really still one WHERE clause per SELECT.

    Q: My third question is the tricky one. Can 2 different WHERE clauses be used in a single command like this but be applied to separate tables? Meaning can I have WHERE Table1 (Condition) AND WHERE Table2 (Condition) when I am joining the tables?

    A:The WHERE keyword can appear only once per SELECT. You are free to include predicates on any table.


    Also, to answer some additional questions you didn’t ask…

    You need to provide an expression, or list of expressions, in the ORDER BY clause. The default order is ASC, so this keyword is most frequently omitted.

    The predicate on the Date column of Table2 appears to represent date literals. (As they are in your statement, they appear to represent an integer value, derived by a sequence of division operations.

    The literals should be explicitly converted to DATETIME (to match the datatype of the Date column). An explicit CONVERT isn’t required by SQL Server, but absent the conversion, you really want those to be represented as strings in a canonical (unambiguous) format. (Does ‘3/5/2012’ represent March 5th, or May 3rd?)

    SQL Server DATTIME datatype stores both date and time components. Typically, when users ask for an end date, they are meaning any time on that day as well. To take into account that a DATETIME value of ‘2011-12-30 09:30:00’ is NOT <= ‘2011-12-30’, we would normally code a test of LESS THAN midnight of the following day.

    It’s very good practice to qualify references to columns. This is frequently done with table aliases. Table aliases are not required, but they are a familiar pattern, and can make reading a statement much easier. That’s especially true when the table names are fully qualified mydatabase.schema.MyLongAndUnWeILDyTblName, and fully qualified column names used in more complex expressions can make deciphering the expression very tedious. (Not really an issue in your case, but it’s a pattern we follow even on simple statements.)

    Also, best practice is to avoid using the * in the SELECT list (unless you are selecting from an inline view or CTE within the statement). Instead list the specific expressions you want returned. For testing and development, using * is fine. Aside from those minor issues, your statement looks fine.

    (Avoiding the * and qualifying column names avoids PROBLEMS in the future which can occur, for example, when a new column is added to a table, giving rise to an “ambiguous column” exception which wasn’t there before. (We like to be able to add columns without running a full regression test of every SQL statement in the application.)

    Given all that information you didn’t ask for… in our shop, the statement to return your specified resultset would be formatted like this:

    SELECT t1.ID
         , t1.Year
         , t1.Theme
         , t2.WeekID
         , t2.Date
         , t2.First
         , t2.Last
         , t2.Affiliation
      FROM dbo.Table1 t1
     CROSS
      JOIN dbo.Table2 t2
     WHERE t1.ID = 5 
       AND t2.Date >= CONVERT(DATETIME,'2011-01-01',20)
       AND t2.Date <  CONVERT(DATETIME,'2011-12-31',20)
     ORDER
        BY t1.ID
         , t1.Year
         , t1.Theme
         , t2.WeekID
         , t2.Date
         , t2.First
         , t2.Last
         , t2.Affiliation
    

    In a later comment, Derek noted that the Date column is VARCHAR. In that unfortunate case, we need to know the format the dates are represented in.

    If the string representation are not in a canonical format, the VARCHAR comparison will yield undesirable results.

    (Observe that the character string ‘3/5/2011’ is NOT BETWEEN ‘1/1/2011′ AND ’12/30/2011’.)

    There are significant advantages to using the DATETIME datatype to store date values. If that is not possible (for whatever insidious reason someone comes up with), and the strings are not in a canonical format, then the predicate should really be something more like:

    AND CONVERT(DATETIME,t2.Date,101) >= CONVERT(DATETIME,'01/01/2011',101)
    AND CONVERT(DATETIME,t2.Date,101) <  CONVERT(DATETIME,'12/31/2011',101)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that I need to join... I want to join table1
I have two tables. I want to join them in a way that only
I want to join two tables together and have ONLY the data in Table
I have two tables that I am outer joining together: Post left outer join
I have two tables with date and id fields. I want to join on
I have two tables that needs to be linked together, I want to count
i have these two queries, that i want to join together, but am not
I have two tables that I want to join into one table and use
I have a two mysql tables that I want to merge together in one
I have two tables in a derby database that I want to query together.

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.