query in sql server 2005 - sql

there are two tables ...know i need
1st condition:
all the records in table
2 nd condition:
In table2 i need only records which have data
...i want one query for the aove two conditions...

SELECT
*
FROM Table1 t1
INNER JOIN Table2 t2 on t1.PK = t2.FK
This will return all rows in table1 that have at least one corresponding row in table2
But if you want all rows from t1 no matter what then this may be what you want
SELECT
*
FROM Table1 t1
LEFT JOIN Table2 t2 on t1.PK = t2.FK
Finally, As I dont know the structure in place perhaps table1 and table2 have similar structures. If this is true perhaps you may want a union of the two
SELECT
*
FROM Table1 t1
UNION ALL
SELECT
*
FROM Table2 t2

Related

How to fetch * records from a table which are not present in other two tables

I want to fetch all records with all columns of a table, Records which are not in the other 2 tables. Please help.
I have tried below query, it is working fine for comparing one column. But I want to compare 5 columns.
select * from A
WHERE NOT EXISTS
(select * from B b where b.id=a.id) AND
NOT EXISTS
(select * from C c where c.id=a.id)
A general solution might look like:
SELECT t1.*
FROM table1 t1
WHERE NOT EXISTS (SELECT 1 FROM table2 t2 WHERE t2.id = t1.t2_id) AND
NOT EXISTS (SELECT 1 FROM table3 t3 WHERE t3.id = t1.t3_id);
This assumes that you want to target table1 for records, ensuring that no matches can found in table2 and table3.
I prefer this approach:
SELECT t1.*
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.id = t2.t1_id
LEFT JOIN table3 AS t3
ON t1.id = t3.t1_id
WHERE t2.id IS NULL
AND t3.id IS NULL;
While this might be a bit less intuitive than using sub queries I think odds of making mistakes with aliases are less likely as in the example below, which happens more often than you might expect:
SELECT *
FROM table1
WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE id = id) AND
NOT EXISTS (SELECT 1 FROM table3 WHERE id = id);
To your question about checks on 5 columns, that can still be done using either of these methods by adding conditions either in the left joins or in the where clause of each sub query.

How to query the specific vales in columns form different tables(SQLite)?

I have two tables, table1 and table2. table1 has 'id' column. table2 has 'id' and 'quantity' columns. I want to compare the same values under 'id' columns from both tables and show the value under 'quantity' column form table2.
The simple logic is "select id=1 from table1 compare with select id=1 from table2, then show the quantity value from table2". Is there any way to query this statement?
Join the tables:
SELECT *
FROM
table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
Using INNER JOIN will cause the results to show only rows where the same ID is present in both tables. If you want to show all rows from table 1 and matching rows from table 2 plus blanks for any rows where there is no table 2 id for a particular table 1 id, then use LEFT JOIN instead of INNER JOIN.
If you only want certain columns in your output you can list them:
SELECT t1.id, t2.quantity
...
etc
More about joins - for future learning
In most other database systems there exists the corollary of LEFT join; RIGHT join. If you want all rows from table 2 plus matching rows from table 1, and blanks where there is no related table 1 row, use RIGHT JOIN. SQLite doesn't support RIGHT joins, so you'll have to rewrite your query (swap the table names around) so t2 is on the LEFT. It's the table name that matters, not the order of appearance in the ON section:
--all rows from t1 plus any matching rows from t2
SELECT *
FROM
table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id
--all rows from t2 plus any matching rows from t1
SELECT *
FROM
table2 t2
LEFT JOIN table1 t1
ON t1.id = t2.id
Eventually you'll come across a FULL [OUTER] JOIN which is "all rows from t1 and any matching from t2, plus any additional rows from t2 and their possible matches from t1". SQLite doesn't support this either, but it can be emulated. Ordinarily it's emulated with a LEFT JOIN UNION RIGHT JOIN, but as SQLite doesn't support RIGHT, you'll have to emulate it with two LEFT joins, one with the tables swapped round:
--all rows from t1 plus any matching rows from t2
SELECT *
FROM
table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id
UNION
SELECT *
FROM
table2 t2
LEFT JOIN table1 t1
ON t1.id = t2.id
Cor. All that for the sake of being able to say this in e.g. SQLServer:
SELECT *
FROM
table1 t1
FULL OUTER JOIN table2 t2
ON t1.id = t2.id

