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:

StudentIDStudentNameAgeCityJoinDate
1Ravi Sharma20Delhi2024-01-10
2Priya Mehta22Mumbai2024-02-05
3Arjun Nair19Chennai2024-02-20
4Sneha Kapoor21Pune2024-03-01
5Rohit Das23Kolkata2024-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:

StudentNameCity
Ravi SharmaDelhi
Priya MehtaMumbai
Arjun NairChennai
Sneha KapoorPune
Rohit DasKolkata

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:

StudentNameAgeAgeAfter5Years
Ravi Sharma2025
Priya Mehta2227
Arjun Nair1924
Sneha Kapoor2126
Rohit Das2328

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:

StudentNameStatus
Ravi SharmaActive
Priya MehtaActive
Arjun NairActive
Sneha KapoorActive
Rohit DasActive

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:

OrderClausePurpose
1SELECTWhich columns to retrieve
2FROMWhich table to get data from
3WHEREFilter rows based on a condition
4GROUP BYGroup rows that share a value
5HAVINGFilter groups based on a condition
6ORDER BYSort the result
7LIMITRestrict 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:

  1. Selects three specific columns
  2. From the Students table
  3. Only includes students older than 20
  4. Sorts them by age in ascending order
  5. 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 SELECT list.
  • The AS keyword gives a column a custom label in the result output.
  • Clauses in a SELECT query 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.

Leave a Comment

Your email address will not be published. Required fields are marked *