Syntax error MySQL query to return the count of only NO values from corresponding column value

MySQL query to return the count of only NO values from corresponding column value



Let us first create a table −

mysql> create table DemoTable1829
     (
     Name varchar(20),
     isTopper ENUM('YES','NO')
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1829 values('Chris','yes');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1829 values('David','yes');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1829 values('Mike','no');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1829 values('David','yes');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1829;

This will produce the following output −

+-------+----------+
| Name  | isTopper |
+-------+----------+
| Chris | YES      |
| David | YES      |
| Mike  | NO   |
| David | YES      |
+-------+----------+
4 rows in set (0.00 sec)

Here is the query to return the count of only NO values −

mysql> select Name,sum(isTopper='no')
     from DemoTable1829
     group by Name;

This will produce the following output −

+-------+--------------------+
| Name  | sum(isTopper='no') |
+-------+--------------------+
| Chris |                  0 |
| David |                  0 |
| Mike  |                  1 |
+-------+--------------------+
3 rows in set (0.00 sec)
Updated on: 2019-12-24T07:45:54+05:30

144 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements