đ The SELECT statement is the most commonly used SQL command. It is used to retrieve data from one or more tables in a relational database. Whether you want to display all records, fetch specific columns, filter rows, sort results, or perform calculations, the SELECT statement is the foundation of SQL querying.
đ What is the SELECT Statement?
The SELECT statement retrieves data stored in database tables. By default, it returns all matching rows based on the specified conditions. You can retrieve every column or choose only the columns you need.
Information
đ Basic Syntax
General Syntax of SELECT
SELECT column1, column2, ...
FROM table_name;To retrieve all columns from a table, use the wildcard character *.
Select All Columns
SELECT *
FROM Students;đ Sample Table
Assume the following Students table exists:
| StudentID | Name | Department | Marks |
|---|---|---|---|
| 101 | Alice | Computer Science | 92 |
| 102 | Bob | Mathematics | 85 |
| 103 | Charlie | Physics | 88 |
| 104 | David | Computer Science | 95 |
đ¯ Selecting Specific Columns
Specify the required column names separated by commas to retrieve only selected data.
Select Specific Columns
SELECT StudentID, Name
FROM Students;đ¯ Using Column Aliases
Aliases provide temporary names for columns in the query result, making output more readable.
Using AS for Aliases
SELECT
StudentID AS ID,
Name AS StudentName,
Department AS Course
FROM Students;đ Filtering Records with WHERE
The WHERE clause retrieves only the rows that satisfy a specified condition.
Using WHERE
SELECT *
FROM Students
WHERE Department = 'Computer Science';đ Sorting Results with ORDER BY
The ORDER BY clause arranges the query results in ascending ( ASC) or descending ( DESC) order.
Sort by Marks
SELECT Name, Marks
FROM Students
ORDER BY Marks DESC;đ§Ž Using Expressions in SELECT
You can perform calculations and return computed values directly in a SELECT statement.
Calculate Bonus Marks
SELECT
Name,
Marks,
Marks + 5 AS BonusMarks
FROM Students;đ Selecting Distinct Values
The DISTINCT keyword removes duplicate values from the result set.
Using DISTINCT
SELECT DISTINCT Department
FROM Students;đ Limiting the Number of Rows
Many database systems allow limiting the number of returned rows. The syntax may vary depending on the database.
| Database | Example |
|---|---|
| MySQL / PostgreSQL / SQLite | LIMIT 5 |
| SQL Server | TOP 5 |
| Oracle (12c+) | FETCH FIRST 5 ROWS ONLY |
Using LIMIT
SELECT *
FROM Students
LIMIT 2;đ Combining Multiple Clauses
A single SELECT statement can include multiple clauses to retrieve precise results.
Complete SELECT Example
SELECT
StudentID,
Name,
Marks
FROM Students
WHERE Department = 'Computer Science'
ORDER BY Marks DESC;đ Common SELECT Clauses
| Clause | Purpose |
|---|---|
| SELECT | Choose columns to retrieve. |
| FROM | Specify the source table. |
| WHERE | Filter rows. |
| DISTINCT | Remove duplicate values. |
| ORDER BY | Sort the result set. |
| GROUP BY | Group rows for aggregate functions. |
| HAVING | Filter grouped results. |
| LIMIT / TOP | Restrict the number of rows returned. |
đŧ Real-World Example
Imagine a university portal where students search for the top-performing Computer Science students. The application can retrieve the required information using a single SELECT query.
Real-World Example
SELECT Name,
Department,
Marks
FROM Students
WHERE Department = 'Computer Science'
ORDER BY Marks DESC;â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ SELECT retrieves data from database tables.
- đ Use * to retrieve all columns.
- đ Specify column names to retrieve only required data.
- đ Use WHERE to filter rows.
- đ Use ORDER BY to sort results.
- đ Use DISTINCT to eliminate duplicate values.
- đ Combine clauses to create powerful and efficient queries.