Lesson 1
Mastering Logical Operators: The Art of Data Filtering with AND/OR in SQL
Introduction to Logical Operations in SQL

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.

Brief Dataset Description

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

AlbumIDAlbumNameReleaseDate
22Speak Now (Deluxe Edition)2010-10-25
23Speak Now2010-10-25
24Fearless Platinum Edition2008-11-11
25Fearless2008-11-11

Albums table contains all of Taylor's albums including release dates.

Songs Table
SongIDAlbumIDNameTrackNumberURIPopularityDurationMS
0vqI4ZIMuifeKeItGiWbPj19Starlight15spotify:track:0vqI4ZIMuifeKeItGiWbPj40217826
0vvt4IZOMkRug195S4MUq022If This Was A Movie16spotify:track:0vvt4IZOMkRug195S4MUq046234546
0wavGRldH0AWyu2zvTz8zb6Sweet Nothing12spotify:track:0wavGRldH0AWyu2zvTz8zb73188496
0XfOV7qY3834QpFVwOb6CC19Treacherous3spotify:track:0XfOV7qY3834QpFVwOb6CC41240773

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.

SongFeatures Table
SongIDAcousticnessDanceabilityEnergyInstrumentalnessLivenessLoudnessSpeechinessTempoValence
4WUepByoeqcedHoYhSNHRt0.009420.7570.610.00003660.367-4.840.0327116.9980.685
0108kcWLnn2HlH2kedi1gn0.08850.7330.73300.168-5.3760.06796.0570.701
3Vpk1hfMAQme8VJ0SNRSkd0.0004210.5110.8220.01970.0899-4.7850.039794.8680.305
1OcSfkeCg9hRC2sFKB4IMJ0.0005370.5450.8850.00005590.385-5.9680.044792.0210.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.

Understanding Syntax and Clause Structure

Think of SQL statements as real-world phrases that you can dissect into multiple parts. Each part plays a specific role. For instance, consider

SQL
1SELECT * 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".

Working with Numeric Fields Using AND/OR

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.

Using Logical Operators to Work with Date Fields

Just like numeric fields, we can use logical operators to work with date fields. Here is another example:

SQL
1SELECT 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.

Summary and Upcoming Practice Overview

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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.