SQL SELECT Statement
The SELECT statement is the most frequently used command in SQL. It is used to retrieve data from one or more tables in a database. Every time data needs to be read or displayed, the SELECT statement is used.
Think of SELECT as asking a question to the database — and the database responds with the matching data.
The Reference Table
All examples in this topic will use the following Students table:
| StudentID | StudentName | Age | City | JoinDate |
|---|---|---|---|---|
| 1 | Ravi Sharma | 20 | Delhi | 2024-01-10 |
| 2 | Priya Mehta | 22 | Mumbai | 2024-02-05 |
| 3 | Arjun Nair | 19 | Chennai | 2024-02-20 |
| 4 | Sneha Kapoor | 21 | Pune | 2024-03-01 |
| 5 | Rohit Das | 23 | Kolkata | 2024-03-15 |
Basic SELECT Syntax
SELECT column1, column2, ...
FROM table_name;Selecting All Columns
The asterisk (*) is a shorthand that means "all columns." It retrieves every column from the table.
SELECT * FROM Students;Result: All 5 rows and all 5 columns from the Students table are returned, as shown in the reference table above.
Selecting Specific Columns
Instead of retrieving all columns, specific column names can be listed. This is useful when only certain information is needed.
SELECT StudentName, City FROM Students;Result:
| StudentName | City |
|---|---|
| Ravi Sharma | Delhi |
| Priya Mehta | Mumbai |
| Arjun Nair | Chennai |
| Sneha Kapoor | Pune |
| Rohit Das | Kolkata |
Only the StudentName and City columns are returned. Age, StudentID, and JoinDate are not included.
Selecting a Single Column
SELECT StudentName FROM Students;Result:
| StudentName |
|---|
| Ravi Sharma |
| Priya Mehta |
| Arjun Nair |
| Sneha Kapoor |
| Rohit Das |
SELECT With Expressions (Calculated Columns)
A calculation can be performed directly inside a SELECT statement. For example, to display each student's age five years from now:
SELECT StudentName, Age, Age + 5 AS AgeAfter5Years
FROM Students;Result:
| StudentName | Age | AgeAfter5Years |
|---|---|---|
| Ravi Sharma | 20 | 25 |
| Priya Mehta | 22 | 27 |
| Arjun Nair | 19 | 24 |
| Sneha Kapoor | 21 | 26 |
| Rohit Das | 23 | 28 |
The AS keyword gives the calculated column a custom name — called an alias. This is covered in more detail in the SQL Aliases topic.
SELECT With a Constant Value
A fixed text or number can also appear as a column in the result:
SELECT StudentName, 'Active' AS Status FROM Students;Result:
| StudentName | Status |
|---|---|
| Ravi Sharma | Active |
| Priya Mehta | Active |
| Arjun Nair | Active |
| Sneha Kapoor | Active |
| Rohit Das | Active |
SELECT DISTINCT
SELECT DISTINCT removes duplicate values from the result. If the same value appears more than once in a column, it will be shown only once in the output.
SELECT DISTINCT City FROM Students;This is covered in detail in the SQL DISTINCT topic, but it is good to know it exists alongside the basic SELECT statement.
Selecting Data With a Column Count
The COUNT() function can be combined with SELECT to count records:
SELECT COUNT(*) AS TotalStudents FROM Students;Result:
| TotalStudents |
|---|
| 5 |
Order of Clauses in a SELECT Statement
A full SELECT query can include many optional clauses. They must appear in the following order:
| Order | Clause | Purpose |
|---|---|---|
| 1 | SELECT | Which columns to retrieve |
| 2 | FROM | Which table to get data from |
| 3 | WHERE | Filter rows based on a condition |
| 4 | GROUP BY | Group rows that share a value |
| 5 | HAVING | Filter groups based on a condition |
| 6 | ORDER BY | Sort the result |
| 7 | LIMIT | Restrict the number of rows returned |
Only SELECT and FROM are required. All others are optional and will be covered in separate topics.
A Full SELECT Query Example
SELECT StudentName, Age, City
FROM Students
WHERE Age > 20
ORDER BY Age ASC
LIMIT 3;This query:
- Selects three specific columns
- From the Students table
- Only includes students older than 20
- Sorts them by age in ascending order
- Returns only the first 3 matching results
Key Points to Remember
- Use
SELECT *to get all columns, or list specific column names to get only what is needed. - Listing specific columns is better practice in production — it makes queries faster and output cleaner.
- Calculations and text values can be included directly in the
SELECTlist. - The
ASkeyword gives a column a custom label in the result output. - Clauses in a
SELECTquery always follow a fixed order.
Summary
The SELECT statement is the primary tool for reading data from a database. It can retrieve all columns or specific ones, perform inline calculations, display constant values, and count records. Every advanced SQL query — including filtering, sorting, grouping, and joining — starts with SELECT.
