Introduction
There are several clauses available in sequel for sorting and filtering data. In the last article, we discussed about the ORDER BY clause which is one of the most useful clauses in SQL when it comes to sorting and filtering data. In addition to the ORDER BY clause, we also have the WHERE clause and the SELECT DISTINCT clause. In this article, we shall discuss in details the meaning of these two clauses and their respective syntax.
The WHERE Clause
The WHERE clause is used to specify conditions for filtering records in a query. In other words, it filters records in SQL query based on a specified condition. Its a clause you use most times with the SELECT SQL COMMAND and the UPDATE SQL COMMAND. The ultimate goal of the WHERE clause is to extract only the records that meet your criteria or condition, allowing for a more precise data retrieval. It’s syntax is written the following way:
SELECT column1_name, column2_name FROM table_name WHERE condition = ?;
For example, you can use the following syntax to retrieve from a table the name of employees whos ages are more than 30.
SELECT * FROM employees WHERE age > 30;
The SELECT DISTINCT clause
The SELECT DISTINCT clause is a clause in the Structured Query Language used to remove duplicate rows from the result set of a query. It ensures that the query returns only unique values for the specified columns. Simply put, if you want to get the list of unique items in a table, you use the SELECT DISTINCT clause. Its syntax should be written as:
SELECT DISTINCT column1, column2
FROM table_name;
Conclusion
Every database needs to be sorted accordingly and filtered whether its the arrangement of the database or how to select certain subjects from the database, sorting and filtering is an essential part to really relating well with your database. In this article, we covered the various clause in MySQL that can be used to sort and filter your data in a database.