FROM clause in MySQL

MySQL FROM
In MySQL, the FROM clause is used with the MySQL SELECT statement to specify the table for the records to be retrieved.

Syntax: To select all fields from a table.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT * FROM table_name;
SELECT * FROM table_name;
SELECT * FROM table_name;

Example: Selecting all fields from a table.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT * FROM items;
SELECT * FROM items;
SELECT * FROM items;

Output:

ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100
4 Grocery 90
5 Toys 50

 

Syntax: To select specific fields from a table.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT expressions
FROM table_name
WHERE conditions;
SELECT expressions FROM table_name WHERE conditions;
SELECT expressions  
FROM table_name
WHERE conditions;

Parameters:

conditions: It is used to specify the conditions to be strictly followed for selection.

Example: Selecting specific fields from a table.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT *
FROM items
WHERE id > 3;
SELECT * FROM items WHERE id > 3;
SELECT *  
FROM items  
WHERE  id > 3;

Output:

ID NAME QUANTITY
4 Grocery 90
5 Toys 50

 

Syntax: To select specific fields from multiple tables.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT expressions
FROM table1
INNER JOIN table2
ON join_predicate;
SELECT expressions FROM table1 INNER JOIN table2 ON join_predicate;
SELECT expressions  
FROM table1
INNER JOIN table2 
ON join_predicate;

Parameters:

join_predicate: It is used to specify the condition for joining tables.

Example: Selecting specific fields from multiple tables.
Items Table:

ID NAME QUANTITY
1 Electronics 30
2 Sports 45
3 Fashion 100
4 Grocery 90
5 Toys 50

Shops Table:

ID SHOP
1 A
2 B
3 C
4 D
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT items.quantity, shops.shop
FROM items
INNER JOIN shops
ON items.id = shops.id
ORDER BY quantity;
SELECT items.quantity, shops.shop FROM items INNER JOIN shops ON items.id = shops.id ORDER BY quantity;
SELECT items.quantity, shops.shop
FROM items  
INNER JOIN shops  
ON items.id = shops.id  
ORDER BY quantity;

Output:

QUANTITY SHOP
30 A
45 B
90 C
100 D