
In this guide, we present a detailed explanation of the MySQL Select and MySQL DISTINCT statements and provide concrete examples and applications.
MySQL SELECT statement
In MySQL, the SELECT statement is pivotal, serving to retrieve data from one or more tables in the database. For beginners trying to navigate through MySQL, understanding the SELECT statement is crucial as it forms the basis for extracting useful information from the database.
Basic Usage of MySQL Select Statement
Syntax
Example
Imagine we have a table named Employees with the following columns: ID, Name, Position, and Salary.
To select all employees’ names from this table:
This will retrieve a list of all names from the Employees table.
Adding Conditions with WHERE
Syntax
Example
To select names of employees who are Managers:
Using ORDER BY to Sort Results
Syntax
Example
To select all names from Employees, ordered by Salary in descending order:
Applying Aggregation Functions
You can use aggregate functions like SUM(), AVG(), MIN(), and MAX() to perform operations on data.
Example
To find the highest salary in the Employees table:
Joining Tables
When data is spread across multiple tables, you can use JOIN clauses to retrieve it in a coherent way.
Syntax
SELECT column1, column2, ...
FROM table1
JOIN table2 ON table1.column = table2.column
WHERE condition;
Example
Imagine a second table named Departments with columns: ID, EmployeeID, and DepartmentName.
To select all employees' names and their respective departments:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
JOIN Departments ON Employees.ID = Departments.EmployeeID;
Grouping Data with GROUP BY
Syntax
Example
To find the average salary for each position:
Filtering Grouped Results with HAVING
Syntax
SELECT column1, AGGREGATE_FUNCTION(column2)
FROM table_name
WHERE condition
GROUP BY column1
HAVING condition2;
Example
To find the positions with an average salary greater than 50000:
SELECT DISTINCT Statement in MySQL
The SELECT DISTINCT statement in MySQL is used to return distinct (different) values from a database table, filtering out any duplicate entries. This is particularly useful when you want to know the different kinds of data available in a column without repetition.
Basic Syntax
Here:
- SELECT DISTINCT: This combination of keywords tells MySQL to return unique non-duplicate values.
- column_name: The name of the column from which you want to retrieve values.
- table_name: The name of the table from which to retrieve data.
- WHERE: An optional clause to filter the results.
Example 1: Basic Usage
Imagine you have a table named Products that includes the following data:
ID | Name | Category |
---|---|---|
1 | Apple | Fruit |
2 | Banana | Fruit |
3 | Carrot | Vegetable |
4 | Apple | Fruit |
5 | Orange | Fruit |
To retrieve all distinct items from the Category column:
Output:
Example 2: Combining With WHERE Clause
The WHERE clause can be added to SELECT DISTINCT to filter the results based on a particular condition.
For example, to select distinct categories where the name is not 'Apple':
Output:
Example 3: Multiple Columns
You can also use SELECT DISTINCT with multiple columns, returning unique combinations of the columns.
Suppose you add a new column Color to the Products table:
ID | Name | Category | Color |
---|---|---|---|
1 | Apple | Fruit | Red |
2 | Banana | Fruit | Yellow |
3 | Carrot | Vegetable | Orange |
4 | Apple | Fruit | Green |
5 | Orange | Fruit | Orange |
To retrieve all distinct combinations of Category and Color:
Output:
| Category | Color |
|-----------|--------|
| Fruit | Red |
| Fruit | Yellow |
| Vegetable | Orange |
| Fruit | Green |
| Fruit | Orange |
Example 4: Counting Distinct Values
You can count the number of distinct values in a column using COUNT(DISTINCT column_name).
To count the number of distinct categories:
Output: