How to return a boolean response for a unique and new entry I am looking for an SQL query to identify if there are any new classs in my table:

CREATE TABLE `Features` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `account_username` varchar(64) NOT NULL DEFAULT '',
  `class` int(11) DEFAULT NULL,
  `new` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;




Edge cases

Here class 3 has no previously new=0 entries. So the SQL should return 1.

| class | new |
|-------|-----|
|     1 |   0 |
|     2 |   0 |
|     3 |   1 |


Here class 2 has already a new=0. So the SQL statement should return 0.

| class | new |
|-------|-----|
|     1 |   0 |
|     2 |   0 |
|     2 |   1 |


Here there are no new classes. So the SQL statement should return 0. 

| class | new |
|-------|-----|
|     1 |   0 |
|     2 |   0 |
|     3 |   0 |


Here all classes are new. So the SQL statement should return 1. 

| class | new |
|-------|-----|
|     1 |   1 |
|     2 |   1 |
|     3 |   1 |




This query should run on a single account_username.
If I understand your question correctly, you want to select a boolean value based on whether another query (that determines if there are any new entries in the table based on some new flag) selects any rows.

Someone could probably do this in a more optimised way, but it could be achieved by doing this:

SELECT CASE
           WHEN COUNT(y.class) &gt; 0 THEN
               1
           ELSE
               0
       END
FROM (SELECT class
    FROM Features
    WHERE account_username = 'someusername'
    GROUP BY class) y
    LEFT JOIN
    (
        SELECT class
        FROM Features
        WHERE new = 0 AND account_username = 'someusername'
        GROUP BY class
    ) x
        ON x.class = y.class
WHERE x.class IS NULL


This selects 1 if the inner query selects any rows, otherwise 0. The inner query selects the rows that have a new flag associated with them by performing a left join on all the possible classes and all the classes where the flag is 0 where the x.class is null. 

To run it on a single username, create a stored procedure and parameterise the username column. 

Hope this helps!