Sorting And Filtering Data Using The WHERE And SELECT DISTINCT Clause in MYSQL.

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.

Olamide Ayeni

Recent Posts

Building A Message Board App With Django

Introduction In this article, we shall use a database for the first time to build…

6 days ago

Building A Two Paged Web Application Using Django

Introduction In this article, we will build a pages app that has a homepage and…

2 weeks ago

Creating Your First App In Django

When you say you want to use Django, it means you want to build a…

3 weeks ago

Creating Your First Project In Django?

Introduction Django is a high level Python framework that encourages rapid development and clean, pragmatic…

4 weeks ago

Django Setup For Windows

Django Setup To work with Django, we have to install Django first. Now when you…

4 weeks ago

Why Choose Django When There Are Other Frameworks For Python?

Why choose Django when you have other frameworks for Python? Django, Django, Django. When it…

1 month ago