SQL Data Retrieval: SELECT, DISTINCT, & WHERE

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:

EmployeeIDFirstNameLastNameDepartmentSalary
1JohnDoeHR50000
2JaneSmithIT60000
3RobertJohnsonFinance70000
4EmilyDavisIT60000

To retrieve the FirstName and Salary of all employees, we use:

SELECT FirstName, Salary
FROM Employees;

Result:

FirstNameSalary
John50000
Jane60000
Robert70000
Emily60000

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:

FirstNameLastName
JaneSmith
EmilyDavis

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:

FirstNameLastNameSalary
JaneSmith60000
EmilyDavis60000

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:

FirstNameLastNameDepartmentSalary
RobertJohnsonFinance70000

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:

FirstNameLastNameSalary
JohnDoe50000
JaneSmith60000
EmilyDavis60000

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

What is SQL data retrieval?

SQL data retrieval is the process of extracting information from database tables using SQL queries, primarily through the SELECT statement.

What is the purpose of the SELECT statement in SQL?

The SELECT statement is used to retrieve one or more columns of data from a database table.

When should I use the DISTINCT keyword?

Use DISTINCT when you want to return only unique values and remove duplicate records from query results.

How does the WHERE clause work in SQL?

The WHERE clause filters records based on specified conditions, allowing you to retrieve only relevant data.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *