How can I join three tables in SQL? - sql

I'm faced with following problem, I'm not sure how to tackle it. I have a rough idea, but I'm not sure how I would execute it.
Here is the issue
Table A exist in the following format:
Name | Name_Att| Name2 | Name_Att2
--------+---------+-------+-------
ryan | red | xyz | green
Likewise Table B exist in this format:
Name | Name_id |
--------+---------+
ryan | 123 |
xyz | 129 |
Likewise Table C exist in this format:
Name_Att| C_id | Name_id
--------+---------+---------
red | 4591 | 123
green | 4592 | 123
blue | 4593 | 123
green | 4594 | 129
blue | 4595 | 129
So I'm trying to create the following table:
Name_id | C_id |Name_id2 | C_id2
--------+---------+---------+----------
123 | 4591 | 129 | 4594
Is there any way I should go about this? I was thinking about writing code to do this. I'm sure there is SQL way to accomplish this. However I'm not sure.

Try to include the following joins in your solution:
FROM (TableA INNER JOIN TableB ON TableA.Name = TableB.Name)
INNER JOIN TableC ON (TableA.Name_Att = TableC.Name_Att) AND
(TableB.Name_id = TableC.Name_id)
You can do the same thing to get Name_Id2 and C_id2; make sure to alias your table names correctly.

Related

How do I select a row with two columns joined by one column from another table?

I'm sorry if my question doesn't make sense. I'm not sure how to word it. I just started self-studying SQL and there's something I want to do but I'm not sure how to do it. I'm hoping someone could help me out.
I have two tables where Table 1 is a list of students and Table 2 is a list of lab partners. Table 2 has students listed with their id numbers instead of names. The two tables can be joined together by student_id = student1_id (or student2_id).
Table 1
| student_id | student_name |
|------------|--------------|
| 01 | Alex |
| 02 | Ben |
| 03 | Chris |
| 04 | Dave |
Table 2
| group_number | student1_id | student2_id |
|--------------|-------------|-------------|
| 1 | 01 | 02 |
| 2 | 03 | 04 |
What would my select query look like if I wanted lab partners to show as names instead of id numbers? (See desired result below.)
Query result:
| group_number | (student_name of student1_id) | (student_name of student2_id) |
|--------------|-------------------------------|-------------------------------|
| 1 | Alex | Ben |
| 2 | Chris | Dave |
I'm actually stumped. I'm hoping someone could send me in the right direction. I would greatly appreciate the help!
You would join on the id columns with the Table1(that holds the name of students) twice-> one for student_1's name and one for student_2's name
select t2.group_number
,t11.student_name as student_name_1
,t12.student_name as student_name_2
from Table2 t2
join Table1 t11
on t2.student1_id=t11.student_id
join Table1 t12
on t2.student2_id=t12.student_id

Query to join two tables with like operator

I have two table which has data as below
Table1
+-----+-------+-------------------------+
| ID | value | Test |
+-----+-------+-------------------------+
| 200 | 2456 | yyy xxcv zuio uio tzrue |
| 201 | 2905 | xxx tttt ssrt uio uioe |
| 203 | 34567 | zzz iii ool uiol werts |
| 204 | 2356 | xxx tttt ssrt uio wertz |
| 205 | 3478 | io ser xcv erto klop |
| 206 | 4567 | xxx tttt ssrt uio poiu |
| 207 | 234 | zzz iii ool uiol wert |
+-----+-------+-------------------------+
I would like to create where clause to get rows with like operator.
for ex.
select *
from Table1
where test like '%xxcv zuio%' Or Like '%iii ool%' OR '%xcv erto%' OR '%ssrt uio%' OR '%uio uioe%'
But my problem is that there are more than 15-20 different parameter in where clause, it can also increase latter.
So I have decided to create a new table in database lets called it Table2 with parameter as shown below.
Table2
+----+-----------+
| ID | Parameter |
+----+-----------+
| 20 | xxcv zuio |
| 21 | iii ool |
| 22 | xcv erto |
| 23 | ssrt uio |
| 24 | uio uioe |
+----+-----------+
My question is how can I join this table to give the same result like above query.
I am still learning joins so any advice will be helpful.
You can use exists:
select t1.*
from Table1 t1
where exists (select 1 from table2 t2 where t1.test like concat('%', t2.parameter, '%'))
I don't think using a join is helping in this scenario, and this kind of text searching is not a strong point for SQL Server. Consider building an elasticsearch index or using another system that supports text searching as a primary use case (SQL Server does support full text search if that feature is enabled, but I've always been warned away from it, so don't have experience with it.)
If staying within TSQL is desirable for your particular situation, then I would use the awkward "OR LIKE" syntax. What it lacks in elegance it makes up for in readability.

SQL - specific requirement to compare tables

I'm trying to merge 2 queries into 1 (cuts the number of daily queries in half): I have 2 tables, I want to do a query against 1 table, then the same query against the other table that has the same list just less entries.
Basically its a list of (let's call it for obfuscation) people and hobby. One table is ALL people & hobby, the other shorter list is people & hobby that I've met. Table 2 would all be found in table 1. Table 1 includes entries (people I have yet to meet) not found in table 2
The tables are synced up from elsewhere, what I'm looking to do is print a list of ALL people in the first column then print the hobby ONLY of people that are on both lists. That way I can see the lists merged, and track the rate at which the gap between both lists is closing. I have tried a number of SQL combinations but they either filter out the first table and match only items that are true for both (i.e. just giving me table 2) or just adding table 2 to table 1.
Example of what I'm trying to do below:
+---------+----------+--+----------+---------+--+---------+----------+
| table1 | | | table2 | | | query | |
+---------+----------+--+----------+---------+--+---------+----------+
| name | hobby | | activity | person | | name | hobby |
| bob | fishing | | fishing | bob | | bob | fishing |
| bill | vidgames | | hiking | sarah | | bill | |
| sarah | hiking | | planking | sabrina | | sarah | hiking |
| mike | cooking | | | | | mike | |
| sabrina | planking | | | | | sabrina | planking |
+---------+----------+--+----------+---------+--+---------+----------+
Normally I'd just take the few days to learn SQL a bit better however I'm stretched pretty thin at work as it is!
I should mention the table 2 is flipped and the headings are all unique (don't think this matters)!
I think you just want a left join:
select t1.name, t2.activity as hobby
from table1 t1 left join
table2 t2
on t1.name = t2.person;

How to select table with a concatenated column?

I have the following data:
select * from art_skills_table;
+----+------+---------------------------+
| ID | Name | skills |
+----+------+---------------------------|
| 1 | Anna | ["painting","photography"]|
| 2 | Bob | ["drawing","sculpting"] |
| 3 | Cat | ["pastel"] |
+----+------+---------------------------+
select * from computer_table;
+------+------+-------------------------+
| ID | Name | skills |
+------+------+-------------------------+
| 1 | Anna | ["word","typing"] |
| 2 | Cat | ["code","editing"] |
| 3 | Bob | ["excel","code"] |
+------+------+-------------------------+
I would like to write an SQL statement which results in the following table.
+------+------+-----------------------------------------------+
| ID | Name | skills |
+------+------+-----------------------------------------------+
| 1 | Anna | ["painting","photography","word","typing"] |
| 2 | Bob | ["drawing","sculpting","excel","code"] |
| 3 | Cat | ["pastel","code","editing"] |
+------+------+-----------------------------------------------+
I've tried something like SELECT * from art_skills_table LEFT JOIN computer_table ON name. However it doesn't give what I need. I've read about array_cat but I'm having a bit of trouble implementing it.
if the skills column from both tables are arrays, then you should be able to get away with this:
SELECT a.ID, a.name, array_cat(a.skills, c.skills)
FROM art_skills_table a LEFT JOIN computer_table c
ON c.id = a.id
That said, While you used LEFT join in your sample, I think either an INNER or FULL (OUTER) join might serve you better.
First, i wondered why the data are stored in such a model.
Was of the opinion that NoSQL databases lack ability for joins and ...
... a semantic triple would be in the form of subject–predicate–object.
... a Key-value (KV) stores use associative arrays.
... a relational database would be normalized.
A few information about the use case would have helped.
Nevertheless, you can select the data with CONCAT and REPLACE for the desired form.
SELECT art_skills_table.ID, computer_table.name,
CONCAT(
REPLACE(art_skills_table.skills, '}',','),
REPLACE(computer_table.skills, '{','')
)
FROM art_skills_table JOIN computer_table ON art_skills_table.ID = computer_table.ID
The query returns the following result:
+----+------+--------------------------------------------+
| ID | Name | Skills |
+----+------+--------------------------------------------+
| 1 | Anna | {"painting","photography","word","typing"} |
| 2 | Cat | {"drawing","sculpting","code","editing"} |
| 3 | Bob | {"pastel","excel","code"} |
+----+------+--------------------------------------------+
I've used the ID for the JOIN, even though Bob has different values.
The JOIN should probably be done over the name.
JOIN computer_table ON art_skills_table.Name = computer_table.Name
BTW, you need to tell us what SQL engine you're running on.

Using a table to lookup multiple IDs on one row

I have two tables I am using at work to help me gain experience in writing SQL queries. One table contains a list of Applications and has three columns -
Application_Name, Application_Contact_ID and Business_Contact_ID. I then have a separate table called Contacts with two columns - Contact_ID and Contact_Name. I am trying to write a query that will list the Application_Name and Contact_Name for both the Applications_Contact_ID and Business_Contact_ID columns instead of the ID number itself.
I understand I need to JOIN the two tables but I haven't quite figured out how to formulate the correct statement. Help Please!
APPLICATIONS TABLE:
+------------------+------------------------+---------------------+
| Application_Name | Application_Contact_ID | Business_Contact_ID |
+------------------+------------------------+---------------------+
| Adobe | 23 | 23 |
| Word | 52 | 14 |
| NotePad++ | 44 | 989 |
+------------------+------------------------+---------------------+
CONTACTS TABLE:
+------------+--------------+
| Contact_ID | Contact_Name |
+------------+--------------+
| 23 | Tim |
| 52 | John |
| 14 | Jen |
| 44 | Carl |
| 989 | Sam |
+------------+--------------+
What I am trying to get is:
+------------------+--------------------------+-----------------------+
| Application_Name | Application_Contact_Name | Business_Contact_Name |
+------------------+--------------------------+-----------------------+
| Adobe | Tim | Tim |
| Word | John | Jen |
| NotePad++ | Carl | Sam |
+------------------+--------------------------+-----------------------+
I've tried the below but it is only returning the name for one of the columns:
SELECT Application_Name, Application_Contact_ID, Business_Contact_ID, Contact_Name
FROM Applications
JOIN Contact ON Contact_ID = Application_Contact_ID
This is a pretty critical and 101 part of SQL. Consider reading this other answer on a different question, which explains the joins in more depth. The trick to your query, is that you have to join the CONTACTS table twice, which is a bit hard to visualize, because you have to go there for both the application_contact_id and business_contact_id.
There are many flavors of joins (INNER, LEFT, RIGHT, etc.), which you'll want to familiarize yourself with for the future reference. Consider reading this article at the very least: https://www.techonthenet.com/sql_server/joins.php.
SELECT t1.application_name Application_Name,
t2.contact_name Application_Contact_name,
t3.contact_name Business_Contact_name
FROM applications t1
INNER JOIN contacts ON t2 t1.Application_Contact_ID = t2.contact_id -- join contacts for appName
INNER JOIN contacts ON t3 t1.business_Contact_ID = t3.contact_id; -- join contacts for busName