SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Null Values: Disallowing Null Values,
Comparisons Using Null Values
Dr. Shalini Gambhir
• In SQL, you can disallow null values in a
column by using the NOT NULL constraint
when creating or altering a table. This
constraint ensures that every row in the table
must have a value for that column and that
value cannot be NULL.
Here's an example of creating a table
with the NOT NULL constraint:
• CREATE TABLE Employees (
• EmployeeID INT PRIMARY KEY,
• FirstName VARCHAR(50) NOT NULL,
• LastName VARCHAR(50) NOT NULL,
• Department VARCHAR(50)
• );
• In this example, both FirstName and LastName
columns are specified as NOT NULL, meaning that
whenever a new record is inserted into the
Employees table, these columns must have a
value, and NULL values are not allowed.
• If you try to insert a record without providing
a value for a column with the NOT NULL
constraint, you'll receive an error. For
instance:
• INSERT INTO Employees (EmployeeID,
FirstName, LastName) VALUES (1, 'John',
NULL);
• This would result in an error because
LastName is declared as NOT NULL, and a
NULL value is being attempted to be inserted.
• To allow NULL values again, you would need to
alter the table and remove the NOT NULL
constraint:
• ALTER TABLE Employees
• ALTER COLUMN LastName VARCHAR(50) NULL;
• Now, LastName column will allow NULL values.
• In summary, using the NOT NULL constraint
ensures data integrity by disallowing NULL values
in the specified column, thereby requiring that
each row has a valid value for that column.
Comparisons Using Null Values
• When performing comparisons involving null
values in SQL, it's essential to understand how
null values behave. Null represents a missing
or unknown value in SQL. Comparisons
involving null values often yield results that
may not be immediately intuitive due to the
unknown nature of null.
• Here are some common behaviors when comparing
null values in SQL:
1. Equality Comparisons: Comparing a null value to
another value, including another null value, using the
equality operator (=) results in an unknown or null
outcome. This is because the value of the null is
unknown.
SELECT * FROM Employees WHERE FirstName =
NULL;
This query doesn't return any rows, even if there are
records with a null value for the FirstName column.
Instead, you should use the IS NULL or IS NOT NULL
operators to check for null values:
SELECT * FROM Employees WHERE FirstName IS
NULL;
2. Inequality Comparisons: Similarly, comparing
a null value to another value using inequality
operators (<>, !=, >, <, >=, <=) also results in
an unknown outcome.
• SELECT * FROM Employees WHERE FirstName
<> NULL;
• This query also doesn't return any rows.
Instead, you should use the IS NULL or IS NOT
NULL operators to check for null values.
3. Aggregate Functions: Aggregate functions
such as COUNT, SUM, AVG, etc., generally
ignore null values unless explicitly specified
otherwise.
SELECT COUNT(FirstName) FROM Employees;
This query counts the non-null values in the
FirstName column. To count all values,
including nulls, you could use:
SELECT COUNT(*) FROM Employees;
• Sorting: Null values are often treated differently when
sorting data. Depending on the database system, null
values might be sorted either at the beginning or end
of a result set. You can use the ORDER BY clause with
the NULLS FIRST or NULLS LAST option to specify how
nulls should be ordered.
• SELECT * FROM Employees ORDER BY FirstName
NULLS FIRST;
• his query sorts the result set by the FirstName column,
with null values appearing first.
• Overall, handling null values properly is crucial for
obtaining accurate query results in SQL. Understanding
how nulls behave in comparisons and utilizing
appropriate operators like IS NULL or IS NOT NULL
helps ensure correct query logic.

More Related Content

PPT
SQL subquery
PDF
Dbms keys
PDF
Create table
PDF
Unit ii chapter 2 Decision making and Branching in C
PPTX
Loop in C Properties & Applications
PPTX
Sql Constraints
PPTX
Sql subquery
SQL subquery
Dbms keys
Create table
Unit ii chapter 2 Decision making and Branching in C
Loop in C Properties & Applications
Sql Constraints
Sql subquery

What's hot (20)

PPT
Single row functions
PPT
SQL DDL
PPT
Joins in SQL
PPTX
5. stored procedure and functions
PPTX
Integrity Constraints
PDF
[APJ] Common Table Expressions (CTEs) in SQL
 
