Checking your session…
Module 05: SQL Command Classifications

5.3 DQL — SELECT

Level: 5 | Version: v1.1 | Author: Meptrasoft

Overview

SELECT is used to retrieve data from a database.

It can return all columns, specific columns, or only rows that match a condition.

TEXT
SELECT
  ↓
FROM
  ↓
WHERE
  ↓
RESULT

SELECT = Retrieve the data you need.

SQL SELECT — How a Query Retrieves Data: Source Table, SQL Query (SELECT, FROM, WHERE), and Result
Figure 1: A SELECT query chooses data from a table and can filter the rows returned.

Practice Table

This topic reuses the employees table from 5.2 DDL.

emp_id emp_name department salary
1 Ravi Kumar IT 45000.00
2 Anjali Mehta HR 38000.00
3 Suresh Rao Finance 42000.00
4 Priya Nair IT 50000.00

Basic SELECT

To retrieve all columns:

SQL
SELECT *
FROM employees;

* means all columns.


SELECT Specific Columns

You can choose only the columns you need:

SQL
SELECT emp_name, department
FROM employees;

Result:

emp_name department
Ravi Kumar IT
Anjali Mehta HR
Suresh Rao Finance
Priya Nair IT

This is usually clearer than selecting unnecessary columns.


SELECT with WHERE

WHERE is used to filter rows.

SQL
SELECT *
FROM employees
WHERE department = 'IT';

Result:

emp_id emp_name department salary
1 Ravi Kumar IT 45000.00
4 Priya Nair IT 50000.00

Simple Idea

TEXT
FROM   → Which table?
SELECT → Which columns?
WHERE  → Which rows?

Logical Query Processing Order

The SQL statement is written in one order, but the database conceptually processes its clauses in a different logical order.

A simplified order is:

TEXT
FROM / JOIN
      ↓
WHERE
      ↓
GROUP BY
      ↓
HAVING
      ↓
SELECT
      ↓
ORDER BY
      ↓
LIMIT

For example, the database first determines the source rows, applies filtering, performs grouping when required, and then produces the selected output.

The order you write SQL and the logical order used to evaluate it are not always the same.

Note: The exact physical execution may differ because the optimizer can choose a different execution plan.


Common Beginner Mistakes

✗ Mistake ✓ Correct Understanding
SELECT * means select all tables It means all columns from the selected source rows
WHERE chooses columns WHERE filters rows
FROM filters the result FROM identifies the source table or tables
SELECT always returns every row WHERE or other clauses can reduce the returned rows
SQL always executes exactly in the written order SQL has a logical processing order, and the optimizer chooses the physical execution plan
SELECT changes stored data SELECT normally retrieves data without modifying it

Placement Quick Points

TEXT
SELECT
→ CHOOSE COLUMNS

FROM
→ CHOOSE SOURCE

WHERE
→ FILTER ROWS
  • SELECT retrieves data.
  • FROM identifies the source table or tables.
  • WHERE filters rows.
  • * means all selected columns.
  • You can select only the columns needed for the result.
  • SQL has a logical query-processing order that differs from the written order.

Interview Questions

What is SELECT?

SELECT is used to retrieve data from a database.

What is the purpose of FROM?

FROM identifies the table or other data source from which the query retrieves data.

What is the purpose of WHERE?

WHERE filters rows based on a condition.

What is the difference between SELECT and WHERE?
TEXT
SELECT → Which columns?
WHERE  → Which rows?
What is the logical order of processing a query?

A simplified order is:

TEXT
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
Does SELECT modify data?

Normally, no. SELECT retrieves data without changing the stored rows.


Practice & Hands-On Exercises

Using the employees table:

  1. Display all columns:
SQL
SELECT * FROM employees;
  1. Display only emp_name and salary:
SQL
SELECT emp_name, salary FROM employees;
  1. Display employees from the IT department:
SQL
SELECT * FROM employees WHERE department = 'IT';
  1. Display employees whose salary is greater than 40000:
SQL
SELECT * FROM employees WHERE salary > 40000;
  1. Display only emp_name for employees in HR:
SQL
SELECT emp_name FROM employees WHERE department = 'HR';
  1. Explain the difference between SELECT * and selecting specific columns.
  2. Identify the roles of SELECT, FROM, and WHERE in a query.

💡 Tip: Test your queries using the in-browser interactive runner above.


Key Takeaway

TEXT
SELECT → COLUMNS
FROM   → SOURCE
WHERE  → ROWS TO KEEP

SELECT is used to retrieve data, while FROM identifies the source and WHERE filters the rows.

End of DQL — SELECT