locked
Select Query ? RRS feed

  • Question

  • Hello

    I have two tables.

    tablename: tableA

    coulumname: id, studentname

    records: 1,  nameA

                 2, name B

    tablename: tableB

    columname: educationid, studentid, education

    records:       1,      1        ,   educationA

                       2,      1        , educationB

                       1,       2       , educationA

    Filterparameter is educationid. When it is 1, result should be like below

    studentname

    name A

    name B

    There is 1 and 2 in tableB as studentid.

    When filterparameter are 1 and 2, result should be like below

    student

    name A

    name B

    Because there are records(edicationid) as 1 and 2 as parameter.

    when parameter is 2, result should be like below

    nameA

    studentid is 1 at tableB.

    Can you type Select Query for this?

    Thanks.

    Monday, August 19, 2013 8:21 PM

Answers

  • Thank you.

    But i need to SELECT QUERY about my sample.

    SELECT DISTINCT a.StudentName
    FROM tabA AS a
    	INNER JOIN tabB AS b
    		ON a.Id = b.StudentId
    WHERE b.EducationId IN (1,2) --Your EducationId param.


    Narsimha

    Monday, August 19, 2013 10:44 PM

All replies

  • Look at the following example for JOINing tables:

    http://www.sqlusa.com/bestpractices/fourtablejoin/

    Just follow it. Here is the trick for a simple multiple-table SELECT: you need to JOIN tables on matching FOREIGN KEYs and PRIMARY KEYs.

    For your example the basic JOIN:

    SELECT *

    FROM TableA a

    INNER JOIN TableB b

      ON a.ID = b.StudentID


    Kalman Toth Database & OLAP Architect sqlusa.com
    New Book / Kindle: Pass SQL Exam 70-461 & Job Interview: Programming SQL Server 2012



    • Edited by Kalman Toth Monday, August 19, 2013 8:53 PM
    Monday, August 19, 2013 8:33 PM
  • Thank you.

    But i need to SELECT QUERY about my sample.

    Monday, August 19, 2013 8:45 PM
  • Thank you.

    But i need to SELECT QUERY about my sample.

    SELECT DISTINCT a.StudentName
    FROM tabA AS a
    	INNER JOIN tabB AS b
    		ON a.Id = b.StudentId
    WHERE b.EducationId IN (1,2) --Your EducationId param.


    Narsimha

    Monday, August 19, 2013 10:44 PM
  • >> I have two tables. <<

    Really?? 

    Please post DDL, so that people do not have to guess what the keys (you have none!), constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules (you have no idea). Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. 

    This is minimal polite behavior on SQL forums. Your narrative is also wrong as well as rude. You dd not even know that rows are not records! Why not read just one book on RDBMS and the forum posting rules? 

    CREATE TABLE Students
    (student_id CHAR(10) NOT NULL PRIMARY KEY,
     student_name VARCHAR(35) NOT NULL);

    CREATE TABLE Education
    (student_id CHAR(10) NOT NULL 
       REFERENCES Students (student_id)
      ON DELETE CASCADE
      ON UPDATE CASCADE,
      awarded_degree VARCHAR(12) NOT NULL,
     PRIMARY KEY (student_id, awarded_degree));

    >> Filter_parameter is education_id. When it is 1, result should be like below <<

    No, not in a valid RDBMS! There would be no such attribute in a normalized database. 


    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking in Sets / Trees and Hierarchies in SQL

    Tuesday, August 20, 2013 1:24 AM