{"id":413295,"date":"2024-02-28T16:22:18","date_gmt":"2024-02-29T00:22:18","guid":{"rendered":"https:\/\/linuxhint.com\/?p=413295"},"modified":"2024-02-29T18:16:23","modified_gmt":"2024-03-01T02:16:23","slug":"sql-is-not-null","status":"publish","type":"post","link":"https:\/\/linuxhint.com\/sql-is-not-null\/","title":{"rendered":"SQL \u201cIs Not Null\u201d Operator"},"content":{"rendered":"<div id=\"wpbody\">\n<p>In SQL, we can use the IS NOT NULL operator to filter out the results from a database table where the value of a specific column is not null. This can help to ensure that you get a clean data that does not contain any missing values which can cause problems especially in functions that do not handle NULL values.<\/p>\n<p>A NULL value refers to the absence of data which does not incorporate the empty strings, numerical zero, or NaN.<\/p>\n<p>In this tutorial, we will learn how we can use the IS NOT NULL operator to filter out the results that contain NULL values in a given query.<\/p>\n<h2>Syntax:<\/h2>\n<p>We can express the basic syntax of the IS NOT NULL condition as shown in the following code snippet:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">SELECT column1, column2, ...<br \/>\nFROM table_name<br \/>\nWHERE column_name IS NOT NULL;<\/div><\/div>\n<p>&nbsp;<br \/>\nWe start with the \u201cselect\u201d keyword and specify the columns that we wish to retrieve in the result set followed by the name of the table from which we wish to fetch the data.<\/p>\n<p>Next, we use the WHERE clause to introduce the data filtering based on a specific column. Finally, we specify the condition on which we wish to filter.<\/p>\n<h2>Example 1:<\/h2>\n<p>Let us look at some examples on how we can use the IS NOT NULL condition to filter out the results.<\/p>\n<p>Consider the Sakila sample database which is available freely for download on the official MySQL page.<\/p>\n<p>Suppose we wish to retrieve the name of customers whose \u201clast_name\u201d is not equal to null. We can use the query as shown in the following to accomplish this:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw1\">select<\/span><br \/>\n&nbsp; &nbsp; <span class=\"sy0\">*<\/span><br \/>\nfrom<br \/>\n&nbsp; &nbsp; customer c<br \/>\nwhere<br \/>\n&nbsp; &nbsp; last_name is not null;<\/div><\/div>\n<p>&nbsp;<br \/>\nIn this case, the query returns all the rows from the customer table where the value of the \u201clast_name\u201d column is not null.<\/p>\n<h2>Example 2: AND and OR Operators<\/h2>\n<p>We can combine the IS NOT NULL condition with other conditions using the AND and OR operators. This creates a more granular filtering.<\/p>\n<p>For example, suppose we want to retrieve the customers whose \u201clast_name\u201d is not null and whose first name is Nancy or Holly.<\/p>\n<p>We can use a query as demonstrated in the following:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">SELECT<br \/>\n&nbsp; &nbsp; customer_id,<br \/>\n&nbsp; &nbsp; first_name,<br \/>\n&nbsp; &nbsp; last_name,<br \/>\n&nbsp; &nbsp; email<br \/>\nFROM<br \/>\n&nbsp; &nbsp; customer<br \/>\nWHERE<br \/>\n&nbsp; &nbsp; last_name IS NOT NULL<br \/>\n&nbsp; &nbsp; AND <span class=\"br0\">&#40;<\/span>first_name = <span class=\"st_h\">'Nancy'<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; OR first_name = <span class=\"st_h\">'Holly'<\/span><span class=\"br0\">&#41;<\/span>;<\/div><\/div>\n<p>&nbsp;<br \/>\nIn this query, we are combining the IS NOT NULL condition with the AND and OR operators to filter out the records where the last name is not null or the first name is equal to either Nancy or Holly.<\/p>\n<p>The resulting output is as follows:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture1-18.png\" alt=\"\" width=\"624\" height=\"87\" class=\"alignnone size-full wp-image-413296\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture1-18.png 624w, https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture1-18-300x42.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><br \/>\nAs you can see, this provides a way of performing a more granular and miniscule filtering on the target data.<\/p>\n<h2>Example 3: Using the Aggregate Functions<\/h2>\n<p>We can also use the IS NOT NULL function in conjunction with the SQL aggregate functions. For example, we can use it with the count() function to count the number of non-null values in a given column.<\/p>\n<p>For example, suppose we want to determine the number of customers with non-null email addresses. We can use the query as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">SELECT<br \/>\n&nbsp; &nbsp; COUNT<span class=\"br0\">&#40;<\/span>customer_id<span class=\"br0\">&#41;<\/span> AS total<br \/>\nFROM<br \/>\n&nbsp; &nbsp; customer<br \/>\nWHERE<br \/>\n&nbsp; &nbsp; email IS NOT NULL;<\/div><\/div>\n<p>&nbsp;<br \/>\nThis should return the number of non-null values in the column as a numerical value as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">total<span class=\"sy0\">|<\/span><br \/>\n-----+<br \/>\n&nbsp; <span class=\"nu0\">599<\/span><span class=\"sy0\">|<\/span><\/div><\/div>\n<p>&nbsp;<\/p>\n<h2>Conclusion<\/h2>\n<p>In this tutorial, we learned all about the IS NOT NULL condition in SQL to filter out the null values from a given result set or database table. We also learned how we can create a more complex filtering by combining the IS NOT NULL condition with other conditions using the AND and OR operators.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial on how we can use the IS NOT NULL operator to filter out the results that contain NULL values in a result set or database table along with examples.<\/p>\n","protected":false},"author":111,"featured_media":413305,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_wp_convertkit_post_meta":{"form":"-1","landing_page":"","tag":"0","restrict_content":"0"},"footnotes":""},"categories":[2451],"tags":[],"class_list":["post-413295","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql-standard"],"_links":{"self":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/413295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/comments?post=413295"}],"version-history":[{"count":0,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/413295\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media\/413305"}],"wp:attachment":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media?parent=413295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/categories?post=413295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/tags?post=413295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}