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

Basic data retrieval in SQL is straightforward with the SELECT statement, which helps you fetch data from one or more columns. To avoid duplicates, use SELECT DISTINCT, and when you need to filter your results, the WHERE clause is essential. By understanding these basics, you can start building more complex queries to manipulate and retrieve the data you need efficiently.

Knowledge Check

Related Posts

Leave a Reply

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