In a Database Management System (DBMS), data stored in tables must often be retrieved for viewing, analysis, or processing. The SELECT statement is used to fetch data from one or more tables in a database.
The SELECT query is one of the most commonly used SQL commands because it allows users to view stored information without modifying the data.
The SELECT query is a Data Manipulation Language (DML) command used to retrieve data from a table in a database.
It allows users to display specific columns or all columns from one or more tables.
SELECT column1, column2, column3
FROM table_name;
Where:
To retrieve all columns from a table, the asterisk (*) symbol is used.
SELECT * FROM table_name;
Suppose there is a table named students.
| id | name | age | course |
|---|---|---|---|
| 1 | Rahul | 20 | BCA |
| 2 | Anita | 21 | BBA |
| 3 | Rohit | 19 | BCA |
Retrieve all records:
SELECT * FROM students;
| id | name | age | course |
|---|---|---|---|
| 1 | Rahul | 20 | BCA |
| 2 | Anita | 21 | BBA |
| 3 | Rohit | 19 | BCA |
You can retrieve only specific columns instead of the entire table.
SELECT name, course
FROM students;
| name | course |
|---|---|
| Rahul | BCA |
| Anita | BBA |
| Rohit | BCA |
The DISTINCT keyword is used to remove duplicate values from the result.
SELECT DISTINCT column_name
FROM table_name;
SELECT DISTINCT course
FROM students;
| course |
|---|
| BCA |
| BBA |
SELECT is a DML command used to retrieve data.* symbol is used to select all columns.The SELECT query is used to retrieve data from tables in a database. It allows users to view complete records or specific columns depending on their requirements. Because of its flexibility and importance in data retrieval, the SELECT statement is considered one of the core commands in SQL.