NULL Values in SQL Queries

NULL Values in SQL Queries

Today’s post is about NULL values in SQL, and comes courtesy of my friend and database wizard, Kaley. You should check out his website if you’d like to learn more about SQL, Oracle database, and making queries run faster .


Here’s a topic that gets a lot of budding developers in trouble–the concept of NULL values in SQL queries.

Whenever you issue a SQL query to a database…and you want to know whether a column has a NULL value in it…what is the proper way to write a query that will find the result?

Should you use a query like this?

SELECT * FROM SOME_TABLE
WHERE SOME_COLUMN = NULL

Or! Should you use a query like this?

SELECT * FROM SOME_TABLE
WHERE SOME_COLUMN IS NULL

…The answer is, you should be using the second query (WHERE SOME_COLUMN IS NULL).

Now why is that?

We don’t use the “IS” keyword with any other comparisons in the database, right?

If we want to know if a field is equal to one, we use a WHERE clause like this:

WHERE SOME_COLUMN = 1

So why on earth would we do the IS keyword with a NULL value? Why would we need to treat NULL differently?

The answer is this: In SQL, NULL represents the concept of “unknown” (so a NULL value represents an “unknown” value).

Null as an Unknown

In most databases, there is a difference between NULL and an empty string (represented by a “double apostrophe” or ”).

But this isn’t always true for all databases:  For example, Oracle database won’t allow you to have an empty string. Anytime Oracle database sees an empty string, it automatically converts the empty string into a NULL value.

For the majority of the other databases out there, though, a NULL value is treated differently than an empty string:

  • An empty string is treated like a known value where there is no value.
  • A NULL value is treated like a value that is not known.

This would be the difference between me asking the question, “What was U.S President Theodore Roosevelt’s middle name?”

  • One answer might be, “Well, I don’t know what Theodore Roosevelt’s middle name is.” (This idea could be represented by a NULL value in the MIDDLE_NAME column for Theodore Roosevelt’s record)
  • Another possible answer could be “President Theodore Roosevelt actually didn’t have a middle name. His parents never gave him a middle name, and I know for a fact that Theodore Roosevelt didn’t have a middle name.” (You would represent that by putting an empty string, or a ” into the MIDDLE_NAME column)

Oracle database is the most notable exception where those two values are actually both going to be represented by NULL–most databases other than Oracle are going to treat NULL and an empty string very differently.

As long as you can remember that a NULL value represents an unknown value, then this is going to help you craft your SQL queries, and help you work around some of the trickier situations that you can get in with NULL values.

If, for example, you were to have a query that uses a WHERE clause like this:

SELECT * FROM SOME_TABLE
WHERE 1 = 1

This query will return rows (assuming SOME_TABLE isn’t an empty table!) because the expression “1 = 1” is provably true…it can be proven to be true.

If I were to say:

SELECT * FROM SOME_TABLE
WHERE 1 = 0

…then the database sees this and evaluates “1 = 0” as false (meaning this query would never return any rows).

But if I were to say:

SELECT * FROM SOME_TABLE
WHERE 1 = NULL

The database basically goes, “I don’t know if these two values (1 and our black-box NULL value) are equal”…so it doesn’t return any records.

Ternary Logic

When you have a WHERE clause in a SQL query, it can have one of three different results:

  • It can be true (and it will return rows)
  • It can be false (and it won’t return rows)
  • Or it can be NULL or unknown (an unknown is also not going to return values)

You may be thinking, “Okay, but why do I care that there’s a difference between false and null since the database handles those two values exactly the same?”

Well, let me show you where you can run into trouble:  Let’s introduce the NOT() condition.

If you were to say:

SELECT * FROM SOME_TABLE
WHERE NOT(1 = 1)

Then the database is first going to evaluate 1 = 1 and say, “Okay, that’s clearly true.”

But then it’s going to apply the NOT() condition to it. The database is going to go, “Well true, when notted, turns to false…so the NOT() condition causes our WHERE clause to be false here.”

So the query above isn’t going to return any records.

However, if you were to say:

SELECT * FROM SOME_TABLE
WHERE NOT(1 = 0)

Then the database first evaluates expression 1 = 0 and says, “That’s clearly false.”

But then it’s going to apply the NOT() condition, which will give us the opposite result, so it becomes true.

So this query will return records!

What if I issued the following query though?

SELECT * FROM SOME_TABLE
WHERE NOT(1 = NULL)

The database is first going to evaluate 1 = NULL. (Remember, it’s going to treat NULL like an unknown value!)

It’s going to say, “I can’t say whether or not 1 is equal to NULL because I don’t know what the NULL (unknown) value is.”

So it doesn’t yield a true result, and it doesn’t yield a false result–it instead yields a NULL (or unknown) result.

This NULL result is going to be interpreted by the NOT() operator.

Whenever you take a NULL and you put it in a NOT() condition…the result is another NULL! (the opposite of unknown is…well…another unknown).

So the NOT() operator doesn’t do anything with null conditions.

So NEITHER of these queries…

