SQL SELECT Statement
The SELECT
statement in SQL is used to fetch data from one or more tables in a database. It is one of the most commonly used commands in SQL and forms the foundation of data retrieval.
Syntax
SELECT Customer Name, Last Name FROM Customer;
column1, column2, ... : These are the columns to retrieve
-
table_name : The name of the table from which data is retrieved
Key Points
-
SELECT
is used to retrieve data -
Use
*
to select all columns -
Use a comma to separate multiple columns
-
The
WHERE
clause can be used to filter rows (not covered in this post)
To fetch all columns from a table:
SELECT * FROM table_name;
The asterisk *
means all columns will be selected.
Examples: - SELECT * FROM CUSTOMER;
Query Result
Customer ID | Customer Name | Customer Name | Country |
---|---|---|---|
1 | Shubham | Thakur | India |
2 | Prashant | Chopra | Australia |
3 | Aditya | Jain | Japan |
Example Queries
1. Select specific columns
To fetch only Customer Name and Last Name:
Example:
SELECT Customer Name, Last Name FROM Customer;
Output:
Customer Name | Customer Name |
---|---|
Shubham | Thakur |
Prashant | Chopra |
Aditya | Jain |
Example:
SELECT * FROM Customer;
Output:
Customer ID | Customer Name | Customer Name | Country |
---|---|---|---|
1 | Shubham | Thakur | India |
2 | Prashant | Chopra | Australia |
3 | Aditya | Jain | Japan |
Two-Minute Drill
1. Which keyword is used to fetch data from a table?
Ans: SELECT
2. What does SELECT *
mean?
Ans: Select all columns from the table
3. What will this query return?
4. How do you select two columns, say Name and Country?
Ans: SELECT Name, Country FROM table_name;