
One of the core operations in SQL is fetching data from a database. SQL provides several powerful clauses and keywords to retrieve and filter data efficiently. In this section, we’ll explore basic data retrieval using the SELECT statement, along with the SELECT DISTINCT and WHERE clauses.
1. The SELECT Statement
The SELECT statement is fundamental for retrieving data from a database. It enables you to define the specific columns you want to extract from a given table, making it one of the most frequently used commands in SQL.
Syntax:
SELECT column1, column2, ...
FROM table_name;
- column1, column2: Specify the columns you want to retrieve.
- table_name: The name of the table where the data is stored.
Example:
Suppose we have a table called Employees:
| EmployeeID | FirstName | LastName | Department | Salary |
| 1 | John | Doe | HR | 50000 |
| 2 | Jane | Smith | IT | 60000 |
| 3 | Robert | Johnson | Finance | 70000 |
| 4 | Emily | Davis | IT | 60000 |
To retrieve the FirstName and Salary of all employees, we use:
SELECT FirstName, Salary
FROM Employees;
Result:
| FirstName | Salary |
| John | 50000 |
| Jane | 60000 |
| Robert | 70000 |
| Emily | 60000 |
2. The SELECT DISTINCT Statement
When retrieving data, you may encounter duplicate values in one or more columns. SELECT DISTINCT is used to return only unique (distinct) values, filtering out duplicates.
Syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;
Result:
| Department |
| HR |
| IT |
| Finance |
Even though there are two employees in the IT department, SELECT DISTINCT returns only one row for the IT department.
3. The WHERE Clause
The WHERE clause is used to filter records and retrieve only those that meet a specific condition. You can use comparison operators like =, >, <, >=, <=, and logical operators like AND, OR, and NOT to construct conditions.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
condition: A logical expression that must be true for the row to be included in the result set.
Example 1 (Single Condition):
To retrieve employees from the Employees table who work in the IT department:
SELECT FirstName, LastName
FROM Employees
WHERE Department = 'IT';
Result:
| FirstName | LastName |
| Jane | Smith |
| Emily | Davis |
Example 2 (Multiple Conditions):
To retrieve employees who work in the IT department and have a salary greater than 55000:
SELECT FirstName, LastName, Salary
FROM Employees
WHERE Department = 'IT' AND Salary > 55000;
Result:
| FirstName | LastName | Salary |
| Jane | Smith | 60000 |
| Emily | Davis | 60000 |
Example 3 (Using OR):
To retrieve employees who either work in the Finance department or have a salary greater than 60000:
SELECT FirstName, LastName, Department, Salary
FROM Employees
WHERE Department = 'Finance' OR Salary > 60000;
Result:
| FirstName | LastName | Department | Salary |
| Robert | Johnson | Finance | 70000 |
Using Comparison Operators with WHERE
- =: Equal to.
- <> or !=: Not equal to.
- <: Less than.
- <=: Less than or equal to.
- >: Greater than.
- >=: Greater than or equal to.
Example:
To retrieve employees with a salary less than or equal to 60000:
SELECT FirstName, LastName, Salary
FROM Employees
WHERE Salary <= 60000;
Result:
| FirstName | LastName | Salary |
| John | Doe | 50000 |
| Jane | Smith | 60000 |
| Emily | Davis | 60000 |
Conclusion
SQL data retrieval is one of the most important skills for working with relational databases. The SELECT statement allows you to fetch data efficiently, while DISTINCT helps eliminate duplicate records and WHERE enables precise filtering of results. Furthermore, mastering these fundamental SQL commands provides the foundation for advanced concepts such as JOINs, subqueries, aggregation, and database reporting. By practicing these techniques regularly, you can write efficient SQL queries and retrieve exactly the data you need from any database.
Knowledge Check
Frequently Asked Questions
SQL data retrieval is the process of extracting information from database tables using SQL queries, primarily through the SELECT statement.
The SELECT statement is used to retrieve one or more columns of data from a database table.
Use DISTINCT when you want to return only unique values and remove duplicate records from query results.
The WHERE clause filters records based on specified conditions, allowing you to retrieve only relevant data.


Most Commented