SELECT query in MySQL

MySQL SELECT
In MySQL, the MySQL SELECT statement is used to fetch records from one or more tables stored in the MySQL database.

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 > 2;
SELECT * FROM items WHERE id > 2;
SELECT *  
FROM items  
WHERE  id > 2;

Output:

ID NAME QUANTITY
3 Fashion 100
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.name, shops.shop
FROM items
INNER JOIN shops
ON items.id = shops.id
ORDER BY name;
SELECT items.name, shops.shop FROM items INNER JOIN shops ON items.id = shops.id ORDER BY name;
SELECT items.name, shops.shop
FROM items  
INNER JOIN shops  
ON items.id = shops.id  
ORDER BY name;

Output:

ID SHOP
Electronics A
Fashion B
Grocery C
Sports D