The SELECT statement is the primary SQL command used to retrieve information from a database. Whenever an application needs to display stored information, it sends a query to the database and receives the required records as a result.
For example, a college application may use SELECT to display student details, an online store may use it to show products, and a banking application may use it to retrieve account information.
SELECT is therefore one of the first SQL commands that students and beginners should understand before learning advanced database queries.
SELECT is a SQL statement used to retrieve data from one or more tables. It can return all columns, selected columns, calculated values, or unique values depending on the query.
The information returned by a SELECT query is called a result set.
A basic SELECT operation does not permanently modify the records stored in the table. It reads the required information and presents it as query output.
A database is useful only when applications and users can retrieve the information stored inside it. SELECT provides the basic mechanism for accessing that information.
The general form of a SELECT statement is:
SELECT column_name FROM table_name;
Multiple columns can be retrieved by separating their names with commas.
SELECT column1, column2, column3 FROM table_name;
The basic structure contains two important parts:
To understand SELECT queries, consider a simple Student table containing the following information.
CREATE TABLE Student (
StudentID INT,
StudentName VARCHAR(100),
Course VARCHAR(50),
Age INT
);
Sample records:
| StudentID | StudentName | Course | Age |
|---|---|---|---|
| 101 | Rahul | BCA | 20 |
| 102 | Priya | B.Tech | 21 |
| 103 | Amit | MCA | 22 |
| 104 | Neha | BCA | 19 |
The asterisk symbol (*) represents all columns of the selected table.
SELECT * FROM Student;
This query retrieves every column and every row from the Student table.
The result will contain:
| StudentID | StudentName | Course | Age |
|---|---|---|---|
| 101 | Rahul | BCA | 20 |
| 102 | Priya | B.Tech | 21 |
| 103 | Amit | MCA | 22 |
| 104 | Neha | BCA | 19 |
Although SELECT * is convenient while learning or inspecting a table, selecting only the required columns is generally preferable in production applications.
A SELECT query does not have to return every column. You can specify exactly which columns are required.
SELECT StudentName, Course FROM Student;
Output:
| StudentName | Course |
|---|---|
| Rahul | BCA |
| Priya | B.Tech |
| Amit | MCA |
| Neha | BCA |
This approach makes the query more precise because the database returns only the information needed by the application or user.
A SELECT query can retrieve just one column.
SELECT StudentName FROM Student;
The result contains only the names of students.
| StudentName |
|---|
| Rahul |
| Priya |
| Amit |
| Neha |
When information from several columns is required, column names are written after SELECT and separated by commas.
SELECT StudentID, StudentName, Age FROM Student;
This query retrieves the student's ID, name, and age but does not return the Course column.
The FROM clause identifies the source of the data.
SELECT StudentName FROM Student;
Here:
A SELECT statement normally needs a data source when retrieving columns from a table.
An alias provides a temporary name for a column in the query result. It does not change the actual column name in the database.
SELECT StudentName AS Name FROM Student;
The result heading will appear as Name instead of StudentName.
| Name |
|---|
| Rahul |
| Priya |
| Amit |
| Neha |
Aliases can be assigned to several columns in the same query.
SELECT
StudentID AS ID,
StudentName AS Name,
Course AS Program
FROM Student;
Aliases are especially useful when creating reports or presenting database information to end users.
SQL can evaluate expressions directly in a SELECT statement.
SELECT 20 + 30;
Result:
50
Arithmetic operators can be used for calculations.
SELECT
100 + 50 AS Addition,
100 - 40 AS Subtraction,
10 * 5 AS Multiplication,
100 / 4 AS Division;
The exact result of division can depend on the numeric data types and database system being used.
Expressions can also be created using values stored in table columns.
Suppose an Employee table contains Salary:
SELECT EmployeeName, Salary, Salary + 5000 AS UpdatedSalary FROM Employee;
The query calculates a value for the result without changing the original Salary stored in the table.
This distinction is important: a SELECT expression calculates a value for the result set; it does not update the table.
SELECT can also return a constant value without retrieving a column from a table.
SELECT 'Welcome to CSE Gyan' AS Message;
Result:
| Message |
|---|
| Welcome to CSE Gyan |
This form is useful for demonstrations, testing expressions, and checking database functions.
A table may contain repeated values. The DISTINCT keyword can be used with SELECT when only unique combinations of the selected columns are required.
SELECT DISTINCT Course FROM Student;
If BCA appears multiple times, the result contains BCA only once.
| Course |
|---|
| BCA |
| B.Tech |
| MCA |
DISTINCT applies to the complete set of selected columns. Therefore, when several columns are selected, duplicate combinations are removed rather than checking only one individual column.
For a detailed explanation, see: SQL DISTINCT Keyword.
A table can contain NULL values. NULL represents the absence of a known value and should not be treated as an ordinary number or text value.
For example:
SELECT StudentName, Email FROM Student;
If Email is NULL for a particular student, the result displays an empty or NULL value depending on the database client.
Conditions involving NULL require special SQL operators such as IS NULL and IS NOT NULL.
SELECT can retrieve information from different tables. When related tables are involved, SQL JOIN operations can be used to combine their data.
SELECT Student.StudentName, Course.CourseName FROM Student INNER JOIN Course ON Student.CourseID = Course.CourseID;
The JOIN operation is an advanced use of SELECT and is covered separately in the SQL Joins tutorial.
The SELECT statement can be combined with a WHERE clause when only particular rows are required.
SELECT StudentName, Course FROM Student WHERE Age > 20;
The WHERE clause filters rows according to a condition.
For a complete explanation of conditions, comparison operators, logical operators, LIKE, IN, BETWEEN, and NULL filtering, see:
SELECT results can be arranged using the ORDER BY clause.
SELECT StudentName, Age FROM Student ORDER BY Age DESC;
The query retrieves student information and sorts the result by age in descending order.
For detailed sorting concepts:
SELECT is also used with GROUP BY when information needs to be summarized into groups.
SELECT Course, COUNT(*) AS TotalStudents FROM Student GROUP BY Course;
HAVING can then be used to filter groups after aggregation.
SELECT Course, COUNT(*) AS TotalStudents FROM Student GROUP BY Course HAVING COUNT(*) > 10;
These concepts are covered in detail in their dedicated tutorials.
Learn SQL GROUP BY →
Learn SQL HAVING →
SELECT can work with aggregate functions such as:
| Function | Purpose |
|---|---|
| COUNT() | Counts values or rows |
| SUM() | Calculates a total |
| AVG() | Calculates an average |
| MIN() | Finds the minimum value |
| MAX() | Finds the maximum value |
Example:
SELECT COUNT(*) AS TotalStudents FROM Student;
This returns the number of rows in the Student table.
For detailed information about SQL functions:
A SELECT statement can contain another query, known as a subquery.
SELECT StudentName
FROM Student
WHERE Marks >
(
SELECT AVG(Marks)
FROM Student
);
The inner query calculates the average marks, while the outer query retrieves students whose marks are above that value.
Subqueries are an advanced SQL topic and are explained separately.
Some database systems provide features for restricting the number of rows returned by a query.
For example, MySQL and several other systems support LIMIT:
SELECT * FROM Student LIMIT 5;
SQL Server supports TOP:
SELECT TOP 5 * FROM Student;
The exact syntax for limiting rows depends on the database management system.
One important property of a normal SELECT query is that it retrieves information without changing the stored rows.
For example:
SELECT StudentName FROM Student;
The query only reads StudentName values. It does not add, remove, or modify records.
The commonly used commands for modifying data are:
A college portal may retrieve student names and courses for displaying a student list.
SELECT StudentName, Course FROM Student;
A banking application may retrieve customer account information.
SELECT CustomerName, AccountNumber FROM Customer;
An online shopping system may retrieve product names and prices.
SELECT ProductName, Price FROM Products;
These examples demonstrate the basic role of SELECT: retrieving the information required by an application.
| Requirement | Example |
|---|---|
| All columns | SELECT * FROM Student; |
| One column | SELECT StudentName FROM Student; |
| Multiple columns | SELECT StudentName, Course FROM Student; |
| Alias | SELECT StudentName AS Name FROM Student; |
| Unique values | SELECT DISTINCT Course FROM Student; |
| Filtered records | SELECT * FROM Student WHERE Age > 20; |
| Sorted records | SELECT * FROM Student ORDER BY Age; |
| Count records | SELECT COUNT(*) FROM Student; |
SELECT is a SQL statement used to retrieve information from database tables.
SELECT column_name FROM table_name;
It requests all columns from the specified table or source.
The result set is the collection of rows and columns returned by a query.
Yes. A single column can be specified after SELECT.
Yes. Multiple column names are separated by commas.
An alias is a temporary name assigned to a column or expression in the query result.
No. It changes only the heading displayed in that particular query result.
DISTINCT is used to remove duplicate result combinations.
Yes. SELECT can evaluate arithmetic expressions and expressions involving columns.
A normal SELECT query retrieves data; it does not insert, update, or delete the retrieved rows.
Yes. SELECT can retrieve data from multiple related tables using JOIN operations.
Yes. WHERE can be used to restrict the rows returned by a SELECT query.
Yes. ORDER BY sorts the rows in the result.
Yes. Functions such as COUNT(), SUM(), AVG(), MIN(), and MAX() can be used with SELECT.
SELECT retrieves data, whereas INSERT adds new records to a table.
SELECT reads data, whereas UPDATE changes existing records.
SELECT retrieves data, whereas DELETE removes records from a table.
The following example demonstrates a simple SELECT operation from beginning to end.
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(100),
Course VARCHAR(50),
Age INT
);
INSERT INTO Student
(StudentID, StudentName, Course, Age)
VALUES
(101, 'Rahul', 'BCA', 20),
(102, 'Priya', 'B.Tech', 21),
(103, 'Amit', 'MCA', 22);
SELECT StudentID, StudentName, Course
FROM Student;
The final SELECT query retrieves only StudentID, StudentName, and Course from the table.
SELECT is the foundation for many advanced SQL operations. After learning the basic statement, the following topics can be studied separately:
The SQL SELECT statement is the foundation of data retrieval in relational databases. It allows users and applications to access complete tables, selected columns, unique values, calculated results, and other useful information without directly modifying the stored records.
A strong understanding of SELECT syntax, column selection, aliases, DISTINCT, expressions, and result sets provides the base required for learning more advanced SQL concepts such as WHERE, ORDER BY, GROUP BY, JOINs, functions, and subqueries.
For SQL students and developers, mastering SELECT is an important first step toward writing efficient database queries and building practical database applications.