
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How MySQL evaluates when I use a conditional expression within SUM() function?
As we know that, by using a conditional expression within SUM() function we can get the number of rows that meet the condition. So, in this case, MySQL evaluates to 1 each time the condition is true and 0 each time it is false.
To understand it, consider the following example of table ‘employee’, having the following details −
mysql> Select * from Employee; +----+--------+--------+ | ID | Name | Salary | +----+--------+--------+ | 1 | Gaurav | 50000 | | 2 | Rahul | 20000 | | 3 | Advik | 25000 | | 4 | Aarav | 65000 | | 5 | Ram | 20000 | | 6 | Mohan | 30000 | | 7 | Aryan | NULL | | 8 | Vinay | NULL | +----+--------+--------+ 8 rows in set (0.00 sec)
Now, suppose if we want to know the total number of rows having a salary more than 20000 then the conditional expression can be used within SUM() function as follows −
mysql> Select SUM(Salary>20000) from employee568; +-------------------+ | SUM(Salary>20000) | +-------------------+ | 4 | +-------------------+ 1 row in set (0.00 sec)
The above result set shows that 4 employees are getting more than 20000 salaries.
- Related Articles
- How MySQL evaluates if I will use an expression within SUM() function?
- How can I use MySQL IF() function within SELECT statement?
- How MySQL SUM() function evaluates if the column having NULL values too?
- How can we use MySQL SUM() function?
- How MySQL SUM() function evaluates if it got the column, having character data type, as its argument?
- How MySQL SUM() function evaluates if it is used with SELECT statement that returns no matching rows?
- How to write a conditional expression in lambda expression in Java?
- How can I use another MySQL function/s with REPEAT() function?
- When should I use a composite index in MySQL?
- How can we use MySQL SUM() function with HAVING clause?
- How do I use the conditional operator in C/C++?
- When should I use MySQL compressed protocol?
- How can I use SPACE() function with MySQL WHERE clause?
- How can I use MySQL INTERVAL() function with a column of a table?
- Can I use SUM() with IF() in MySQL?

Advertisements