Welcome to another amazing course on CodeSignal!
As we dive deep into the world of SQL, it's important to understand the mechanics of logical operators. SQL offers several logical operators, but in this lesson, we'll focus on two of the most commonly used ones: AND
and OR
. Logical operators play a crucial role in specifying conditions in an SQL statement and are very helpful in retrieving precise and accurate results. The AND
operator allows you to select rows that satisfy both conditions A and B, whereas the OR
operator helps you select rows that satisfy either condition A or B.
And like with some other introductory SQL courses you might have taken on CodeSignal, this one too uses a dataset which is inspired by the discography of Taylor Swift and contains three main tables. Below are some sample rows to give you an idea of the structure starting from the simplest table:
Albums Table
AlbumID | AlbumName | ReleaseDate |
---|---|---|
22 | Speak Now (Deluxe Edition) | 2010-10-25 |
23 | Speak Now | 2010-10-25 |
24 | Fearless Platinum Edition | 2008-11-11 |
25 | Fearless | 2008-11-11 |
Albums table contains all of Taylor's albums including release dates.
SongID | AlbumID | Name | TrackNumber | URI | Popularity | DurationMS |
---|---|---|---|---|---|---|
0vqI4ZIMuifeKeItGiWbPj | 19 | Starlight | 15 | spotify:track:0vqI4ZIMuifeKeItGiWbPj | 40 | 217826 |
0vvt4IZOMkRug195S4MUq0 | 22 | If This Was A Movie | 16 | spotify:track:0vvt4IZOMkRug195S4MUq0 | 46 | 234546 |
0wavGRldH0AWyu2zvTz8zb | 6 | Sweet Nothing | 12 | spotify:track:0wavGRldH0AWyu2zvTz8zb | 73 | 188496 |
0XfOV7qY3834QpFVwOb6CC | 19 | Treacherous | 3 | spotify:track:0XfOV7qY3834QpFVwOb6CC | 41 | 240773 |
This table lists Taylor Swift's songs, linking them to their albums, and includes details about track numbers, Spotify URIs, popularity ratings, and song lengths in milliseconds.
SongID | Acousticness | Danceability | Energy | Instrumentalness | Liveness | Loudness | Speechiness | Tempo | Valence |
---|---|---|---|---|---|---|---|---|---|
4WUepByoeqcedHoYhSNHRt | 0.00942 | 0.757 | 0.61 | 0.0000366 | 0.367 | -4.84 | 0.0327 | 116.998 | 0.685 |
0108kcWLnn2HlH2kedi1gn | 0.0885 | 0.733 | 0.733 | 0 | 0.168 | -5.376 | 0.067 | 96.057 | 0.701 |
3Vpk1hfMAQme8VJ0SNRSkd | 0.000421 | 0.511 | 0.822 | 0.0197 | 0.0899 | -4.785 | 0.0397 | 94.868 | 0.305 |
1OcSfkeCg9hRC2sFKB4IMJ | 0.000537 | 0.545 | 0.885 | 0.0000559 | 0.385 | -5.968 | 0.0447 | 92.021 | 0.206 |
The SongFeatures
table breaks down the musical traits of each song, like its rhythm, energy, loudness, and mood, among others. It helps us understand what makes each song unique beyond just the lyrics and tune.
Think of SQL statements as real-world phrases that you can dissect into multiple parts. Each part plays a specific role. For instance, consider
SQL1SELECT *
2FROM Songs
3WHERE Popularity > 80 AND DurationMS BETWEEN 180000 AND 240000;
This SQL query has distinct parts:
SELECT *
: This phrase indicates that it wants to retrieve all columns.FROM Songs
: This phrase specifies the table from which to retrieve the data.WHERE
: This word starts the condition clause, which refines the query.Popularity > 80 AND DurationMS BETWEEN 180000 AND 240000
: These are the conditions that rows must meet to be included in the result.Notice the AND
operator here? It helps us set multiple conditions. Our statement tells SQL to "Show me all columns from the Songs table, but only those which are popular (Popularity > 80
) and whose DurationMS
is between 180000 and 240000".
Logical operators like AND
and OR
aren't just for boolean values. They can be used with numeric data, too. In SQL, numeric comparisons are quite straightforward: you can use =
for equality, >
for greater than, <
for less than, >=
for greater than or equal to, <=
for less than or equal to, and <>
or !=
for not equal to.
We can use the AND
logical operator to add multiple conditions to our SQL statement. For instance, in our example, we want a list of songs, but not just any songs. We're looking for the popular ones (Popularity > 80
), AND those within a certain duration (DurationMS BETWEEN 180000 AND 240000
). Similarly, the OR
operator allows us to query for songs that meet either one condition or another, enabling more flexible data retrieval based on varying criteria.
Just like numeric fields, we can use logical operators to work with date fields. Here is another example:
SQL1SELECT Songs.*
2FROM Songs
3JOIN Albums ON Albums.AlbumID = Songs.AlbumID
4WHERE Albums.ReleaseDate > '2020-01-01' AND Songs.TrackNumber < 5;
In this statement, we're looking for all songs (SELECT Songs.*
) from the dataset where the ReleaseDate
is after the first day of 2020, AND the song is among the first four tracks (Songs.TrackNumber < 5
). We use the AND
operator to combine these two conditions.
That wraps up our introductory lesson on using AND/OR
logical operators in SQL. We've learned how to construct SQL queries using these operators to filter out specific data based on multiple conditions. Now that you've gained a solid understanding of using AND/OR
operators, it's time to put this knowledge into practice. In the next section, you'll encounter various exercises to solve, giving you hands-on experience in using logical operators in SQL to retrieve specific data. Keep practicing and happy querying!