
- 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
Write a single MySQL query to return the ID from the corresponding row which is a NOT NULL value
Let us first create a table −
mysql> create table DemoTable ( StudentId int, StudentName varchar(100) ); Query OK, 0 rows affected (0.85 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(NULL,'Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL,'Robert'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(101,'David'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(102,'Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(103,'Sam'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | NULL | Chris | | NULL | Robert | | 101 | David | | 102 | Mike | | 103 | Sam | +-----------+-------------+ 5 rows in set (0.00 sec)
Following is the query to return ID from the corresponding row which is a NOT NULL value −
mysql> select StudentId from DemoTable where StudentName='David' and StudentId IS NOT NULL;
This will produce the following output −
+-----------+ | StudentId | +-----------+ | 101 | +-----------+ 1 row in set (0.00 sec)
- Related Articles
- Return value from a row if it is NOT NULL, else return the other row value in another column with MySQL
- A single MySQL query to combine strings from many rows into a single row and display the corresponding User Id sum in another column?
- Write a single MySQL query to exclude a record and display NULL value
- How to retrieve the corresponding value for NULL with a MySQL query?
- MySQL query to return all items in a single row
- Can we update a row with the highest ID in a single MySQL query?
- Count NOT NULL values from separate tables in a single MySQL query
- MySQL query to return the count of only NO values from corresponding column value
- How to check if any value is Null in a MySQL table single row?
- MySQL query to merge rows if Id is the same and display the highest corresponding value from other columns
- Ignore NULL values from separate tables in a single MySQL query and display count of NOT NULL records
- Return only a single row from duplicate rows with MySQL
- Display NULL and NOT NULL records except a single specific value in MySQL
- Get only a single value from a specific MySQL row?
- MySQL query to concatenate records with similar corresponding ids in a single row separated by a special character

Advertisements