combine multiple tables based on commonalities of a single column - sql

How do i make one main table that has one instance of the column 'Instructor' and shows 'formsreturned' and 'availability_avg' next to the main 'Instructor' column?

Just create a standard INNER JOIN:
SELECT T1.Instructor, T1.Availability_Avg, T2.FormsReturned
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Instructor = T2.Instructor

Related

Is there any alternative way of achieving below logic in sql?

I have two tables -> tb1 and tb2.
I am performing left join operation on these tables using ID column and also i have one more condition such as one column is not equal to other column .
Below is sample code
select * from tb1 LEFT JOIN tb2 ON tb1.id=tb2.id AND tb1.pid!=tb2.pid;
I am able to get results from above query.
But i need to know is there any alternate ways to get same result using sql.?
The actually SQL standard uses <> instead of !=.
select * from tb1 LEFT JOIN tb2 ON tb1.id=tb2.id AND tb1.pid<>tb2.pid;
It seems to you not equal not working because of your join and join condition.
if we create two tables and create like your query
create table t1(id int,pid int);
create table t2 (id int,pid int );
insert into t1 values(1,2),(2,3),(3,4);
insert into t2 values(1,2),(2,3),(3,4);
select t1.* from t1 left join
t2 on t1.id=t2.id and
t1.pid!=t2.pid
order by t1.id
id pid
1 2
2 3
3 4
It returns all the values of 1st table, because LEFT JOIN returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.
But if you put inner join in the same it will not return any row. so i think problem is not in the "not equal operator"

SQL Getting value in same row as another known value

Best way is to explain in pseudo code
How do I Get x,
When table1.activity == "some_string"
Then x = table1.line_number in that same row.
I'm doing an INNER JOIN and I'm doing checks on table 2. Basically I don't want to join that row if table1.activity == "some_string"
well it's not mentioned table2 in your pseudo code
but you could filter values in the inner join ON statement or WHERE statement of the query. It depends what you want By example, (In where Section)
SELECT * FROM table1 AS pivot
INNER JOIN table2 USING(id)
WHERE pivot.activity <>'not_want_these_kind_of_Records';
Or in ON Section
SELECT * FROM table1 AS pivot
INNER JOIN table2 AS t2 ON t2.id=pivot.id
AND t2.activity <>'not_want_these_kind_of_Records';
The second One filter the results before join to the pivot table
Regards

Join SQL Tables with Unique Data (Not same number of columns!)

How can I join three or four SQL tables that DO NOT have an equal amount of rows while ensuring that there are no duplicates of a primary/foreign key?
Structure:
Table1: id, first_name, last_name, email
Table2: id (independent of id in table 1), name, location, table1_id, table2_id
Table3: id, name, location
I want all of the data from table 1, then all of the data from table 2 corresponding with the table1_id without duplicates.
Kind of tricky for a new guy...
Not sure what do you want to do with Table3.
A LEFT JOIN returns all records from the LEFT table, and the matched records from the right table. If there is no match (from the right side), then the result is NULL.
So per example:
SELECT * FROM Table1 AS t
LEFT JOIN Table2 AS tt
ON t.id = tt.id
The LEFT table refers to the table statement before the LEFT JOIN, and the RIGHT table refers to the table statement after the LEFT JOIN. If you want to add in Table3 as well, use the same logic:
SELECT * FROM Table1 AS t
LEFT JOIN Table2 AS tt
ON t.id = tt.id
LEFT JOIN Table3 AS ttt
ON t.id = ttt.id
Note, that I use alias names for the tables (by using AS), so that I can more easily refer to a specific table. For example, t refers to Table1, tt refers to Table2, and ttt refers to Table3.
Joins are often used in SQL, therefore it is useful to look into: INNER JOIN, RIGHT JOIN, FULL JOIN, and SELF JOIN, as well.
Hope this helps.
Good luck with learning!
You will want to use an LEFT JOIN
SELECT * FROM table1 LEFT JOIN table2 ON Table1.ID = Table2.table1_id

select everything from one table, and array from another

I have one table that contains lots of data and I want to select everything from this one.
Another table contains different statuscodes ( fields ID and text ), and I want to join this table into the first one so i get something like
All data from first table id1,text2,id2,text2 or id1,id2,text1,text2 from the second table.
USe LEFT OUTER JOIN
Select T1.*,T2.ID,T2.Text
from firstTable t1
LEFT OUTER JOIN SecondTable T2
on T1.<col>=T2.<col>
You can use join like this whether it may be left/right/inner join
SELECT t1.*,t2.id,t2.text
FROM table1 t1
LEFT JOIN table2 t2
ON t2.column_name=t1.column_name

SQL JOIN two tables with two equally named columns

I have two tables that I'd like to join
Right now I just do :
SELECT * FROM (default_insurance)
JOIN default_profiles ON uid = default_profiles.id
WHERE `uid` = '1
problem is that both default_insurance and default_profiles contains a column named company, and I only want the one from default_insurance, but is there a way to make a join that will automatically prefer columns from one of the tables WITHOUT having to SELECT (all columns that I want)
You can always specify what exactly do you need (+use table aliases):
SELECT t1.*, t2.company AS default_insurance_company
FROM default_profiles t1 LEFT JOIN default_insurance t2
ON t1.uid = t2.id
WHERE t1.uid = 1
(MySQL example)
Above will return all columns from t1 and additionally column company from t2, but on your result it will be named default_insurance_company.
Automatically no. You need to use like this:
SELECT [all columns that you want], di.company
FROM default_insurance di
JOIN default_profiles dp ON di.uid = dp.id
WHERE di.uid = '1