
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count values greater and less than a specific number and display count in separate MySQL columns?
For this, you can use COUNT() along with CASE STATEMENT. Let us first create a table −
mysql> create table DemoTable ( Score int ); Query OK, 0 rows affected (0.71 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.77 sec) mysql> insert into DemoTable values(48); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(59); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(33); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(38); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(35); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Score | +-------+ | 40 | | 48 | | 59 | | 33 | | 38 | | 89 | | 35 | +-------+ 7 rows in set (0.00 sec)
Following is the query to count values greater and less than a specific number and display count in separate columns −
mysql> select count( case when Score > 45 then 1 end) as CountOfValueGreaterThan45, count( case when Score <= 45 then 1 end) as CountOfValueLessThanOrEqualTo45 from DemoTable;
This will produce the following output −
+---------------------------+---------------------------------+ | CountOfValueGreaterThan45 | CountOfValueLessThanOrEqualTo45 | +---------------------------+---------------------------------+ | 3 | 4 | +---------------------------+---------------------------------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- Count the number of words having sum of ASCII values less than and greater than k in C++
- Count of alphabets having ASCII value less than and greater than k in C++
- MySQL select to count values equal to 0 and greater than 0 from a column?
- MySQL query to count the duplicate ID values and display the result in a separate column
- Count numbers with difference between number and its digit sum greater than specific value in C++
- Count duplicate ids and display the result in a separate column with MySQL
- Count pairs with sum as a prime number and less than n in C++
- Count values based on conditions and display the result in different columns with MySQL?
- MySQL GROUP BY with WHERE clause and condition count greater than 1?
- Ignore NULL values from separate tables in a single MySQL query and display count of NOT NULL records
- Queries for greater than and not less than using C++
- Count only null values in two different columns and display in one MySQL select statement?
- How to find repeated rows and display there count in a separate column with MySQL?
- Display the sum of positive and negative values from a column in separate columns with MySQL
- MySQL query to count where more than three columns values are true?
Advertisements