Hello again! Thus far in this course, we've explored the building blocks of SQL, such as SELECT
statements and the WHERE
clause. These tools assist us in querying data and retrieving it from our music-based database. However, to delve deeper and retrieve more precise data, we need additional tools. Currently, we’re going to learn about SQL logical operators.
Logical Operators are at the heart of any computational language, SQL being no exception. They're used in the WHERE
clause of SELECT
statements (as well as other statements like INSERT
, UPDATE
, and DELETE
which you'll learn about in the future) to combine or negate conditions and ultimately help us sieve out precise information from our database.
Firstly, we have the AND
and OR
operators.
An AND
operator returns TRUE if both listed conditions are true. It essentially narrows your search results because it adds more conditions that records must meet.
Meanwhile, an OR
operator returns TRUE if either of the conditions listed is true, effectively broadening your search results because it only requires one of the conditions to be met.
To see them in action, follow the code examples:
SQL1/* Using 'AND' operator */
2SELECT * FROM Albums WHERE AlbumID > 2 AND ReleaseDate < '2009-01-01';
3
4/* Using 'OR' operator */
5SELECT * FROM Songs WHERE Popularity >= 70 OR DurationMS > 240000;
Now let's analyze the above code snippets:
In the first example, we employ the AND
operator, which will extract albums from the database (SELECT * FROM Albums
) that meet both conditions - the AlbumID is greater than 2 and the ReleaseDate is earlier than 2009-01-01.
In the second snippet, we utilize an OR
operator, extracting songs that are either popular (Popularity >= 70) or longer in duration (DurationMS > 240000).
Next, we have the IN
and BETWEEN
operators:
The IN
operator allows us to specify multiple values in a WHERE
clause, a clean, efficient alternative to multiple OR conditions.
The BETWEEN
operator selects values within a given range, which can be numbers, text, or dates.
Now let's use these operators:
SQL1/* Using 'IN' operator */
2SELECT * FROM Songs WHERE AlbumID IN (1, 2, 3);
3
4/* Using 'BETWEEN' operator */
5SELECT * FROM Songs WHERE TrackNumber BETWEEN 1 AND 5;
The first example employs the IN
operator to extract (SELECT * FROM Songs
) songs from albums that have an AlbumID of 1, 2, or 3. It's less tedious than writing AlbumID = 1 OR AlbumID = 2 OR AlbumID = 3
.
In the next line, the BETWEEN
operator performs a range-based search. So, this command will extract songs that have a TrackNumber between 1 and 5, i.e., numbers 1, 2, 3, 4, and 5.
Finally, we have the NOT
operator, which is used to exclude records that meet specific conditions. Let's try it out in a SELECT
statement:
SQL1/* Using 'NOT' operator */
2SELECT * FROM SongFeatures WHERE Energy NOT BETWEEN 0.5 AND 0.8;
In the snippet above, we use the NOT
operator to exclude certain records from our selection. This line extracts features of songs where the Energy is not in the range of 0.5 to 0.8.
Today, we've navigated the terrains of SQL Logical Operators - AND
, OR
, IN
, BETWEEN
, and NOT
. By adding or negating conditions, they enhance the precision of our data retrieval operations. In your upcoming practice sessions, make sure to use these operators and master various combinations.
Now that SQL queries and Taylor Swift’s discography have been brought together, learning about data doesn't seem all too intimidating, does it? Don't forget that music — just like SQL — is all about harmony and rhythm. Follow the rhythm of SQL operators and create a harmony of insightful data. Keep practicing!