0% found this document useful (0 votes)
9 views34 pages

SQL Arithmetic and Null Value Handling

This document is a lesson on using arithmetic expressions and handling null values in SQL. It includes a pre-test and assessment with true/false statements, technical terms related to SQL functions, and examples of how null values behave in different conditions. Additionally, it provides instructions for generating a report of employee salaries using SQL Developer.

Uploaded by

ALDRIN LLAGAS
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views34 pages

SQL Arithmetic and Null Value Handling

This document is a lesson on using arithmetic expressions and handling null values in SQL. It includes a pre-test and assessment with true/false statements, technical terms related to SQL functions, and examples of how null values behave in different conditions. Additionally, it provides instructions for generating a report of employee salaries using SQL Developer.

Uploaded by

ALDRIN LLAGAS
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Using Arithmetic Expressions and

Null Values LESSON 4


Attendance check
CHAPTER
RETRIEVING DATA USING THE 1
SQL SELECT STATEMENT
Lesson 1: Listing Capabilities of SQL SELECT Statements
Lesson 2: Generating Report of Data from the Output of a Basic
Select Statement
Lesson 3: Sorting and Restricting Data
Lesson 4: Using Arithmetic Expressions and NULL Values
Lesson 5: Implementing Column Aliases
Lesson 6: Describing Concatenation Operator, Literal Character
Strings, Alternative Quote Operator, and the Distinct Keyword
Using Arithmetic Expressions and
Null Values LESSON 4
Pre-test

Directions: Write the word


True if the statement is
correct. And Write the word
False if it is incorrect.
__________1. Use two consecutive minus signs (--) in arithmetic
expressions to indicate double negation or the subtraction of a
negative value.
__________2. You should separate consecutive minus signs with a
space or parentheses.
__________3. To check for nulls, use only the comparison conditions
ISNULL and ISNOTNULL.
__________4. Every function always displays a null value except for
the REPLACE, NVL, and CONCAT function.
__________5. If a column in a row has no value, then the column is
said to be null or to contain null.
__________6. If the value of X is 10 and the condition is X IS
NULL, then the result will be a true value.
__________7. If the value of X. is NULL and the condition is
X !=5, then the result will be UNKNOWN.
__________8. If the value of X is null and the condition is X=
15, then the result will be UNKOWN.
__________9. If the value of X is NULL and the condition is
X= NULL, then the result will be a TRUE value.
__________10. If the value of X is NULL and the condition is
X IS NOT NULL, then the result will be a FALSE value.
Using Arithmetic Expressions and
Null Values LESSON 4
[Link] [Link]
[Link] [Link]
[Link] [Link]
[Link] [Link]
[Link] [Link]
Using Arithmetic Expressions and
Null Values LESSON 4
TECHNICAL TERMS

• CONCAT function - used to concatenate two strings to


form a single string.
• condition - this is an expression of several operators or
expressions that evaluate True or False, or Unknown
• DECODE function – compares expressions to each
search value one by one.
• NVL function – replaces an N/A value or empty string
with a string
TECHNICAL TERMS

• Operators – these are represented by a


single character or reserved words
• REPLACE function – replaces a sequence
of characters in a string with another set
of characters.
• SCALAR function – returns a single value
based on the input value.
Arithmetic Expressions
In SQL expressions, arithmetic
operators are used to add, subtract,
multiply, divide, and negate data
values. In SQL statements, arithmetic
expressions are used to perform a
calculation.
Operator Purpose Example

+~ Unary operator operates SELECT * FROM item WHERE


one on one operand. It y=-1; or SELECT * FROM
denotes a positive or employee WHERE –salary<1;
negative expression.

+~ Binary operator operates on SELECT hire_date FROM


