/    /  MySQL – FROM Clause

MySQL – FROM Clause

 

 

The FROM clause in MySQL is used to select records from a table. By using JOIN conditions, records from multiple tables can also be retrieved.

Syntax:

SELECT *
FROM employees;

Note

  • The FROM clause must contain at least one table in a MySQL statement.
  • From clauses that contain more than one table are typically joined using INNER or OUTER joins rather than the old syntax of WHERE clauses.

Example 

As an example, if we have a table named “employees” and would like to retrieve all the data from this table, we would use the following query:

select * from employees;;

MySQL - FROM Clause

A JOIN can also be used to specify multiple tables in the FROM clause. In MySQL, there are several types of joins, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.

Example 

As an example, if we have two tables named “employees” and “departments” and we wish to retrieve the employee name and department name from both tables, we would use the following query:

SELECT employees.first_name, employees.last_name, departments.dept_name
FROM employees
INNER JOIN departments ON employees.emp_no = departments.dept_no;

  MySQL - FROM Clause

The FROM clause can also contain sub-queries. If, for example, we want to retrieve the names of employees whose salary is greater than the average salary in the Employees table, we would enter the following query:

SELECT first_name, last_name
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

MySQL - FROM Clause