SQL SELECT Statement | Syntax, Examples and Complete Guide

SQL SELECT Statement

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.


What is SELECT in SQL?

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.


Why is SELECT Important?

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.


Basic Syntax of SELECT

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:


Creating a Sample Student Table

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

Selecting All Columns

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.


Selecting Specific Columns

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.


Selecting a Single Column

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

Selecting Multiple Columns

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.


Understanding the FROM Clause

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.


Using Column Aliases with SELECT

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

Using Multiple Column Aliases

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.


SELECT with Expressions

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.


SELECT with Column Calculations

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.


Selecting Constant Values

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.


Using DISTINCT with SELECT

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.


SELECT and NULL Values

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 from Different Tables

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.

Learn SQL Joins →


SELECT with WHERE Clause

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:

SQL WHERE Clause →


SELECT with ORDER BY

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:

Learn SQL ORDER BY →


SELECT with GROUP BY and HAVING

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 with Aggregate Functions

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:

Learn SQL Functions →


SELECT with Subqueries

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.

Learn SQL Subqueries →


SELECT with LIMIT or TOP

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.


SELECT Does Not Modify Table Data

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:


SELECT in Real-World Applications

College Management System

A college portal may retrieve student names and courses for displaying a student list.

SELECT StudentName, Course
FROM Student;

Banking Application

A banking application may retrieve customer account information.

SELECT CustomerName, AccountNumber
FROM Customer;

E-Commerce Application

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.


Common Mistakes While Using SELECT


SELECT Query Best Practices


SELECT Statement: Quick Reference

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 Statement Interview Questions

1. What is SELECT in SQL?

SELECT is a SQL statement used to retrieve information from database tables.

2. What is the basic syntax of SELECT?

SELECT column_name
FROM table_name;

3. What does SELECT * mean?

It requests all columns from the specified table or source.

4. What is a result set?

The result set is the collection of rows and columns returned by a query.

5. Can SELECT retrieve only one column?

Yes. A single column can be specified after SELECT.

6. Can SELECT retrieve multiple columns?

Yes. Multiple column names are separated by commas.

7. What is a column alias?

An alias is a temporary name assigned to a column or expression in the query result.

8. Does an alias rename the actual database column?

No. It changes only the heading displayed in that particular query result.

9. What is DISTINCT used for?

DISTINCT is used to remove duplicate result combinations.

10. Can SELECT perform calculations?

Yes. SELECT can evaluate arithmetic expressions and expressions involving columns.

11. Does SELECT modify table records?

A normal SELECT query retrieves data; it does not insert, update, or delete the retrieved rows.

12. Can SELECT work with multiple tables?

Yes. SELECT can retrieve data from multiple related tables using JOIN operations.

13. Can SELECT use WHERE?

Yes. WHERE can be used to restrict the rows returned by a SELECT query.

14. Can SELECT use ORDER BY?

Yes. ORDER BY sorts the rows in the result.

15. Can SELECT use aggregate functions?

Yes. Functions such as COUNT(), SUM(), AVG(), MIN(), and MAX() can be used with SELECT.

16. What is the difference between SELECT and INSERT?

SELECT retrieves data, whereas INSERT adds new records to a table.

17. What is the difference between SELECT and UPDATE?

SELECT reads data, whereas UPDATE changes existing records.

18. What is the difference between SELECT and DELETE?

SELECT retrieves data, whereas DELETE removes records from a table.


Complete Practical Example

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.


Related SELECT Concepts

SELECT is the foundation for many advanced SQL operations. After learning the basic statement, the following topics can be studied separately:


Conclusion

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.

← Previous: INSERT INTO Next: WHERE Clause →
Home Visit Our YouTube Channel