DATA with BARAA

SQL SQL Aggregation Functions

In this tutorial, you will learn how to filter rows based on specified conditions using the WHERE clause. 

Once you know how to fetch your data using  SELECT and FROM and the next step is to learn how to filter your data using WHERE clause.

COUNT() Returns the number of items in a set
SUM() Returns the sum of all or distinct values in a set
AVG() Returns the average of a set
MAX() Returns the minimum value in a set
MIN() Returns the maximum value in a set

In the previous tutorial, you learned how to retrieve all your rows from tables, but in real-world scenarios, we usually select only the rows which fulfill certain conditions like customers who come from a certain country.

The basic syntax of the WHERE clause to filter the data returned by a query can be given with:

				
					SELECT column_list
FROM table_name
WHERE condition
				
			

It’s real easy to read in a plain-English way: select some columns from a table, except that the results will only include rows where that fulfill my conditions.

  • The clauses need to be in this order e SELECT  FROM WHERE
  • The WHERE clause  appears immediately after the FROM clause
  • In WHERE clause, you can specify one or more comparisons and logical operators.

To understand the ORDER BY statement in a better way, let’s look at the following customers and orders tables in our tutorial database:

Now, let’s check out some examples that demonstrate how it actually works.

.

SQL COUNT

We have the following task be to solve using SQL statements

Find the total number of customers

The following SQL statement will returns all customers from customers table and order the result by the score column in ascending order.

You can have the same the result set by skipping the ASC, because it is the default option in ORDER BY.

				
					SELECT COUNT(*) AS total_customers
FROM customers
				
			

After executing the above query, you’ll get the result set something like this:

Find the total quantity of all orders
				
					SELECT SUM(quantity) AS sum_quantity
FROM orders
				
			

As you can see the output contains everything the whole customers tables including all rows and columns.

Similarly, you can use the DESC option to perform a sorting in descending order. The following statement will orders the result set by the numeric salary column in descending order.

Find the average of score of all customers
				
					SELECT AVG(score) AS avg_score
FROM customers
				
			
Find the highest score of all customers
				
					SELECT MAX(score) AS max_score
FROM customers
				
			

Once you specify multiple columns after ORDER BY, the Database will sort the result by the first column, then the new ordered list will be sorted again by next column.

Operators in WHERE Clause

Find the lowest score of all customers

You can filter your results in a number of ways using comparison and logical operators, which you’ll learn about in the next tutorials. I summarized in the following table the most important ones.

				
					SELECT MIN(score) AS min_score
FROM customers
				
			
.
Share it !