![]() |
The Count() function in MYSQL is an inbuilt function that helps count the number of rows or values in a table. It can count based on the specific conditions given by the user which can help in targetted operations. There are certain times when we just need the values that satisfy a specific condition and not all the values of the column. This function is available in the 4.0 and above versions of MySQL. In this article, This article delves into the concept of conditional counting in MySQL and demonstrates its practical usage. we will see the concept of the count() function with specific conditions and see the syntax with the output. COUNT() with Condition in MySQLThe count() functions without any condition will just count all the rows in a specified table. But since we need to see the syntax when we put some conditions in the function. The general syntax looks like this: Syntax:
Another way to show the COUNT() Function: Syntax:
Here, if the case statement is true then the count is incremented or else it is not incremented. Examples of Count() in MySQLLet’s take a table of Shippings with column names as shipping_id, status, and customer. We will add some values to all the columns and use the count() function to count the number of customers whose status is ‘Pending‘ and whose customer is greater than 3. CREATE TABLE Shippings ( There are some values inserted in the table and it looks something like this. INSERT INTO Shippings (shipping_id, status, customer) Output: ![]() Shippings table Example 1: Count of Shipped Items with ‘Pending’ Status for Customers > 3Now we apply the count() function with the condition. We will be using the first method here. SELECT COUNT(*) AS shipped_items_count Output: +---------------------+ Explanation: As we can see there are two rows with status as pending and customer value more than 3. One with the shipping_id as 2 and the other one with 4. The output comes as 2 as shown above which is the count. Example 2: Count of High-Value Orders in the “Orders” TableLet’s take a table with the name Orders and columns as order_id, item, amount, and customer_id. We will insert some values into the table and count the values when the amount is greater than 350. CREATE TABLE Orders ( There are some values inserted in the table and it looks something like this. INSERT INTO Orders(order_id,item,amount,customer_id) Output: ![]() orders table We will be using the second method here for counting the values. SELECT COUNT(CASE WHEN amount > 350 THEN 1 ELSE NULL END) AS high_value_orders Output: +-------------------+ Explanation: As we can see from the table there are 3 rows with an amount of more than 350. There are 3 rows with amounts more than 350 with order_id 1.3 and 4. Hence the output comes as 3. Example 3: Customer Counts Based on Different ConditionsLet’s take a table for Customers with columns customer_id, first_name, last_name, age, and country. In this example, we will see how to work with the count() function based on certain conditions. CREATE TABLE Customers( We will insert some values into the table INSERT INTO Customers(customer_id,first_name,last_name,age,country) Output: ![]() Customers table 1. Counting Rows Based on a Single Condition SELECT COUNT(*) FROM Customers WHERE country = 'USA';
Output: +----------+ Explanation: As we can see there are 2 values where the country is the USA. Hence the output is 2. 2. Counting Rows Based on Multiple ConditionsSELECT COUNT(*) FROM Customers WHERE country = 'USA' AND age > 30;
Output: +----------+ Explanation: As we can see there is one value where the country is USA and age is greater than 30. Hence the output comes as 1. 3. Counting Rows with NULL ValuesSELECT COUNT(*) FROM Customers WHERE last_name IS NULL;
Output: +----------+ Explanation: As we can see there are 0 values where last name is null. Hence the output is 0. 4. Counting Rows Excluding NULL ValuesSELECT COUNT(*) FROM Customers WHERE last_name IS NOT NULL;
Output: +----------+ Explanation: There are 5 values where last_name is not NULL. Hence, the output is 5. ConclusionIn conclusion, MySQL allows you to specify conditions within the FAQs on Conditional Counting in MySQL Using COUNT() FunctionWhat is the purpose of the
|
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |