
201 views
MySQL FROM
The MySQL FROM
clause is used in a SELECT
statement to specify the table or tables from which you want to retrieve data. It is a fundamental part of SQL queries and is typically the first clause in a SELECT
statement.
The basic syntax of a SELECT
statement with the FROM
clause is as follows:
SELECT column1, column2, ...
FROM table_name;
Here’s how it works:
column1
,column2
, and so on represent the columns you want to retrieve data from. You can specify one or more columns, or you can use*
to select all columns.table_name
is the name of the table from which you want to retrieve data.
Here are some examples of how to use the FROM
clause in MySQL:
- Retrieve all columns from a table named
employees
:
SELECT *
FROM employees;
- Retrieve only the
product_name
andprice
columns from a table namedproducts
:
SELECT product_name, price
FROM products;
- Retrieve data from multiple tables by specifying multiple table names in the
FROM
clause. This is often used in conjunction withJOIN
clauses to combine data from different tables.
SELECT orders.order_id, customers.customer_name
FROM orders
JOIN customers ON orders.customer_id = customers.customer_id;
The FROM
clause is essential for identifying the source of data in your SQL query. It tells MySQL which table or tables you want to query and retrieve information from. You can use it to query a single table or multiple tables and, in combination with other clauses like WHERE
, JOIN
, and GROUP BY
, construct complex queries to retrieve and manipulate data from your database.