SQL SELECT statement
- The SELECT statement is used to retrieve data from one or more tables in a database.
Syntax:
SELECT column1, column2,…
FROM table_name;
- column1,column2,… : The columns you want to retrieve from the table.
- table_name: The name of the table from which you want to retrieve data.
- WHERE condition: An optional clause that specifies a condition that must be satisfied for the rows to be selected.
To select all the fields available in the table, we use this:
Syntax:
SELECT * FROM table_name;
- asterisks represent all attributes of the table.
Examples:
Customer table:
Customer ID | Customer Name | Customer Name | Country |
---|---|---|---|
1 | Shubham | Thakur | India |
2 | Prashant | Chopra | Australia |
3 | Aditya | Jain | Japan |
Query to fetch the fields Customer Name, Last Name from the table Customer:
Example:
SELECT Customer Name, Last Name FROM Customer;
Output:
Customer Name | Customer Name |
---|---|
Shubham | Thakur |
Prashant | Chopra |
Aditya | Jain |
To retrieve all the fields from the table Customer:
Example:
SELECT * FROM Customer;
Output:
Customer ID | Customer Name | Customer Name | Country |
---|---|---|---|
1 | Shubham | Thakur | India |
2 | Prashant | Chopra | Australia |
3 | Aditya | Jain | Japan |