Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQl Joins


SQl Joins

Author
Message
prashantvp79
prashantvp79
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 10
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]
BriPan
BriPan
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 296
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.
prashantvp79
prashantvp79
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 10
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)
BriPan
BriPan
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 296
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?
BriPan
BriPan
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 296
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


prashantvp79
prashantvp79
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 10
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.
prashantvp79
prashantvp79
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 10
WorkplaceCode WRegNum WorkplaceName NumOfEmployees EmpName OfficePhNum Address Address_1 Email
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
BriPan
BriPan
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 296
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)
prashantvp79
prashantvp79
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 10
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
BriPan
BriPan
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 296
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search