SQL Join tables with empty values

I have 2 tables
Lets say Table1 and Table2
They both have one shared value(id)
What I'm looking for is whether there is any function to combine them both based on that key, however if table2 has more elements, i want columns of table1 to be empty, and if table1 has more elements, table 2 columns to be empty
I tried a lot of different joins, but most of the time I end up with a lot of duplicate values as it tries to fill in both sides.
Tried Full outer join, Full join, etc
You are looking for full join:
select t1.*, t2.*
from t1 full join
t2
on t1.id = t2.id;
The above code from Gordon is right. However, since you have not specified the database and its version, I will post an alternate version for MySQL, which should also work for other databases.
Without duplicates:
SELECT * FROM Table1
LEFT JOIN Table2 ON Table1.id = Table2.id
UNION
SELECT * FROM Table1
RIGHT JOIN Table2 ON Table1.id = Table2.id
With duplicates:
SELECT * FROM Table1
LEFT JOIN Table2 ON Table1.id = Table2.id
UNION ALL
SELECT * FROM Table1
RIGHT JOIN Table2 ON Table1.id = Table2.id

SQL Server - Return all records from LEFT table and only non matching records from right table

I have 2 tables with the same structure (field names). Table1 and Table2.
I need to return all records from Table1 and only records from Table2 that do not match/join to a record in Table1.
Table2 has more records than Table1.
I am joining the 2 tables on 3 fields.
So basically I want all records from table1 returned and only records that don't have a match (joining on the 3 fields) to table1 from table2 returned.
Put another way, Table1 records take precedence over table2 records in my final result output when the records exist in both tables (same value for the 3 fields)
I started writing something like the below but I don't think it will work. Should I use a left outer join instead?
Select * from table1 t1
left join table2 t2 on t1.id = t2.id and t1.date = t2.date and t1.custid= t2.custid
where t2.id is null or t2.date is null or t2.custid is null
So, you need every row from table1 plus the rows from table2 that don't match with table1?:
SELECT *
FROM table1
UNION ALL
SELECT *
FROM table2 t2
WHERE NOT EXISTS(SELECT * FROM table1
WHERE id = t2.id
AND date = t2.date
AND custid = t2.custid);
Select * from table1 t1
Union
Select * from table2 t2
Where Not exists
(Select * from table1
Where id = t1.id
and date = t1.date
and custid= t1.custid)

SQL Query: select all the records from table1 which exist in table2 + all the records from table2 which don't exist in table1

consider the following example.
I have to select all the records from table1 which exist in table2 also + all the records from table2 which don't exist in table1 but exist in table2 and have IsActive=1 and status not null.
I initially tried it with a join but how to do the later part where I have to select the records which don't exist in table 1 ? I have to do it inside a single query presumably with a SQL view.
Edit
I need to combine the results like a UNION of 2 tables, so incase of rows absent in table1 but present in table2, the columns of belonging to table1 would be blank.
Here's an example query:
select *
from Table2 t2
left join
Table1 t1
on t1.id = t2.id
where t1.id is not null
or (isActive = 1 and status is not null)
The first line of the where clause takes care of "all the records from table1 which exist in table2". The second line is for "don't exist in table1 but exist in table2 and have IsActive=1 and status not null".
You will need an outer join here.
http://msdn.microsoft.com/en-us/library/ms187518.aspx
Is this it? Not sure if I got right what you want to do.
SELECT
*
FROM
Table1 t1
JOIN
Table2 t2 ON (t1.ID = t2.ID OR (t1.ID IS NULL AND t2.isActive = 1 AND t2.Status IS NOT NULL))