Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

Data retrieval

Data retrieval is one of the most important and frequently used operations in SQL. The SELECT statement is used to fetch data from one or more tables stored in a database. Almost every real-world SQL task involves selecting data, whether it is displaying employee details, generating reports, or filtering job candidates. Understanding the SELECT statement thoroughly is essential for writing effective SQL queries.


Basic SELECT Statement

The most basic form of data retrieval in SQL is selecting all columns from a table.

SELECT * FROM employees;

Explanation

In this query:

  • SELECT tells the database that we want to retrieve data.
  • The asterisk symbol * represents all columns in the table.
  • FROM employees specifies the table from which the data should be fetched.

This query retrieves every column and every row from the employees table. It is commonly used when we want to view all data or quickly inspect the contents of a table.

Important Notes

  • Using SELECT * is not recommended in large databases because it retrieves unnecessary data.
  • It can affect performance if the table contains many columns or records.
  • It is useful mainly for learning, testing, or small datasets.

Selecting Specific Columns

In real-world applications, we usually need only certain columns instead of all data.

SELECT first_name, last_name, salary 
FROM employees;

Explanation

This query retrieves only the selected columns:

  • first_name
  • last_name
  • salary

By selecting specific columns:

  • The result set becomes smaller.
  • Queries execute faster.
  • Output is more relevant and readable.

Real-World Example

If an HR manager wants to view only employee names and salaries, selecting unnecessary columns like email or hire date is avoided.


Using Column Aliases

Column aliases are used to rename column headings in the output result for better readability.

SELECT 
    first_name AS "First Name",
    last_name AS "Last Name",
    salary AS "Annual Salary"
FROM employees;

Explanation

  • AS is used to create an alias (temporary name).
  • The alias is shown only in the output, not in the table.
  • Quotation marks allow spaces in alias names.

This query does not change the actual column names in the database; it only changes how they appear in the result set.

Why Use Aliases?

  • Makes reports user-friendly
  • Improves readability for non-technical users
  • Useful in dashboards and export files

Important Rules

  • Aliases are temporary
  • You can skip AS keyword, but using it improves clarity
  • Aliases are especially useful when working with expressions and joins

Selecting Distinct Values

Sometimes a table contains repeated values in a column. To retrieve only unique values, SQL provides the DISTINCT keyword.

SELECT DISTINCT department 
FROM employees;

Explanation

  • DISTINCT removes duplicate entries.
  • It returns each unique value only once.
  • The result shows a list of all different departments in the company.

Real-World Use Case

  • Finding how many departments exist
  • Listing unique job roles
  • Identifying distinct locations or skills

Important Points

  • DISTINCT applies to the entire row if multiple columns are selected
  • It may slightly slow down queries on large tables
  • Often used with aggregate functions and reporting

How SELECT Works Internally (Simple Flow)

  1. SQL reads the SELECT clause
  2. Identifies columns to retrieve
  3. Reads data from the specified table
  4. Removes duplicates if DISTINCT is used
  5. Displays the final result

Common Mistakes by Beginners

  • Using SELECT * unnecessarily
  • Forgetting column names
  • Misspelling table or column names
  • Misusing aliases as real column names
  • Expecting aliases to change table structure

Best Practices for SELECT Queries

  • Always select only required columns
  • Use meaningful aliases
  • Avoid SELECT * in production
  • Use DISTINCT only when needed
  • Format queries for readability

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