Results 1 to 7 of 7

Thread: SQL Statements

  1. #1
    Join Date
    Apr 2004
    Posts
    9

    Unhappy Unanswered: SQL Statements

    Greetings,

    I'm trying to create querys for the first time and I'm getting errors when I run them against the SQL analyzer. Please let me know where my errors might be.

    1st Requirement:
    The first query joins two tables in the database and uses BETWEEN to restrict record selection.

    My result:
    SELECT *
    FROM Job_Table, Employees_Table
    WHERE Employee_ID Between 2 and 4;


    2nd Requirement:
    The second query joins two tables in the database and uses LIKE to restrict record selection.

    My result:
    SELECT Employee_ID
    FROM Job_Table, Employees_Table
    WHERE Employee_ID Like '3';

    3rd Requirement:
    The third query uses UNION of two record sets with different record selection criteria (exempt and not exempt employees) to produce a third table.

    My result:
    No clue where to start at this point :-(

    Any assistance would be greatly appreciated!!

    Warm regards,
    sqlhelp

  2. #2
    Join Date
    Jan 2004
    Location
    Singapore
    Posts
    89
    Why dont you try

    SELECT *
    FROM Job_Table, Employees_Table
    WHERE Employee_ID Between 2 and 4
    OR Employee_ID Like '3'
    Thanks and Regards,

    Praveen Pulikunnu

  3. #3
    Join Date
    Apr 2004
    Posts
    9
    Thanks so much for your response!

    The querys are required to be seperate at this time (per my instructor).

    Saying that, is what I already put together OK for the first two querys?

    Also, any info on how to get started on the Union query would be extremely appreciated!

    Thanks again!

    sqlhelp

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Before you do much at all, you need to establish the JOIN criteria between your two tables in each query. As it stands right now you seem to have a cartesian query.

    Before you can apply the UNION operator, you need to make the result sets the same in terms of the number of columns and their data types.

    As this is an assignment, you really need to do this yourself. I'll be happy to answer questions and throw in a few comments, but I won't do the assignment for you!

    Anyhow, welcome to dbforums and the world of databases in general!

    -PatP

  5. #5
    Join Date
    Apr 2004
    Posts
    9
    Thanks for your response!!

    So if I'm reading this correctly my first two querys will not work until a "JOIN" is included?

    Please advise.

    Thanks!

    sqlhelp

  6. #6
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Originally posted by sqlhelp
    So if I'm reading this correctly my first two querys will not work until a "JOIN" is included?
    Yes. When you just list the tables using SQL-89 syntax (which is what your examples show), you need to include conditions in the WHERE clause to limit the number of rows returned from the cartesian product. Otherwise if you have 500 employee rows and 2000 job rows, you'll get 1,000,000 combined rows!

    A better solution in general is to use the SQL-92 syntax. It uses JOIN clauses inside of the FROM clause to specify how to join the tables. It is preferred over the older syntax because it allows more specific criteria to be applied in the join process, which makes for better SQL source and more efficient engine performance.

    -PatP

  7. #7
    Join Date
    Apr 2004
    Posts
    9
    Thanks I'll see if I can incorporate this.

    Thanks for your feedback!

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •