[SQL] Query using Data from 2 Tables

  • Thread starter WWGD
  • Start date
  • Tags
    Data Sql
In summary, the conversation discusses how to query data from two related tables, T_1 and T_2, for information on the salary of French teachers from 2-to-3 P.M. It is suggested to do a join query using the subject column in both tables, and it is noted that the resulting table will have NULL values if there is no match. It is also clarified that the join table is not actually created, but rather exists in memory during the computation of the query.
  • #1
WWGD
Science Advisor
Gold Member
7,020
10,605
Hi, say I am doing a query for which I need data from two tables,

on, say the salary of those who teach French from 2-to-3 P.M, and one table T_1

contains the teaching subject and another table T_2 contains the class schedule/hours.

Say these two tables are related.

Do we then do a query on the join of the tables T_1 with T_2 ?
 
Technology news on Phys.org
  • #2
Yes you simply do a join.
It depends on which table may contain NULL values.
Lets say :

SELECT *
FROM T_1
LEFT JOIN T_2
ON T_1.subject = T_2.subject;

This query tells to join the left table (T_1) with the right table (T_2) . The result will be NULL on the right side if there is no match.
 
  • #3
I see, thanks, but the join table is not actually created, is it, it just lives in memory when the query is being computed, right?
 
  • #4
Sorry for the late reply,
Yes, the table won't be created
 
  • #5
Thank you, ZLB.
 

Related to [SQL] Query using Data from 2 Tables

1. What is a SQL query?

A SQL query is a request for data or information from a database. It is written in a special language called Structured Query Language (SQL) and is used to retrieve, insert, update, or delete data in a database.

2. How do you combine data from two tables in a SQL query?

To combine data from two tables, you can use a join statement in your SQL query. A join statement allows you to match rows from one table with rows from another table based on a common column or key.

3. What is the difference between an inner join and an outer join?

An inner join in SQL only includes rows from both tables that match the specified conditions. An outer join, on the other hand, includes all rows from one table, even if there is no matching row in the other table.

4. How do you specify which columns to include in a SQL query?

To specify which columns to include, you can use the SELECT statement in your SQL query. This allows you to choose specific columns from one or both tables to be included in the results.

5. Can you use data from more than two tables in a single SQL query?

Yes, it is possible to use data from more than two tables in a single SQL query. This can be accomplished by using multiple join statements and specifying the appropriate conditions for each table.

Similar threads

  • Programming and Computer Science
2
Replies
51
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
7
Views
514
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
850
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
5K
Back
Top