*/ two operands – add or employees WHERE SYSDATE-
subtract and multiply or hire_date>360;
divide. UPDATE employees SET
salary=salary * 2.2;
Using two consecutive minus signs (--) indicates
a double negation or subtraction of a negative
value. In arithmetic expression this may have
different meaning in SQL statements. Two
consecutive minus signs (--) signify the beginning of
the comment WITHIN THE SQL statement. when
using two consecutive minus signs, separate them
by placing a space or parentheses.
NULL VALUES
Some users are often confused with NULL values.
To clarify this misconception, a NULL value does not
pertain to zero value. It is an empty string () value that
represents missing or unknown data, or in applicable
values. It simply indicates that the value unknown. For
instance, if a column in a certain row contains no value,
the column is said to be null, or its value contains a null
value.
NULL VALUES
The null value always appears in all
columns of any data type except for
those columns that have a NOT NULL or
PRIMARY KEY integrity constraint. Use a
null value every time the value is not
recognized or when a value is not
significant anymore
NULL VALUES
When given a NULL value in SQL, almost all of
functions return NULL value except REPLACE, NVL, and
CONCAT. Most aggregate functions ignore the NULL
value.
Whenever a user wants to check for a null value,
he or she should always use the comparison conditions
such as ISNULL and ISNOTNULL. Other conditions will
just display INKNOWN result every time a user wants to
check for a null value.
NULLS IN CONDITIONS
Null value displays different results
based on the conditions set by the
user. The following examples show the
different results displayed every time
a null value is used in a condition.
1. if the condition is X IS NULL and the value of X is 5, then the result in this
example will be a FALSE value because the value of X is 5.
2. if the condition is X IS NOT NULL and the value of X is 5, then the result in this
example will be a TRUE value because the value of X is 5.
3. if the condition is X IS NULL and the value of NULL is 5, then the result in this
example will be a TRUE value because the value of X is a NULL value.
4. if the condition is X IS NOT NULL and the value of X is NULL, then the result in
this example will be a FALSE value because the value of X is NULL but to satisfy
the condition, the value must not be a NULL value.
5. if the condition is X = NULL and the value of X is 5, then the result in this
example will be an UNKNOWN value; that is why there will be no rows to be
returned by that query.
6. if the condition is X != NULL and the value of X is 5 , then the result in this
example will be an UNKOWN value; that is why there will be no rows to be
returned by that query.
7. if the condition is X = NULL and the value of X is NULL, then the result in this
example will be an UNKOWN value; that is why there will be no rows to be
returned by that query.
8. if the condition is X != NULL and the value of X is NULL, then the result in this
example will be an UNKOWN value; that is why there will be no rows to be
returned by that query.
9. if the condition is X = 5 and the value of X is NULL, then the result in this
example will be an UNKOWN value; that is why there will be no rows to be
returned by that query.
10. if the condition is X != 5 and the value of X is NULL, then the result in this
example will be an UNKOWN value; that is why there will be no rows to be
returned by that query.
Skills Needs Improvement Good Excellent
• I Know Arithmetic
Operations and Null
Values.
• I can describe
Arithmetic Operations
and Null Values.
• I can perform
Arithmetic Operations
and Null Values.
Using Arithmetic Expressions and
Null Values LESSON 4
ASSESSMENT

Directions: Write THE WORD


True if the statement is
correct. AND Write THE
WORD False if it is
incorrect.
__________1. Use two consecutive minus signs (--) in arithmetic
expressions to indicate double negation or the subtraction of a
negative value.
__________2. You should separate consecutive minus signs with a
space or parentheses.
__________3. If the value of X is 10 and the condition is X IS NULL,
then the result will be a true value.
__________4. Every function always displays a null value except for
the REPLACE, NVL, and CONCAT function.
__________5. If a column in a row has no value, then the column is
said to be null or to contain null.
__________6. To check for nulls, use only the comparison
conditions ISNULL and ISNOTNULL.
__________7. If the value of X. is NULL and the condition is
X !=5, then the result will be UNKNOWN.
__________8. If the value of X is null and the condition is X=
15, then the result will be UNKOWN.
__________9. If the value of X is NULL and the condition is
X= NULL, then the result will be a TRUE value.
__________10. If the value of X is NULL and the condition is
X IS NOT NULL, then the result will be a FALSE value.
[Link] [Link]
[Link] [Link]
[Link] [Link]
[Link] [Link]
[Link] [Link]
Using Arithmetic Expressions and
Null Values LESSON 4
Connect to the HR Account to generate a report of employees’ salaries
higher than ten thousand. Do these:
1. Open SQL Developer.
2. Connect to your Oracle database by typing your password.
3. Type the following code:
SELECT MIN_SALARY from JOBS
WHERE MIN_SALARY > 1000
4. Click the Run Statement button or press Ctrl+Enter.
The output should look like this:
RUBRICKS:
Criteria Outstanding Adequate Not Adequate
(6 Pts.) (5 Pts.) (4 Pts.)
Table Creation Created all of the tables defined Created most of the tables Partially created some of the
in project requirements defined in project requirements tables defined in project
Named tables appropriately in Table name was a little unclear in requirements. Table name did not
regard to their data elements regard to its data elements correlate to its data elements