SELECT * FROM SOME_TABLE
WHERE NOT(1 = NULL)

SELECT * FROM SOME_TABLE
WHERE 1 = NULL

…is ever going to return any records…even though they’re opposites!

NULL and NOT IN

If I issued a query with a WHERE clause like this:

SELECT * FROM SOME_TABLE
WHERE 1 IN (1, 2, 3, 4, NULL)

…then clearly the WHERE clause is going to be true, and this query will return records, since 1 is in our IN list…

But if I were to say:

SELECT * FROM SOME_TABLE
WHERE 1 NOT IN (1, 2, 3, 4, NULL)

Then clearly this is going to be false, and this query will never return records, since the number 1 appears in our IN list and we’re saying “NOT IN”…

Now what if I were to say something like this?

SELECT * FROM SOME_TABLE
WHERE 5 NOT IN (1, 2, 3, 4, NULL)

This WHERE clause will never return any records, since it is not provably true (it cannot be proven to be true). The number 5 doesn’t explicitly appear in the “IN” list–But 5 could be inside our “black box” NULL value (the database doesn’t necessarily know what the value of NULL is).

This yields a NULL result (meaning an unknown result) and this WHERE clause is never going to return any records.

This is why it’s important to consider a NULL value to be equivalent to an unknown value–it’s going to help you whenever you craft complex SQL queries.

Hopefully you’re now equipped to deal with NULL values in SQL queries! For more information on SQL, Oracle database, and making queries run faster, visit blog.tuningsql.com.

How To Make Appealing Game Graphics

How To Make Appealing Game Graphics

We’re taking a break from my programming series to bring you a post coming courtesy of my first guest contributor, Ashlie Lopez! She is an experienced writer who will be covering a side of building cool technology that, based on the visuals of my hangman game, I probably have no business trying to tackle. She will be discussing how to make appealing game graphics.

Enjoy!


Developing realistic and appealing game graphics is not an easy task. When a user first sees your game, his first impression is based on the art of the game. Gamers tend to form an opinion about a game based on the overall appeal and aesthetics before actually playing the game. Therefore, game developers and producers invest a major chunk of their budget into game art and design.

As a designer, there are many different areas that you need to work on to create visually stunning graphics. Your designs should give a good user experience to players and should be consistent in terms of characters, colors, backdrop etc.

Whether you are an experienced or a new graphic designer, the following are some useful tips and techniques that will help you create better visuals for your game:

Follow the principles of design

The job of a skilled graphic designer is to understand and create designs by following the principles of design. The principles of design help a designer convert various design elements into powerful visuals. In order to create correct and meaningful visuals that leave a strong impact on the user’s mind, you need follow the rules of designing:

  1. Alignment 
  2. Proportion
  3. Repetition
  4. Contrast
  5. Balance
  6. Emphasis
  7. Hierarchy
  8. Harmony
  9. Unity
  10. Proximity

As a designer, it is your job to understand each and every element and use them as powerful tools to create stunning game designs. 

Have strong layouts

A layout is the foundation of every design. As a game designer, your layouts should be well balanced and well-proportioned in order to connect with the audience’s eye. In simple words, a layout is simply the assortment of various graphical elements. A layout also ensures consistency in designs. A strong layout can help viewers to focus on gaming elements that are the most important.

Every element in a game has its own identity and significance. The value of each element depends on its position on the game’s layout along with the proportion, repetition and contrast of the element.

Add depth to gaming images

The use of depth to achieve appealing game graphics

Depth is yet another useful element of a visual image that helps to make certain images more elaborate and prominent in a game.

A designer can use depth as a powerful tool to help players get a better understanding of scale. Depth can be achieved through experimenting with lighting, outlining, and adjusting the hues and saturation of an image.

Provide unique styling

For every game, you need to come up with a new style that would give a distinctive identity to your design. You play with colors and lighting to create a new style for your design. Your graphics should be unique enough to attract the attention of gamers. Whether it is the backdrop or the characters of the game, you need to design them in such a way that they instantly create a connection with the players.

As a designer, your main goal should be to create crisp and sharp graphics. It is not necessary to create too realistic or intricate graphics to make your character stand out. Sometimes, going for a minimalist approach by creating simple characters is more effective than creating overly complex designs.

Enhance graphics with colors

Colors are the one the simplest tools for bringing images to life. In games, it is recommended to go for saturated and vibrant colors. However, every game has its own unique requirements based on its story line, characters and genre.  More vibrant and bright saturated colors work for casual or strategic games that are designed to cater to a wider audience of different age groups. Pastel colors are mostly used for fantasy video games. You can use customized video game glasses to enhance your gaming experience.

Get constructive feedback

Getting constructive feedback and criticism is a great way to know which design element works for the game and which could be replaced with a better alternative. Learning and improving your skills from feedback is a great way to keep upgrading your portfolio and stay relevant in the industry. Through feedback and suggestions, a designer gets to know about his strength and weaknesses in designing. The best and most honest review that a game designer could get is from the gamers themselves.