SELECT Command
The SELECT command is one of the most fundamental and frequently used commands in SQL.
It is used to retrieve data from a database table.
In simple terms, whenever you want to view or read data stored in a table, you use the SELECT command.
Example: Students Table
Syntax
Selecting All Columns from a Table
To retrieve all columns from a table, use the asterisk (*) symbol.
Output
Output
Selecting Multiple Columns
Output
Important: The SELECT command does not modify the data. It only displays the requested information.
Understanding Database and Table
Before learning SELECT, it is important to understand two basic terms:
- Database: A structured place where data is stored.
- Table: A collection of data organized in rows and columns.
Think of a table as a spreadsheet:
- Columns represent fields (such as name, age, marks).
- Rows represent individual records.
Example: Students Table
| id | name | marks |
|---|---|---|
| 1 | Rahul | 85 |
| 2 | Anjali | 90 |
Why Use the SELECT Command?
The SELECT command is used to:
- Retrieve data from a table
- Display specific or all columns
- Generate reports
- Analyze stored information
- Safe to use
- Read-only operation
- Does not change database data
Syntax
SELECT column_nameFROM table_name;- SELECT specifies what data to retrieve
- FROM specifies which table contains the data
Selecting All Columns from a Table
To retrieve all columns from a table, use the asterisk (*) symbol.
SELECT * FROM students;Output
| id | name | marks |
|---|---|---|
| 1 | Rahul | 85 |
| 2 | Anjali | 90 |
The * symbol means all columns.
Selecting Specific Columns
To retrieve data from specific columns only, mention the column names separated by commas.
Example
Example
SELECT name FROM students;Output
name Rahul Anjali
Selecting Multiple Columns
SELECT name, marks FROM students;Output
name marks Rahul 85 Anjali 90
Important Points to Remember
- SQL keywords are typically written in uppercase (recommended practice)
- Column and table names must match the database exactly
- The SELECT command only retrieves data
- Using SELECT * is convenient for learning but not recommended for large databases
Two Minute Drill
- SELECT is used to retrieve data from a table
- It does not modify or delete data
- SELECT* retrieves all columns
- Specific columns can be retrieved by listing their names
- FROM specifies the source table
- Tables store data in rows and columns