CodesJava

Easy learning with example program codes

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.

SELECT * FROM table_name;  

Example: Selecting all fields from a table.

SELECT * FROM items;

Output:

ID	NAME	        QUANTITY
1	Electronics	30
2	Sports	        45
3	Fashion	        100
4	Grocery	        90
5	Toys	        50
5 rows  in set <0.02 sec>

Syntax: To select specific fields from a table.

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.

SELECT *  
FROM items  
WHERE  id > 2;

Output:

ID NAME QUANTITY
3 Fashion 100
4 Grocery 90
5 Toys 50
3 rows in set <0.01 sec>

Syntax: To select specific fields from multiple tables.

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

Output:

NAME	        SHOP
Electronics	A
Fashion	        C
Grocery	        D
Sports	        B
4 rows in set <0.00 sec>
Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status