PPTX
Relational model
PPTX
1.2 sql create and drop table
PPT
If and nested if statements
PDF
Nested Queries Lecture
PPT
SQL select clause
PPTX
DPLYR package in R
PPT
PL/SQL Introduction and Concepts
PPTX
Theory of automata and formal language
PPTX
Aggregate function
PPTX
SQL Joins.pptx
PPTX
Nested queries in database
PPTX
Sql Functions And Procedures
PPTX
PPT
Sql join
Single row functions
SQL DDL
Joins in SQL
5. stored procedure and functions
Integrity Constraints
[APJ] Common Table Expressions (CTEs) in SQL
 
Relational model
1.2 sql create and drop table
If and nested if statements
Nested Queries Lecture
SQL select clause
DPLYR package in R
PL/SQL Introduction and Concepts
Theory of automata and formal language
Aggregate function
SQL Joins.pptx
Nested queries in database
Sql Functions And Procedures
Sql join
Ad

Similar to Null Values.ppt briefing about null values in SQL. Very helpful if you are learning SQL (20)

PPTX
Aggregate functions in SQL.pptx
PPTX
Aggregate functions in SQL.pptx
PDF
0808.pdf
PDF
0808.pdf
PPTX
DBMS.pptx
PDF
Integrity constraint fundamentals of dbms.pdf
PPTX
data base programming chapter2 29 slides
PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
PPTX
More Complex SQL and Concurrency ControlModule 4.pptx
PDF
Oracle SQL Part 2
PPT
Oracle Sql & PLSQL Complete guide
PPTX
Data-driven Programming Class for University by Slidesgo.pptxData-driven Prog...
PPTX
5 surprising oracle sql behaviors that very few people know
PPTX
Practical 03 (1).pptx
PDF
Autonumber
PPT
Advanced Sql Training
PPT
Using subqueries to solve queries
PPTX
Data Definition Language Commands in DBMS
PPT
Chinabankppt
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
0808.pdf
0808.pdf
DBMS.pptx
Integrity constraint fundamentals of dbms.pdf
data base programming chapter2 29 slides
Database Systems - SQL - DDL Statements (Chapter 3/3)
More Complex SQL and Concurrency ControlModule 4.pptx
Oracle SQL Part 2
Oracle Sql & PLSQL Complete guide
Data-driven Programming Class for University by Slidesgo.pptxData-driven Prog...
5 surprising oracle sql behaviors that very few people know
Practical 03 (1).pptx
Autonumber
Advanced Sql Training
Using subqueries to solve queries
Data Definition Language Commands in DBMS
Chinabankppt
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Ad

Recently uploaded (20)

PPTX
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
PDF
L-0018048598visual cloud book for PCa-pdf.pdf
PDF
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
PPTX
Principles of Inheritance and variation class 12.pptx
PPTX
The Stock at arrangement the stock and product.pptx
PDF
Entrepreneurship PowerPoint for students
PDF
Prostaglandin E2.pdf orthoodontics op kharbanda
PDF
Understanding the Rhetorical Situation Presentation in Blue Orange Muted Il_2...
PPTX
ESD MODULE-5hdbdhbdbdbdbbdbdbbdndbdbdbdbbdbd
PDF
Why Today’s Brands Need ORM & SEO Specialists More Than Ever.pdf
PPTX
Cerebral_Palsy_Detailed_Presentation.pptx
PPTX
cse couse aefrfrqewrbqwrgbqgvq2w3vqbvq23rbgw3rnw345
PPTX
1751884730-Visual Basic -Unitj CS B.pptx
PPT
Gsisgdkddkvdgjsjdvdbdbdbdghjkhgcvvkkfcxxfg
PPTX
microtomy kkk. presenting to cryst in gl
PPTX
Your Guide to a Winning Interview Aug 2025.
PDF
Blue-Modern-Elegant-Presentation (1).pdf
PPTX
Surgical thesis protocol formation ppt.pptx
PPTX
OnePlus 13R – ⚡ All-Rounder King Performance: Snapdragon 8 Gen 3 – same as iQ...
PDF
MCQ Practice CBT OL Official Language 1.pptx.pdf
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
L-0018048598visual cloud book for PCa-pdf.pdf
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
Principles of Inheritance and variation class 12.pptx
The Stock at arrangement the stock and product.pptx
Entrepreneurship PowerPoint for students
Prostaglandin E2.pdf orthoodontics op kharbanda
Understanding the Rhetorical Situation Presentation in Blue Orange Muted Il_2...
ESD MODULE-5hdbdhbdbdbdbbdbdbbdndbdbdbdbbdbd
Why Today’s Brands Need ORM & SEO Specialists More Than Ever.pdf
Cerebral_Palsy_Detailed_Presentation.pptx
cse couse aefrfrqewrbqwrgbqgvq2w3vqbvq23rbgw3rnw345
1751884730-Visual Basic -Unitj CS B.pptx
Gsisgdkddkvdgjsjdvdbdbdbdghjkhgcvvkkfcxxfg
microtomy kkk. presenting to cryst in gl
Your Guide to a Winning Interview Aug 2025.
Blue-Modern-Elegant-Presentation (1).pdf
Surgical thesis protocol formation ppt.pptx
OnePlus 13R – ⚡ All-Rounder King Performance: Snapdragon 8 Gen 3 – same as iQ...
MCQ Practice CBT OL Official Language 1.pptx.pdf

