Learning how to use the NOT, IN, and BETWEEN operators

Some additional logical operators are NOT, IN, and BETWEEN:

  • IN allows you to list the values that you want to return in your query results. For instance, if you want to return any values in g_all (all games played) that are in 40, 50, or 60, you could run the following query: 
USE lahmansbaseballdb;
SELECT playerid, g_all, g_batting, g_defense FROM appearances
WHERE g_all IN (40, 50, 60);
  • BETWEEN allows you to list two values, and your query will return all the values between and including those values. For instance, if you want to return any values in g_all that are between 40 and 60, you could run the following query: 
USE lahmansbaseballdb;
SELECT playerid, g_all, g_batting, g_defense FROM appearances
WHERE g_all BETWEEN 40 and 60;
  • NOT allows you to exclude values from your query. For instance, if you want to return any values in g_all that are NOT between 40 and 60, you can execute the following query:  
USE lahmansbaseballdb;
SELECT playerid, g_all, g_batting, g_defense FROM appearances
WHERE g_all NOT BETWEEN 40 and 60;

The previous query will give you the results shown in the following screenshot: 

You can also use NOT with IN. For instance, if you want to return any values in g_all that are NOT IN 40, 50, or 60, you can execute the following query: 

USE lahmansbaseballdb;
SELECT playerid, g_all, g_batting, g_defense FROM appearances
WHERE g_all NOT IN (40, 50, 60);

You can also use NOT in multiple clauses of the WHERE clause, as shown in the following query: 

USE lahmansbaseballdb;
SELECT playerid, g_all, g_batting, g_defense FROM appearances
WHERE g_all NOT IN (40, 50, 60)
OR g_batting NOT BETWEEN 30 and 40;

You can also combine it with AND, as shown in the following query: 

USE lahmansbaseballdb;
SELECT playerid, g_all, g_batting, g_defense FROM appearances
WHERE g_all NOT IN (40, 50, 60)
AND g_batting NOT BETWEEN 30 and 40;

As you can see, there are many ways to use IN, BETWEEN, and NOT

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.167.114