Keys and Entities Correctly choose all primary and Correctly choose most of the Incorrectly choose most of the
foreign keys respecting the primary and foreign keys primary and foreign Keys, and
naming convention respecting the naming convention somehow respected the naming
convention

Implementation Completely populated tables with Populated tables with majority of Populated tables with minimal
correct data elements reflecting data elements outlined in the data elements defined in project
the design design the design

Common questions

Powered by AI

Handling NULL values appropriately is critical in database design and SQL querying to ensure data integrity and accuracy. If NULLs are not considered, it can lead to incorrect query results, misinterpretation of data such as treating NULL as zero or any other value, or database functionality errors, particularly with foreign key constraints and aggregations. Employing the correct conditions like IS NULL/IS NOT NULL in queries, understanding function behaviors concerning NULLs, and setting appropriate default constraints during database design are essential practices to mitigate issues related to NULL values .

Arithmetic operators in SQL are used to perform calculations on data values. A unary operator, such as the negative sign (-), operates on a single operand to denote a negative value, as seen in expressions like '-salary < 1'. A binary operator requires two operands and is used for basic arithmetic operations, such as addition (+), subtraction (-), multiplication (*), and division (/), exemplified by expressions like 'salary = salary * 2.2' .

NULL values in conditional SQL queries lead to an UNKNOWN result when using equality operators like '=' or '!=', because NULL represents an undefined value rather than an actual value; therefore, direct comparisons do not return true or false, resulting in no rows being selected. To avoid this issue and ensure accurate query results, IS NULL and IS NOT NULL checks should be utilized, as they are specifically designed to evaluate the presence or absence of NULLs, thereby providing a definitive true or false outcome .

Functions like REPLACE, NVL, and CONCAT in SQL are designed to handle NULL values differently because they explicitly account for cases where NULL may be involved in their operations. For instance, NVL replaces NULL with a specified value, ensuring a non-NULL result, while CONCAT always treats NULL as an empty string rather than an unknown value, allowing concatenation to occur without resulting in NULL. In contrast, most SQL functions return NULL when any of their arguments are NULL because they do not explicitly account for NULL handling beyond propagating its undefined nature .

NULL values in SQL represent missing or unknown data, distinct from a zero or an empty string, which have a defined value. In conditional expressions, NULL values yield UNKNOWN results if directly compared using operators like '=' or '!=', as these do not recognize NULL as equivalent to any value. To accurately evaluate conditions involving NULL, specific comparison operators like IS NULL or IS NOT NULL should be used .

The two consecutive minus signs (--) in SQL are used to signify the beginning of a comment within an SQL statement. This means that everything following these signs on the same line will not be executed as part of the SQL query. In arithmetic expressions, however, the meaning can differ as they may indicate double negation or the subtraction of a negative value, but correct usage necessitates separation with a space or parentheses to avoid confusion .

Comparing NULL to any other value results in UNKNOWN in SQL because NULL represents a lack of a known value rather than a concrete value itself. Unlike a specific value, it neither equals nor does not equal another value, including NULL, as there is no actual content to compare. Consequently, in logical operations, SQL treats such comparisons as indeterminate (UNKNOWN) to reflect the absence of a definite result. This necessitates the use of the IS NULL or IS NOT NULL operators for definitive evaluations .

Using arithmetic expressions within a SQL SELECT statement enables dynamic data calculation and transformation directly in the query, facilitating complex data retrieval without requiring post-processing. This feature allows for calculations such as scaling salaries ('salary * 2.2') or determining value changes over time ('SYSDATE - hire_date'). These expressions operate on the data to produce new values or conditions that can be immediately evaluated and used in sorting, filtering, or reporting operations, enhancing SQL's functionality for data manipulation .

Column aliases provide meaningful names to derived or existing columns in SQL queries, improving readability and understanding of the result set by avoiding ambiguous or unwieldy column names. They facilitate the interpretation of query outputs, especially in reports or when working with complex expressions. However, improper or inconsistent use of aliases can lead to confusion in understanding data output or in subsequent query operations, emphasizing the need for deliberate and consistent alias naming conventions for effective communication and data interpretation .

The DISTINCT keyword in SQL eliminates duplicate rows from the result set and treats NULL values as equivalent for this purpose, collapsing multiple NULL appearances into a single entry. This is particularly useful for ensuring unique data views and avoiding redundant information in queries reporting aggregated or specific data subsets. Consequently, understanding the implications of DISTINCT on set semantics and NULL values ensures more coherent query results, particularly when combined with aggregation functions and complex conditional evaluations .

You might also like