Null Values.ppt briefing about null values in SQL. Very helpful if you are learning SQL

  • 1. Null Values: Disallowing Null Values, Comparisons Using Null Values Dr. Shalini Gambhir
  • 2. • In SQL, you can disallow null values in a column by using the NOT NULL constraint when creating or altering a table. This constraint ensures that every row in the table must have a value for that column and that value cannot be NULL.
  • 3. Here's an example of creating a table with the NOT NULL constraint: • CREATE TABLE Employees ( • EmployeeID INT PRIMARY KEY, • FirstName VARCHAR(50) NOT NULL, • LastName VARCHAR(50) NOT NULL, • Department VARCHAR(50) • ); • In this example, both FirstName and LastName columns are specified as NOT NULL, meaning that whenever a new record is inserted into the Employees table, these columns must have a value, and NULL values are not allowed.
  • 4. • If you try to insert a record without providing a value for a column with the NOT NULL constraint, you'll receive an error. For instance: • INSERT INTO Employees (EmployeeID, FirstName, LastName) VALUES (1, 'John', NULL); • This would result in an error because LastName is declared as NOT NULL, and a NULL value is being attempted to be inserted.
  • 5. • To allow NULL values again, you would need to alter the table and remove the NOT NULL constraint: • ALTER TABLE Employees • ALTER COLUMN LastName VARCHAR(50) NULL; • Now, LastName column will allow NULL values. • In summary, using the NOT NULL constraint ensures data integrity by disallowing NULL values in the specified column, thereby requiring that each row has a valid value for that column.
  • 6. Comparisons Using Null Values • When performing comparisons involving null values in SQL, it's essential to understand how null values behave. Null represents a missing or unknown value in SQL. Comparisons involving null values often yield results that may not be immediately intuitive due to the unknown nature of null.
  • 7. • Here are some common behaviors when comparing null values in SQL: 1. Equality Comparisons: Comparing a null value to another value, including another null value, using the equality operator (=) results in an unknown or null outcome. This is because the value of the null is unknown. SELECT * FROM Employees WHERE FirstName = NULL; This query doesn't return any rows, even if there are records with a null value for the FirstName column. Instead, you should use the IS NULL or IS NOT NULL operators to check for null values: SELECT * FROM Employees WHERE FirstName IS NULL;
  • 8. 2. Inequality Comparisons: Similarly, comparing a null value to another value using inequality operators (<>, !=, >, <, >=, <=) also results in an unknown outcome. • SELECT * FROM Employees WHERE FirstName <> NULL; • This query also doesn't return any rows. Instead, you should use the IS NULL or IS NOT NULL operators to check for null values.
  • 9. 3. Aggregate Functions: Aggregate functions such as COUNT, SUM, AVG, etc., generally ignore null values unless explicitly specified otherwise. SELECT COUNT(FirstName) FROM Employees; This query counts the non-null values in the FirstName column. To count all values, including nulls, you could use: SELECT COUNT(*) FROM Employees;
  • 10. • Sorting: Null values are often treated differently when sorting data. Depending on the database system, null values might be sorted either at the beginning or end of a result set. You can use the ORDER BY clause with the NULLS FIRST or NULLS LAST option to specify how nulls should be ordered. • SELECT * FROM Employees ORDER BY FirstName NULLS FIRST; • his query sorts the result set by the FirstName column, with null values appearing first. • Overall, handling null values properly is crucial for obtaining accurate query results in SQL. Understanding how nulls behave in comparisons and utilizing appropriate operators like IS NULL or IS NOT NULL helps ensure correct query logic.