SQl Joins

  • Hi,

    I am new to SQL. I have question regarding joins. Let us say i have 3 different tables

    Table Workplace

    Workplacecode (PK) autogenerated

    WRegn

    WorkplaceName

    NumOfEmployees

    EmpNumber (FK)

    address_id(FK)

    Table Employee

    EmpNum (PK)

    EmpName

    Table Address

    Address_id (PK)

    address1

    address2

    email

    phone1,

    phone2

    ...

    It is one to many relationship

    Company can have many employees

    Company can have one location address but can have multiple branch address.

    I want to populate table workplace using joins. I tried to use below query, but i don get result of employee table and address table in Workplace table. Please let me know where am i missing

    select Workplace.[WorkplaceCode], Workplace.WRegNum ,Workplace.[WorkplaceName],

    Workplace.[NumOfEmployees],Employee.[EmpName] as EmpNum,address.[OfficePhNum],address.[HomePhone],

    address.[Fax],address.[Address],address.[Address],address.homepage,address.[Email],Workplace.[Notes1],Workplace.[Notes2],Workplace.[Notes3],Workplace.[EmpNumber], Workplace.[AddressId] from Workplace

    left join Employee on Workplace.[EmpNumber] = employee.EmpNum

    left join Address on Workplace.[AddressId]=address.[Address_id]

  • Hi,

    i have doubt on table structure and design itself

    in Workplace if its Primary key

    then how can you put EmpNumber with it

    each Employee have different Workplacecode ? if yes then what is NumOfEmployees in Workplace and address_id

    if table fine according to you then please share some fake data so can give batter result

    as far as your query left join seems ok.

  • Hi,

    Thanks for your response.

    Workplacecode is autogenerated field and is primary key of Workplace table.

    EmpNumber is FK to Workplace (It is the individual emp number)

    NumOfEmployees is (count of employees in company)

    Fake data

    Workplace code-1

    WRegNum-098-098 (Company reg Number)

    WorkplaceName- Oracle Inc

    NumOfEmplouees-1000

    EmpName - 'John' (I get emp name from Employee table using EmpNumber which is FK to workplace table and using this only to get emp name)

    [address -x-102,abc, Newyork

    email -x@yz.com

    Ph: 01-87483284] (I get address details from Address table and address_id is FK to Workplace table using this only to get address details of company)

  • Okk now

    for 2nd Employee it would be

    Workplace code-2

    WRegNum-098-098 (Company reg Number) (It will be same )

    WorkplaceName- Oracle Inc

    NumOfEmplouees-1000

    EmpName - 'Bill' (I get emp name from Employee table using EmpNumber which is FK to workplace table and using this only to get emp name)

    [address -x-102,abc, Newyork

    email -x@yz.com

    Ph: 01-87483284] (I get address details from Address table and address_id is FK to Workplace table using this only to get address details of company)

    So Workplace code is changing with each and every employee also address and everything

    is what you are saying?

  • Is it what you want or just give me result which you want base on this data

    so that can give you query

    declare @workplace table(

    Workplacecode int identity(1,1),

    WRegn varchar(50),

    WorkplaceName varchar(50),

    NumOfEmployees int,

    EmpNumber int,

    address_id int

    )

    declare @Employee table(

    EmpNumber int identity(1,1),

    EmpName varchar(50)

    )

    declare @Address table(

    Address_id int identity(1,1),

    address1 varchar(50)

    )

    insert into @workplace values

    ('Test','Name',1000,1,1)

    ,('Test1','Name1',1000,2,1)

    ,('Test2','Name2',1000,3,3)

    insert into @Employee values

    ('Empname'),

    ('Empname1'),

    ('Empname2')

    insert into @Address values

    ('Add'),

    ('Add1'),

    ('Add2')

    select * from @workplace as w

    left join @Employee e on w.EmpNumber=e.EmpNumber

    left join @Address a on w.address_id=a.Address_id

  • Workplace code is like row count as auto increment field. ( i guess i should not make this as primary key otherwise it will map each employee to one row right)

    Instead i can make WRegn as PK for workplace where it can have 100 employees.

  • WorkplaceCodeWRegNumWorkplaceNameNumOfEmployeesEmpNameOfficePhNumAddressAddress_1Email

    105 099-1A Oracle 888 Wang ho 201543115 Hyundai-4-ga Hyundai-4-ga x@hdfc.com

    Above is the output when i run below query

    select distinct Workplace.[WorkplaceCode], Workplace.WRegNum ,Workplace.[WorkplaceName], Workplace.[NumOfEmployees],Employee.[EmpName],address.[OfficePhNum],address.[HomePhone], address.[Fax],address.[Address],address.[Address],address.homepage,address.[Email],Workplace.[Notes1],Workplace.[Notes2],Workplace.[Notes3] from Workplace

    left join Employee on Workplace.[EmpNumber] = employee.EmpNum

    left join Address on Workplace.[AddressId]=address.[Address_id]

    Also can you tell me if it is possible to disable Autoincrement field, i am using SQLite DB

  • yes create one more table

    1)WRegn Master where WRegnNum is PK having(WRegnNum PK,Wregname,Address_id,NumOfEmployees)

    2)Workplace (Workplacecode PK,WRegnNum,Empid)

    3)EmployeeMaster

    4)AddressMaster

    so Data will not repeat and save your size and everything

    now as far as join concern

    still i am not getting what you want

    you want all WRegn having employee or not having (use Left Join)

    you want all WRegn having employee (use Inner join)

    you want all employee having region or not(Use right join)

  • Thanks once again for you help. So i have to Normalize.

    Basically I am looking for company having all the details of employee working in tat company( i guess your 2nd point "you want all WRegn having employee (use Inner join)")

    as said this will include company details like registration number, address, number of employees and employee details.

    The main purpose of this query is to display in GUI table

  • yes then go for Inner join and yes do Normalize you table ,

    Like companymaster,workregmaster,empmaster,addressmaster,workdetail

    just take care that data should not repeat in table ,it will affect size and performance.

    you will get all you want using inner join.

  • Thank you very much for your help. I will follow as you said.

    Also i have one question, is it possible to disable AUTOINCREMENT field? I am using SQLite DB

  • I havent try that Sqllite db

    but if toy create table like

    CREATE TABLE test (ID INTEGER PRIMARY KEY AUTOINCREMENT,Othercol varchar(20))

    then try to remove AUTOINCREMENT from that and create table

  • Thanks for your help. 🙂

    Also, i have one question, how to insert FK using insert query

    Suppose i have table A containing FK

    table A

    Id_a

    b

    c

    id_m (FK)

    Now i want to do insert operation

    insert into A( id_a,b,c,id_m) values (1,2,3,(select id_m from B where id_m=5))

    Will above insert query work

  • SQLite doesnot allow to disable autoincrement field,so i thought if there is any way using SQL command.

  • insert into A( id_a,b,c,id_m) values (1,2,3,(select id_m from B where id_m=5))

    This will work unless and until

    select id_m from B where id_m=5 return only one(return multiple value not allowed gives error) value

    and for autoincreament in SQLLITE DB i also need to do google.

Viewing 15 posts - 1 through 15 (of 19 total)

You must be logged in to reply to this topic. Login to reply