MySQL FROM Clause

MySQL FROM Clause

The MySQL FROM Clause is used to select some records from a table. It can also be used to retrieve records from multiple tables using JOIN condition.

Syntax:

  1. FROM table1
  2. [ { INNER JOIN | LEFT [OUTERJOINRIGHT [OUTERJOIN } table2
  3. ON table1.column1 = table2.column1 ]

Parameters

table1 and table2: specify tables used in the MySQL statement. The two tables are joined based on table1.column1 = table2.column1.

Note:

  • If you are using the FROM clause in a MySQL statement then at least one table must have been selected.
  • If you are using two or more tables in the MySQL FROM clause, these tables are generally joined using INNER or OUTER joins.

MySQL FROM Clause: Retrieve data from one table

The following query specifies how to retrieve data from a single table.

Use the following Query:

  1. SELECT *
  2. FROM officers
  3. WHERE officer_id <= 3;

aa

MySQL FROM Clause: Retrieve data from two tables with inner join

Let’s take an example to retrieve data from two tables using INNER JOIN.

Here, we have two tables “officers” and “students”.

10

Execute the following query:

  1. SELECT officers.officer_id, students.student_name
  2. FROM students
  3. INNER JOIN officers
  4. ON students.student_id = officers.officer_id;

101

MySQL FROM Clause: Retrieve data from two tables using outer join

Execute the following query:

  1. SELECT officers.officer_id, students.student_name
  2. FROM officers
  3. LEFT OUTER JOIN students
  4. ON officers.officer_id = students.student_id;

102