SELECT in SQL

🔍 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

SELECT belongs to Data Query Language (DQL) because it is used to query and retrieve data without modifying it.

📝 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:

StudentIDNameDepartmentMarks
101AliceComputer Science92
102BobMathematics85
103CharliePhysics88
104DavidComputer Science95

đŸŽ¯ 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.

DatabaseExample
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

ClausePurpose
SELECTChoose columns to retrieve.
FROMSpecify the source table.
WHEREFilter rows.
DISTINCTRemove duplicate values.
ORDER BYSort the result set.
GROUP BYGroup rows for aggregate functions.
HAVINGFilter grouped results.
LIMIT / TOPRestrict 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

Retrieve only the columns you need instead of using SELECT * in production applications. Use meaningful aliases, format queries consistently, apply WHERE clauses to reduce unnecessary data retrieval, and use ORDER BY only when sorted output is required.

🚀 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.
>>"The SELECT statement is the gateway to exploring and understanding data stored in a database."

Summary

✅ The SELECT statement is the foundation of SQL. It enables you to retrieve, filter, sort, and organize data efficiently. Mastering SELECT is essential before learning advanced SQL concepts such as joins, subqueries, and window functions.