Retrieving data from the table in SQL Server 2008 - sql

I have 4 tables
table1 (id, stateId(fk), name, carId(fk))
table2 (stateId(pk), state, countryId(fk))
table3 (countryId, country, currency)
table4 (carId, car)
I want to retrieve name, state, country, car from the above tables through stored procedure using joins. And if some other easy way then please tell.
thanks.

A simple JOIN should do:
CREATE PROCEDURE schema.YourStoredProcedureName
AS
SELECT t1.name, t2.state, t3.country, t4.car
FROM table1 t1
JOIN table2 t2 ON t1.stateId = t2.stateId
JOIN table3 t3 ON t2.countryId = t3.countryId
JOIN table4 t4 ON t1.carId = t4.carId
GO

No other easy method. Join is basic and easy (http://msdn.microsoft.com/en-us/library/ms191517(v=sql.105).aspx)
select tbl1.name, tbl2.state, tbl3.country, tbl4.car
From table1 tbl1
inner join table2 tbl2 on tbl1.stateid =tbl2.stateid
inner join table3 tbl3 on tbl2.countryid = tbl3.countryid
inner join table4 tbl4 on tbl1.carId = tbl4.carId

Related

Inner join the tables where the value of one column is different. But the meaning of the values are same

Table t1
Table t2
I tried running the query
select CountyCode, ContactPerson
from table1 t1
inner join select * from table 2 t2 on t1.CountyCode[1] = Code[0]
Any one help me please.
Use this syntax:
select CountyCode, ContactPerson
from t1
inner join t2 on t1.CountyCode = t2.Code

INSERT INTO ... SELECT with more SELECT columns

Thank you for your time.
I have a INSERT INTO ... SELECT setup, but I also want to filter using extra columns (that do not exist in the table I am inserting into)
For example:
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3,
t4.filter_col
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';
table1 only has columns t1_col1, t1_col2 and t1_col3 so when I attempt to run this it fails as expected:
ERROR: INSERT has more expressions than target columns
So my question is, how can I still include the filter, but specify which columns from my SELECT statement should be used in the INSERT INTO statement.
Many thanks for any help!
you have to same number of column in select ,so remove t4.filter_col from selection
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';
Note: you are not bound to select all columns those you used in join or filter
It's almost right, but you select one column too much. Try this:
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';

triple join using sqlite

Let's say we have 3 tables:
table1: firstname, id
table2: id, lastname
table3: lastname, height
and we want to display: firstname, height
I'd likely use full join for that:
select firstname, height from
(select firstname, lastname ln from table1
full join table2 on table1.id=table2.id)
full join table3 on ln=table3.lastname
but in sqlite, full and right joins do not exist and I need to some how use union to get it.
Any ideas, how to do it?
Query:
SELECT firstname, height
FROM table1 INNER JOIN table2 ON table1.id=table2.id
INNER JOIN table3 ON table2.lastname=table3.lastname
you need a JOIN here between three tables
SELECT T1.firstname, T2.lastname, T3.height
FROM Table1 T1
JOIN Table2 T2
on T1.id = T2.id
JOIN Table3 T3
on T3.lastname = T2.lastname

SQL join order and conditions

I've got 3 tables that I want to join and filter on some conditions.
I've first wrote this query:
select * from table1 t1
left join (select * from table2 where table2.fieldX=...) t2
on t1.id_12=t2.id_12
left join table3
on t2.id_23=t3.id_23
where t1.fieldY=...
Then I wanted to make it looks like more canonical by rewritting it like that:
select * from table1 t1
left join table2 t2
on t1.id_12=t2.id_12
left join table3
on t2.id_23=t3.id_23
where table2.fieldX=...
and t1.fieldY=...
But it does not give the same result.
I dont't understand why...
Do you?
Thanks in advance.
When you put table2.fieldX=... in the where clause you eliminate all rows from table1 that do not have a corresponding row in table2. Effectively you are changing the left join into an inner join.
Instead, you can apply the table2 filter in the join itself:
SELECT
*
FROM
Table1 t1
LEFT JOIN Table2 t2 ON t1.id_12 = t2.id_12 AND t2.fieldX = ...
LEFT JOIN Table3 t3 ON t2.id_23 = t3.id_23
WHERE
t1.fieldY = ...
Did you add the inner select where on the second query?
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
on t1.id_12=t2.id_12
LEFT JOIN table3
on t2.id_23=t3.id_23
WHERE t1.fieldY=...
and t2.fieldX=...

How to join 3 tables by a single sql query?

I have 3 tables.
Table1: Group_Code, Group_Name,companyID;(PK: Group_Code)
Table2: PartyID,GroupID,companyID;(FK: GroupID, PK:PartyID)
Table3: VendorID, companyID;(FK:VendorID)
I want to fetch Group_Name from Table1 for all VendorID of Table3. How can I do this?
here i write a code. But it shows "Syntex error in FROM clause." My database is in ms access.
select Group_Name from Table1 join Table2 on Table1.Group_Code= Table2.GroupID
join Table3 on Table2.PartyID=Table3.VendorID
try this !!!
SELECT table1.group_name FROM (table1
INNER JOIN ON table1.group_code=table2.groupid)
INNER JOIN table3 ON table2.partyid=table3.vendorid
GROUP BY table1.group_name
select Group_Name from Table1
join Table2 on Table1.Group_Code = Table2.GroupID
join Table3 on Table2.PartyID = Table3.VendorID
SELECT table1.group_name FROM table1 join table2
ON table1.group_code=table2.groupid
join table3 ON table2.partyid=table3.vendorid
error becoz you didnt take the group name DB instance ?
You Can do this :
select Table1.Group_Name, Table3.VendorID from Table1 join Table2 on Table1.Group_Code= Table2.GroupID join Table3 on Table2.PartyID =Table3.VendorID
If you are data has been stored in proper relationship. the query should get you going. :)
use this code for question,
Select Table1.Group_Name from ((Table1
left join Table2 on Table1.Group_Code=Table2.GroupID)
left join Table3 on Table2.PartyID=Table3.VendorID)